我刚刚了解了键/值对。
我尝试寻找现有的答案,并尝试了解有关键/值对和关联数组的知识。尽管这有点太耗时了。
我很难弄清楚如何迭代这个多维关联数组而不出现任何错误。
$arr = array(
'test1' => array(
'testing 1-1' => array('testing 1-1-1', 'testing 1-1-2', 'testing 1-1-3'),
'testing 1-2' => array('testing 1-2-1', 'testing 1-2-2', 'testing 1-2-3'),
'testing 1-3' => array('testing 1-3-1', 'testing 1-3-2', 'testing 1-3-3')),
'test2' => array(
'testing 2-1' => array('testing 2-1-1', 'testing 2-1-2', 'testing 2-1-3'),
'testing 2-2' => array('testing 2-2-1', 'testing 2-2-2', 'testing 2-2-3'),
'testing 2-3' => array('testing 2-3-1', 'testing 2-3-2', 'testing 2-3-3')),
'test3' => array(
'testing 3-1' => array('testing 3-1-1', 'testing 3-1-2', 'testing 3-1-3'),
'testing 3-2' => array('testing 3-2-1', 'testing 3-2-2', 'testing 3-2-3'),
'testing 3-3' => array('testing 3-3-1', 'testing 3-3-2', 'testing 3-3-3')));
所以我知道 print_r 完美地完成了渲染整个数组的工作,但我想要实现的是能够获取每个可能的键和值。
我尝试通过 foreach 循环一一回显所有字符串,但似乎单词数组本身会以某种方式导致错误。我相信这是我最合乎逻辑的方法。
foreach ($arr as $test) {
echo $test . '<br>';
foreach ($test as $testing1) {
echo '  ' . $testing1 . '<br>';
foreach ($testing1 as $testing2) {
echo '    ' . $testing2 . '<br>';
}
}
}
虽然它会导致以下结果:
Notice: Array to string conversion in /opt/lampp/htdocs/projects/form/phptest.php on line 88
Array
Notice: Array to string conversion in /opt/lampp/htdocs/projects/form/phptest.php on line 90
Array
testing 1-1-1
testing 1-1-2
testing 1-1-3
Notice: Array to string conversion in /opt/lampp/htdocs/projects/form/phptest.php on line 90
Array
testing 1-2-1
testing 1-2-2
testing 1-2-3
Notice: Array to string conversion in /opt/lampp/htdocs/projects/form/phptest.php on line 90
Array
testing 1-3-1
testing 1-3-2
testing 1-3-3
Notice: Array to string conversion in /opt/lampp/htdocs/projects/form/phptest.php on line 88
Array
Notice: Array to string conversion in /opt/lampp/htdocs/projects/form/phptest.php on line 90
Array
testing 2-1-1
testing 2-1-2
testing 2-1-3
Notice: Array to string conversion in /opt/lampp/htdocs/projects/form/phptest.php on line 90
Array
testing 2-2-1
testing 2-2-2
testing 2-2-3
Notice: Array to string conversion in /opt/lampp/htdocs/projects/form/phptest.php on line 90
Array
testing 2-3-1
testing 2-3-2
testing 2-3-3
Notice: Array to string conversion in /opt/lampp/htdocs/projects/form/phptest.php on line 88
Array
Notice: Array to string conversion in /opt/lampp/htdocs/projects/form/phptest.php on line 90
Array
testing 3-1-1
testing 3-1-2
testing 3-1-3
Notice: Array to string conversion in /opt/lampp/htdocs/projects/form/phptest.php on line 90
Array
testing 3-2-1
testing 3-2-2
testing 3-2-3
Notice: Array to string conversion in /opt/lampp/htdocs/projects/form/phptest.php on line 90
Array
testing 3-3-1
testing 3-3-2
testing 3-3-3
所以我想知道是否有我遗漏的东西或者这是不可能实现的?
更新:感谢您的回答。
这是我根据得到的答案使用的代码:
foreach ($arr as $k => $test) {
echo $k . '<br>';
foreach ($test as $k1 => $testing1) {
echo '  ' . $k1 . '<br>';
foreach ($testing1 as $k2 => $testing2) {
echo '    ' . $testing2 . '<br>';
}
}
}
UPDATE:
我真的很喜欢 HoldOffHunger 的建议。
<?php
// Your Array Here
$arr = [...];
// IterateArray() function
function IterateArray($data) {
if(is_array($data)) {
foreach ($data as $data_key => $data_value) {
print("\n\n");
print($data_key . ' :: ');
print(IterateArray($data_value));
}
} else {
print($data);
print("\n");
}
}
// Call IterateArray()
IterateArray($arr);
?>
不幸的是,我没有使用这个递归函数。对循环的每个实例进行硬编码可以让我为每个嵌套循环执行不同的操作。不过(如果我错了,请纠正我)如果多维数组的每个维度都重复相同的代码,这将很有用。