dataviews
dataviews

Reputation: 3100

Pushing an element in multidimensional array in PHP

so I am working with a multidimensional array.

For example, the output data I am trying to get is like this:

[[element 1],[element 2], [element 3]]

This is my PHP code (minus the entire prepared statement which goes above the bind ((not included as this is working fine)):

$insertquery->bind_result($tracking_type, $tracking_change_date, $vessel_fcm_new, $vessel_fcm_old, $vessel_hull_id_new, $vessel_hull_id_old, $vessel_name_new, $vessel_name_old, $vessel_length_new, $vessel_length_old, $vessel_manufacturer_new, $vessel_manufacturer_old, $vessel_manufacturer_id_new, $vessel_manufacturer_id_old, $vessel_year_new, $vessel_year_old, $vessel_value_new, $vessel_value_old, $owner_id_new, $owner_id_old, $loss_payee_id_new, $loss_payee_id_old, $policy_id_new, $policy_id_old, $policy_start_date_new, $policy_start_date_old, $policy_end_date_new, $policy_end_date_old, $vessel_fcm, $vessel_hull_id, $vessel_name, $vessel_manufacturer_id);
while ($insertquery->fetch()){
    if($vessel_fcm_new != $vessel_fcm_old){
        $data = array($vessel_fcm_new, $vessel_fcm_old);
    }
    if ($vessel_name_new != $vessel_name_old){
         array_push($data, $vessel_name_new, $vessel_name_old);
    }
    $data_return[] = $data;
}
echo json_encode($data_return);

Basically, the code is initiated to iterate through each database row, and if the condition is met it will build an array, and add the array to the array object. So the outcome would look like this, if the matching conditions are met:

[[row 1], [row2], [row3]]

However, I am getting this error:

Warning: array_push() expects parameter 1 to be array, null given in C:\htdocs\alterajax.php on line 16

But I am specifying the array already, or at least I think I am ($data). This is also what I see as the output:

[null,null,null,null,null,["FCMjgis","fFH465","Smokey","GIIGE"]]

I'm sure this is just something minor, but I would appreciate some guidance if you can assist. Thank you in advance!

Upvotes: 1

Views: 75

Answers (2)

Geolim4
Geolim4

Reputation: 454

Like I said, just initialize your variable or test if its initialized using isset

$insertquery->bind_result($tracking_type, $tracking_change_date, $vessel_fcm_new, $vessel_fcm_old, $vessel_hull_id_new, $vessel_hull_id_old, $vessel_name_new, $vessel_name_old, $vessel_length_new, $vessel_length_old, $vessel_manufacturer_new, $vessel_manufacturer_old, $vessel_manufacturer_id_new, $vessel_manufacturer_id_old, $vessel_year_new, $vessel_year_old, $vessel_value_new, $vessel_value_old, $owner_id_new, $owner_id_old, $loss_payee_id_new, $loss_payee_id_old, $policy_id_new, $policy_id_old, $policy_start_date_new, $policy_start_date_old, $policy_end_date_new, $policy_end_date_old, $vessel_fcm, $vessel_hull_id, $vessel_name, $vessel_manufacturer_id);
while ($insertquery->fetch()){
    if($vessel_fcm_new != $vessel_fcm_old){
        $data = array($vessel_fcm_new, $vessel_fcm_old);
    }
    if (isset($data) && $vessel_name_new != $vessel_name_old){
         array_push($data, $vessel_name_new, $vessel_name_old);
         $data_return[] = $data;
    }
}
echo json_encode($data_return);

Upvotes: 1

dataviews
dataviews

Reputation: 3100

Make sure to initialize your variables!

In this case, all I needed to do was initialize the $data array. I did this by placing $data = []; (or also $data = array();) on the line above the while loop

Upvotes: 0

Related Questions