PHP Access Multidimensional Array 然后插入值


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

我有一个这样的数组。我想像这样访问 Foreach 中的值。

第一个循环 => 1 | 文本 1 | 选中或空

第二个循环 => 2 | 文本 2 | 选中或空

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

结果:

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

这段代码很有用

$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";
}

输出:

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

最新更新