How to insert a 3d array to the beginning of another 3d array

Here is a 3d array:

Array (
 [0] => Array ( [item] => audcad [time] => 09-17 [acc_profit] => -3.23 )
 [1] => Array ( [item] => audusd [time] => 10-17 [acc_profit] => -3.29 )
 [2] => Array ( [item] => audnzd [time] => 10-17 [acc_profit] => 4.94 )
 [3] => Array ( [item] => audusd [time] => 10-17 [acc_profit] => -2.24 )
 [4] => Array ( [item] => audnzd [time] => 10-17 [acc_profit] => -3.26 )
 [5] => Array ( [item] => audcad [time] => 10-17 [acc_profit] => -1.74 ) 
..
..
 [43] => Array ( [item] => audusd [time] => 11-18 [acc_profit] => -1.57 )
 [44] => Array ( [item] => audusd [time] => 12-18 [acc_profit] => -1.76 )
)

I want to insert this array:

Array ( [item] => audcad [time] => 07-17 [acc_profit] => 0.00 )

to the beginning of the first one.

The second array was created in a “hybridization” of the [item] element of the first array with [time] and [acc_profit] of another 2d array.

Here is what I did:

//Get the 3D aray data for family line graph
$data_l = get_all_family_profit($coin);
//Get the data for all couples line graph to get deposit data which is the first item in array
//$items _l is a 2D array
$items_l = get_all_profit();
//Create a new 3D array and insert 
$deposit_date_primer = $items_l[0];
$deposit_date['item'] = $data_l[0]['item'];
$deposit_date['time'] = $deposit_date_primer['time'];
$deposit_date['acc_profit'] = $deposit_date_primer['profit'];

$data_l = array_unshift($data_l, $deposit_date);

something dousn;t work well because when I transform it to a JSON array using

<?php echo json_encode($data_l); ?> to use it in javascript the code using it doesn't work.

what do I do wrong?

No it’s not, you only have two coordinates.

and what should that mean for anybody? There’s no JS code in your post. And json_encode doesn’t change any dimension, so it’s nonsense to involve JS in the debugging of this process, just check why your array has an unexpected format

<?php

$all = [['what' => 'ever'], ['this' => 'is']];
$prepend = ['can' => 'be'];

array_unshift($all, $prepend);

print_r($all);```

As Chorn points out, this is a 2-d array.

Well you’ve already figured out how to do that; you use array_unshift. However, your problem is here:

array_unshift is an in-situ function, and it returns an integer. So your code inserts the record into the array, but then overwrites $data_l with the integer that gets returned.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.