Search code examples
phparraysstringlaravelphp-7

Array to string conversion error using php v7.0.13


I have the following code in my laravel controller file.

$j_decode->$data['_kfdTourDate']->available = ($j_decode->$data['_kfdTourDate']->available+$totalincrement); 

and I am getting the following error.

ErrorException in BookingsController.php line 325: Array to string conversion in BookingsController.php line 325 at HandleExceptions->handleError('8', 'Array to string conversion', 'D:\XAMPP\htdocs\lara\app\Http\Controllers\BookingsController.php', '325', array('request' => object(Request), 'id' => '0', 'rules' => array(), 'validator' => object(Validator), 'data' => array('_kpnID' => '153290', '_kfnTourID' => '2', '_kfdTourDate' => '2017-03-16', 'nAdults' => '2', 'nChildren' => '1', 'nInfants' => '0', 'nBabies' => '2', 'nFOC' => '2', 'nPriceAdult' => '74.25', 'nPriceChild' => '49.5', 'nPriceInfant' => '0', 'nPriceBaby' => '0', 'nTotalPrice' => '148.5', 'tGuestName' => 'Yuhiko Nishioka', 'tGuestOrigin' => 'Unknown', 'tEnquirySourceWhat' => 'Unknown', 'tStatus' => 'Confirmed', '_kfnAgentID' => '0', '_kfnPersonID' => '0', '_kfnInvoiceID' => '0', 'nAgentCommissionPercent' => '0', 'nDiscount_percent' => '0', 'nDiscount_fixed' => '0', 'tNotes' => '4WD Tour package/Rezdy, applied discount', 'tInitials' => 'JD', 'CreatedOn' => '2017-01-21 15:08:00', 'ModifiedOn' => '2017-01-21 15:10:00', 'tTicketNumber' => 'Rezdy', '_kfnOrganisationID' => '0'), 'schedule' => object(Collection), 'j_decode' => object(stdClass), 'update_id_data' => object(stdClass), 'totalincrement' => '3')) in BookingsController.php line 325

Interesting part that it's working on the linux server when I upload it to my host. I have PHP Version 7.0.13 on localhost and PHP Version 5.6.30 on the server.

How can this line cause an Array to string conversion error?

I am not willing to downgrade my php version on localhost as I have other codes that php5 is not supporting.

The whole code in controller:

$rules = $this->validateForm();
$validator = Validator::make($request->all(), $rules);  
if ($validator->passes()) {
$data = $this->validatePost( $request );

$schedule = DB::table('schedule')
        ->where('id','=',$data['_kfnTourID'])
        ->get();
        
        if(isset($_SESSION['bookingiddata']))
        {
            print_r  ($j_decode= json_decode($schedule[0]->data));
            $update_id_data = json_decode($_SESSION['bookingiddata']);
            
            $totalincrement = $update_id_data->nAdults+$update_id_data->nChildren+$update_id_data->nInfants+$update_id_data->nFOC;
            //$j_decode->$data['_kfdTourDate']->available = ($j_decode->$data['_kfdTourDate']->available+$totalincrement);
            $j_decode->$data['_kfdTourDate']['available'] = ($j_decode->$data['_kfdTourDate']['available']+$totalincrement);
            ($j_decode->$data)['_kfdTourDate']->status = "available";

Also print_r $j_encode = json_encode($j_decode); prints me the following

{"2017-02-13":{"available":1,"bind":0,"info":"","notes":"","price":0,"promo":0,"status":"available"},"2017-02-14":{"available":1,"bind":0,"info":"","notes":"","price":0,"promo":0,"status":"available"},"2017-02-08":{"available":0,"bind":0,"info":"","notes":"","price":0,"promo":0,"status":"booked"},"2017-02-12":{"available":0,"bind":0,"info":"","notes":"","price":0,"promo":0,"status":"booked"},"2017-02-10":{"available":0,"bind":0,"info":"","notes":"","price":0,"promo":0,"status":"booked"},"2017-02-15":{"available":0,"bind":0,"info":"","notes":"","price":0,"promo":0,"status":"booked"},"2017-02-16":{"available":0,"bind":0,"info":"","notes":"","price":0,"promo":0,"status":"booked"}...


Solution

  • well, as mentioned here

    in php 5.6.3 , it's allowed to use this expression:

    echo $json_decode->$data['_kfdTourDate']->available;
    

    so , you are trying to access the value of $data['_kfdTourDate'] which is element in $json_decode object

    for example: https://3v4l.org/i9Q7p


    in php 7,

    Indirect access to variables, properties, and methods will now be evaluated strictly in left-to-right order, as opposed to the previous mix of special cases. The table below shows how the order of evaluation has changed.

    so, the interpreter will interpret this code as follow:

    echo $json_decode->$data['_kfdTourDate']->available;
    // first , give me the value $json_decode->$data,
    // then choose the _kfdTourDate key
    

    and to solve this, you need to :

    echo $json_decode->{$data['_kfdTourDate']}->available;
    

    to tell php that $data['_kfdTourDate'] is just a value;