Forum Moderators: coopster

Message Too Old, No Replies

Break down multidimensional array

         

kieftrav

9:06 pm on Sep 19, 2006 (gmt 0)

10+ Year Member



Hey everyone. I have a multidimensional array and was wondering how it can be broken down... Here's the array.

Array
(
[0] => Array
(
[0] => Array
(
[0] => 1
[intsch_id] => 1
)

[1] => Array
(
[0] => 2
[intsch_id] => 2
)

)

)

And I want to break it down to

Array ( [0] => 1 [intsch_id] => 1, [1] => 2 [intsch_id] => 2) And this is in a dynamic query, so the results won't always be this simple. It will have the same structure as the above multidimensional array.

Thanks,
Travis

whoisgregg

10:15 pm on Sep 19, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm a bit confused about what you want to end up with. Is the result array missing two commas? One before each [intsch_id] key? Like this:
Array ( [0] => 1, [intsch_id] => 1, [1] => 2, [intsch_id] => 2)

If so, it won't be possible to have multiple array values with identical keys, [intsch_id].

If however, you just want to collapse the outer array that's holding this data, that's pretty straightforward:

$source_array = array(
array(
array(
"0" => 1,
"intsch_id" => 1
),
array(
"0" => 2,
"intsch_id" => 2
)
)
);
echo '<pre>'; print_r($source_array); echo '</pre>';
$return_array = $source_array[0];
echo '<pre>'; print_r($return_array); echo '</pre>';

kieftrav

10:41 pm on Sep 19, 2006 (gmt 0)

10+ Year Member



Hey whoisgregg, thanks for the help. Works great! Does it matter that it's a dynamic array?

whoisgregg

1:12 pm on Sep 20, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It shouldn't matter... I'd look at eliminating whatever part of the array generation that is adding the extra outer array. :)