本文介绍了通过PHP多维数组的每个键和值循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下数组:
<pre>Array
(
[student] => Array
(
[admission_no] => Array
(
[isEmpty] => Please enter Admission No
)
[admission_date] => Array
(
[isEmpty] => Please enter Admission Date
)
)
[student_personal_details] => Array
(
[first_name] => Array
(
[isEmpty] => Please enter First Name
)
[gender] => Array
(
[isEmpty] => Please select Gender
)
[birth_date] => Array
(
[isEmpty] => Please enter Birth Date
)
)
[student_gardian_details] => Array
(
[first_name] => Array
(
[isEmpty] => Please enter Gardian First Name
)
)
[student_education_details] => Array
(
[roll_no] => Array
(
[isEmpty] => Please enter Pin Code
)
)
)
我想用while循环得到这个数组中的所有字符串。我的意思是不是一个数组本身整个阵列的钥匙。
I want to get all the strings in this array using while loop. I mean keys of the whole array that are not an array itself.
我尝试以下code:
private function getInternalErorrString($array) {
while (list($var, $val) = each($array)) {
if(is_array($var)) {
$this->getInternalErorrString($var);
} else {
return $val;
}
}
}
我能有什么更好的主意?
Can I have any better idea?
推荐答案
另外,你可以使用SPL RecursiveArrayIterator
来获取值。考虑这个例子:
Alternatively, you could use SPL RecursiveArrayIterator
to get the values. Consider this example:
$values = array( 'student' => array( 'admission_no' => array('isEmpty' => 'Please enter Admission No'), 'admission_date' => array('isEmpty' => 'Please enter Admission Date'), ), 'student_personal_details' => array( 'first_name' => array('isEmpty' => 'Please enter First Name'), 'gender' => array('isEmpty' => 'Please enter Gender'), 'birth_date' => array('isEmpty' => 'Please enter Birth Date'), ), 'student_gardian_details' => array( 'first_name' => array('isEmpty' => 'Please enter Gardian First Name'), ), 'student_education_details' => array( 'roll_no' => array('isEmpty' => 'Please enter Pin Code'), ),);
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($values));
foreach($iterator as $key => $value) {
$data[] = $value;
}
示例输出:
Array
(
[0] => Please enter Admission No
[1] => Please enter Admission Date
[2] => Please enter First Name
[3] => Please enter Gender
[4] => Please enter Birth Date
[5] => Please enter Gardian First Name
[6] => Please enter Pin Code
)
这篇关于通过PHP多维数组的每个键和值循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!