Php 多维数组
我有一个php多维数组,如下所示:Php 多维数组,php,loops,multidimensional-array,Php,Loops,Multidimensional Array,我有一个php多维数组,如下所示: $fields = array( array('input', 'title', 'slug', 'keywords'), array('textarea', 'content'), array('radio', 'active', 'active2', 'active3', 'active4', 'active5') ); 我正在访问阵列,就像这样 但是,由于某些数组包含的值比其他数组多,所以我遇到了问题,正如您在$type
$fields = array( array('input', 'title', 'slug', 'keywords'),
array('textarea', 'content'),
array('radio', 'active', 'active2', 'active3', 'active4', 'active5')
);
我正在访问阵列,就像这样
但是,由于某些数组包含的值比其他数组多,所以我遇到了问题,正如您在$type<2下面看到的那样……我如何解决这个问题
for($type = 0; $type < 2; $type++) {
for($field = 0; $field < 2; $field++) {
echo $fields[$type][$field];
}
}
($type=0;$type<2;$type++)的{
对于($field=0;$field<2;$field++){
回显$fields[$type][$field];
}
}
使用:
您可以使用:
为您提供数组中的项目数:
for($type = 0; $type < count($fields); $type++) {
for($field = 0; $field < count($fields[$type]); $field++) {
echo $fields[$type][$field];
}
}
($type=0;$type{
对于($field=0;$field
通常更易于使用,并且可以创建易于更改的代码。好的格式是你的朋友。此外,他可以使用count($array)
来确定$array
中有多少项,如果这是出于其他目的。为什么我没有想到在循环中循环??谢谢,现在都整理好了!:DAnother提示:您还可以在foreach循环中创建对值的引用。这在修改数组中的值时非常有用。示例:foreach($array as&$value){$value='something other';}
<?php
array_walk_recursive($fields, 'echo');
?>
for($type = 0; $type < count($fields); $type++) {
for($field = 0; $field < count($fields[$type]); $field++) {
echo $fields[$type][$field];
}
}