简体   繁体   中英

how can iterate inner array of 7 and 51 in php

How can I get the values of 51 and 7 through iteration.

Array
(
    [686] => Array
        (
            [51] => Array
                (
                    [0] => 1483691174
                    [1] => 1483691174
                )

            [7] => Array
                (
                    [0] => 1483691174
                    [1] => 1483691174
                    [2] => 1483691174
                    [3] => 1483691174
                    [4] => 1483691174
                    [5] => 1483691174
                    [6] => 1483691174
                    [7] => 1483691174
                    [8] => 1483691174
                    [9] => 1483691174
                    [10] => 1483691174
                    [11] => 1483691174
                    [12] => 1483691174
                    [13] => 1483691174
                    [14] => 1483691174
                )

        )
)

This function can be used to iterate any level of array with sub array:-

function iteratetor(){

static $cnt = 0;

$args = func_get_args();

if (!empty($args)) {
    echo '<ol>';
    foreach ($args as $k => $v) {
            if(in_array($k,array('7','51'))){
            $v = htmlspecialchars(print_r($v, true));
            if ($v == '') {
                $v = '    ';
                }
            echo '<li><pre>' . $v . "\n" . '</pre></li>';
        }
    }
    echo '</ol>';
}
$cnt++;}

just expand the statament in_array($k,array('7','51')) to suite your spec

you need to loop through the inner array and to get there you need arrays inside 686 . use foreach() to get this done.Try this: .

foreach($arr as $arrs){ 
    foreach($arrs as $key=>$val){
        echo "For:".$key."::::::::::<br>";
        foreach($val as $k=>$value){

            echo $k." =>".$value."<br>";
        }

    }
}

DEMO

Iterate it like this:

foreach($array[686] as $k => $v)
{
    echo $k."\n";
    var_dump($v);
}

You can try other way,

<?php
print_r(array_column($arr, 7));
print_r(array_column($arr, 51));
?>

You can check output here

array_column : Return the values from a single column in the input array

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM