gordon142 Posted September 8, 2009 Share Posted September 8, 2009 I'm sure there must be a simple solution, but I've scoured the net and come up blank. I have a three-dimensional array. I want to append a new array to the third level. In other words, it would be: array > array > array array array new_array_here If I simply do: $3darray = array('value1'); it appends it as a new array on the second level of 3darray, but I can't find any way to append it further down than that… Quote Link to comment https://forums.phpfreaks.com/topic/173498-solved-appending-to-lower-levels-of-multidimensional-array/ Share on other sites More sharing options...
dave_sticky Posted September 8, 2009 Share Posted September 8, 2009 Would this do the job? $array = array(array('value1')); [EDIT] Correction. That Won't. This will: // Level 1 $array = array('array1','array2'); // Level 2 $array[0] = array('value1','value2'); // Level 3 $array[0][0] = array('sub_value1'); Gives you: Array ( [0] => Array ( [0] => Array ( [0] => sub_value1 ) [1] => value2 ) [1] => array2 ) Quote Link to comment https://forums.phpfreaks.com/topic/173498-solved-appending-to-lower-levels-of-multidimensional-array/#findComment-914535 Share on other sites More sharing options...
gordon142 Posted September 8, 2009 Author Share Posted September 8, 2009 OK, that is almost perfect, except that that code you posted seems to create a NEW array on the second level, then another array within that one. Reading back I did not actually make this clear in my initial posting, but what I want to do is to create a new third-level array within an existing second-level array: existing_array > existing_array > existing_array new_array_here The above tree is now an exact representation of my arrays and what I want to do with them. You might ask why I would need a third-level array at all if I have only a single second-level array. The reason is that after the array structure is properly created, the entire thing is going to be run through json_encode. For the json to parse properly for my uses, I need the three levels of arrays. Many thanks. Quote Link to comment https://forums.phpfreaks.com/topic/173498-solved-appending-to-lower-levels-of-multidimensional-array/#findComment-914880 Share on other sites More sharing options...
mikesta707 Posted September 8, 2009 Share Posted September 8, 2009 this seems easy enough, but without seeing your actual array I can only give an educated guess $array;//this is your full array $array[0];//this is the second level of your array $array[0][0];//this is the third level of your array to add a new entry to the third level just do $array[0][0][] = $new_array; //where new_array is the array that is being pushed into the 3rd level of your array is that what you're looking for? Quote Link to comment https://forums.phpfreaks.com/topic/173498-solved-appending-to-lower-levels-of-multidimensional-array/#findComment-914893 Share on other sites More sharing options...
PrinceOfDragons Posted September 8, 2009 Share Posted September 8, 2009 You can always use the next() function it will get the next level of the array for you. $test = next($yourarray) echo $test[0]; echo $test[1]; Quote Link to comment https://forums.phpfreaks.com/topic/173498-solved-appending-to-lower-levels-of-multidimensional-array/#findComment-915063 Share on other sites More sharing options...
gordon142 Posted September 9, 2009 Author Share Posted September 9, 2009 Still not getting it to work. Here is the code I'm running (the variable values stripped from the script, as they add nothing to this example): <?php $final_output = array( 'Departure Locations' => Array ( '0' => Array ( 'name' => $stop_location, 'time1' => $next_arrival, 'direction1' => $bus_direction, 'time2' => $second_arrival, 'direction2' => $bus_return, 'weekendservice' => $weekend_service ) ) ); $final_output[0][0] = array( 'name' => $stop_location, 'time1' => $next_arrival, 'direction1' => $bus_direction, 'time2' => $second_arrival, ' direction2' => $bus_return, 'weekendservice' => $weekend_service ); print_r($final_output); ?> Here is what it returns: Array ( [Departure Locations] => Array ( [0] => Array ( [name] => Templeton [time1] 6: 00 PM => [direction1] => null [time2] 7: 04 PM => [direction2] => null [weekendservice] => true ) ) [0] => Array ( [0] => Array ( [name] => Law School [time1] => 5: 43 PM [direction1] => To Lewis and Clark [time2] => 6: 05 PM [ direction2] => To Pioneer Square [weekendservice] => true ) ) ) Here is what it SHOULD return: Array ( [Departure Locations] => Array ( [0] => Array ( [name] => Templeton [time1] => 6: 00 PM [direction1] => null [time2] => 7: 04 PM [direction2] => null [weekendservice] => true ) [1] => Array ( [name] => Law School [time1] => 5: 43 PM [direction1] => To Lewis and Clark [time2] => 6: 05 PM [direction2] => To Pioneer Square [weekendservice] => true ) ) ) Quote Link to comment https://forums.phpfreaks.com/topic/173498-solved-appending-to-lower-levels-of-multidimensional-array/#findComment-915237 Share on other sites More sharing options...
Garethp Posted September 9, 2009 Share Posted September 9, 2009 Use $final_output[0][] instead of $final_output[0][0] Quote Link to comment https://forums.phpfreaks.com/topic/173498-solved-appending-to-lower-levels-of-multidimensional-array/#findComment-915244 Share on other sites More sharing options...
gordon142 Posted September 9, 2009 Author Share Posted September 9, 2009 Use $final_output[0][] instead of $final_output[0][0] Unfortunately, in this instance that produces output completely identical to the output of $final_output[0][0]. Quote Link to comment https://forums.phpfreaks.com/topic/173498-solved-appending-to-lower-levels-of-multidimensional-array/#findComment-915492 Share on other sites More sharing options...
gordon142 Posted September 9, 2009 Author Share Posted September 9, 2009 I believe I've figured it out. Never could get stuff to properly append at the third level, but I can achieve the same final product by creating the arrays separating, the stacking them one at a time, like so: <?php $levelone = array(); $leveltwo = array(array( 'name' => $stop_location, 'time1' => $next_arrival, 'direction1' => $bus_direction, 'time2' => $second_arrival, 'direction2' => $bus_return, 'weekendservice' => $weekend_service )); $leveltwo[] = array( 'name' => $stop_location, 'time1' => $next_arrival, 'direction1' => $bus_direction, 'time2' => $second_arrival, 'direction2' => $bus_return, 'weekendservice' => $weekend_service ); ##print_r($leveltwo); $levelone['Departure Locations'] = $leveltwo; print_r($levelone); ?> Which outputs: Array ( [Departure Locations] => Array ( [0] => Array ( [name] => [time1] => [direction1] => [time2] => [direction2] => [weekendservice] => ) [1] => Array ( [name] => [time1] => [direction1] => [time2] => [direction2] => [weekendservice] => ) ) ) Which is what I want. Cool. Quote Link to comment https://forums.phpfreaks.com/topic/173498-solved-appending-to-lower-levels-of-multidimensional-array/#findComment-915519 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.