Senaxx Senaxx - 1 month ago 5
PHP Question

Loop over an indexed array

Array
(
[2] => Array
(
[12] => Array
(
[0] => Array
(
[id] => 24339
[active] => 1
[lang] => en
[link] => http://vk.com/video_ext.php?oid=203859683&id=165118953&hash=545584bd3253fab9&hd=1
)

[1] => Array
(
[id] => 24483
[active] => 1
[lang] => ru
[link] => http://vk.com/video_ext.php?oid=203859108&id=164809310&hash=5544f9e52b6ebc10&hd=1
)

)

[11] => Array
(
[0] => Array
(
[id] => 24016
[active] => 1
[lang] => en
[link] => http://vk.com/video_ext.php?oid=203859683&id=165118860&hash=088323c81fb1f360&hd=1
)

[1] => Array
(
[id] => 24115
[active] => 1
[lang] => ru
[link] => http://vk.com/video_ext.php?oid=203859108&id=164809379&hash=dd49270049f5a632&hd=1
)

)

[10] => Array
(
[0] => Array
(
[id] => 23463
[active] => 1
[lang] => en
[link] => http://vk.com/video_ext.php?oid=203859683&id=165118763&hash=f328401f5bd7f2e4&hd=1
)

[1] => Array
(
[id] => 23656
[active] => 1
[lang] => ru
[link] => http://vk.com/video_ext.php?oid=203859108&id=164809462&hash=7a61a9ac444e2b95&hd=1
)

)

[9] => Array
(
[0] => Array
(
[id] => 21815
[active] => 1
[lang] => en
[link] => http://vk.com/video_ext.php?oid=203859683&id=165118684&hash=3bbddfcc12426510&hd=1
)

[1] => Array
(
[id] => 22406
[active] => 1
[lang] => ru
[link] => http://vk.com/video_ext.php?oid=203859108&id=164809548&hash=cd0e7f43171569a6&hd=1
)

)

[8] => Array
(
[0] => Array
(
[id] => 20443
[active] => 1
[lang] => en
[link] => http://vk.com/video_ext.php?oid=203859683&id=165118608&hash=26c0f730bc1dd6c3&hd=1
)

[1] => Array
(
[id] => 20921
[active] => 1
[lang] => ru
[link] => http://vk.com/video_ext.php?oid=203859108&id=164809649&hash=47c524f47946fb96&hd=1
)

)

[7] => Array
(
[0] => Array
(
[id] => 19812
[active] => 1
[lang] => en
[link] => http://vk.com/video_ext.php?oid=203859683&id=165118510&hash=1243ca95114cbdf2&hd=1
)

[1] => Array
(
[id] => 20057
[active] => 1
[lang] => ru
[link] => http://vk.com/video_ext.php?oid=203859108&id=164809741&hash=266c635751d2f9d5&hd=1
)

)

[6] => Array
(
[0] => Array
(
[id] => 19262
[active] => 1
[lang] => en
[link] => http://vk.com/video_ext.php?oid=203859683&id=165118374&hash=e947746533f9d895&hd=1
)

[1] => Array
(
[id] => 19392
[active] => 1
[lang] => ru
[link] => http://vk.com/video_ext.php?oid=203859108&id=164809836&hash=e58901e3e2f32993&hd=1
)

)

[5] => Array
(
[0] => Array
(
[id] => 18217
[active] => 1
[lang] => en
[link] => http://vk.com/video_ext.php?oid=203859683&id=165118222&hash=3a236df89bcfaac5&hd=1
)

[1] => Array
(
[id] => 18417
[active] => 1
[lang] => ru
[link] => http://vk.com/video_ext.php?oid=203859108&id=164809927&hash=d8a72be4d4eb7d86&hd=1
)

)

[4] => Array
(
[0] => Array
(
[id] => 17231
[active] => 1
[lang] => en
[link] => http://vk.com/video_ext.php?oid=203859683&id=165118092&hash=13e854824bde17ab&hd=1
)

[1] => Array
(
[id] => 17603
[active] => 1
[lang] => ru
[link] => http://vk.com/video_ext.php?oid=203859108&id=164810038&hash=9d7b6a4336dd2b98&hd=1
)

)

[3] => Array
(
[0] => Array
(
[id] => 15838
[active] => 1
[lang] => en
[link] => http://vk.com/video_ext.php?oid=203859683&id=165117998&hash=031a9c6d70058e4f&hd=1
)

[1] => Array
(
[id] => 16465
[active] => 1
[lang] => ru
[link] => http://vk.com/video_ext.php?oid=203859108&id=164810119&hash=06985923604c61ca&hd=1
)

)

[2] => Array
(
[0] => Array
(
[id] => 15070
[active] => 1
[lang] => en
[link] => http://vk.com/video_ext.php?oid=203859683&id=165117903&hash=b1a21a9021b74aa2&hd=1
)

[1] => Array
(
[id] => 15284
[active] => 1
[lang] => ru
[link] => http://vk.com/video_ext.php?oid=203859108&id=164810199&hash=3b818c6ef7c7488b&hd=1
)

)

[1] => Array
(
[0] => Array
(
[id] => 13663
[active] => 1
[lang] => en
[link] => http://vk.com/video_ext.php?oid=203859683&id=165117810&hash=62f01fa6c53a195f&hd=1
)

[1] => Array
(
[id] => 14000
[active] => 1
[lang] => ru
[link] => http://vk.com/video_ext.php?oid=203859108&id=164810267&hash=60074b4f93d62429&hd=1
)

)1

)

)


I have the following that i have sliced from a much bigger array. Now i want to loop trough this array and filter some value's. I've used the sliced_array feature with the true option to preserve the [2] array number.

Now this array has some several sub arrays and i need some value's and some filtering. In the end result I want to create a table that lists the array numbers (1 to 12) And have the link behind it. (so a 2 column table) What i don't need is the 2nd array from the episode number. this is always the [1] array from a episode. This is the "russian" part and i don't need that in my end results.

How can i loop over this and get a simple html table as output? I've been trying with the foreach() method but for now all i get back is "Array".

edit
the answer was indeed to do multiple foreach.

<?php
$json = file_get_contents("http://trendico.ru/api/moviebox/get_tv_data?id=1&season=7");
$array = json_decode($json, TRUE);


//Create an index array
$indexarray = (array_values($array));

//Create an array with only the episodes
$episodes = array_slice($indexarray, 2, 1);



foreach($array as $arrayl1) {
foreach($arrayl1 as $arrayl2) {
foreach($arrayl2 as $arrayl3) {
foreach($arrayl3 as $key => $val) {
echo "$key=>$val"."<br>";
}
}
}
}

?>

Answer

Use recursive function and is_array.

function doSomething(&$complex_array){
foreach ($complex_array as $n => $v)
{
    if (is_array($v))
        doSomething($v);
    else
        do whatever you want to do with a single node
}}
Comments