简体   繁体   中英

PHP Access Multidimensional Array then insert values

$array = array(
  "id" => array("1","2","3"), 
  "text" => array("text 1","text 2", "text 3"), 
  "checked" => array("checked","","checked")
);

i have an array like this. i want to access value in foreach like this.

first loop => 1 | text 1 | checked or null

second loop => 2 | text 2 | checked or null

foreach($array['id'] as $key=>$value)
{

    echo $value . ' | ' . $array['text'][$key] . ' | ' . ($array['checked'][$key] == 'checked' ? 'checked' : 'null') . '<br />';

}

Result:

1 | text 1 | checked
2 | text 2 | null
3 | text 3 | checked

This code can be usefull

$array = array(
  "id" => array("1","2","3"), 
  "text" => array("text 1","text 2", "text 3"), 
  "checked" => array("checked","","checked")
);

// find the max count of multidimensional arrar
$count = max( array_map( 'count',  $array ) );
for($i=0;$i<$count;$i++){
    foreach($array as $key=>$value){
        echo $array[$key][$i]."|";

    }
     echo "\n";
}

OutPut:

1|text 1|checked|
2|text 2||
3|text 3|checked|

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