Jump to content

[SOLVED] Appending to lower levels of multidimensional array


gordon142

Recommended Posts

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…

Link to comment
Share on other sites

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
)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 ) ) )

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.