我刚刚了解了键/值对。
我试图寻找一个现有的答案,并试图理解我所能理解的关于键/值对和关联数组的内容。尽管这有点太耗时了。
我很难弄清楚如何迭代这个多维关联数组而不会得到任何错误。
$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循环逐个回显所有字符串,但似乎单词array本身以某种方式导致错误。我相信这是我最合乎逻辑的方法。
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>';
}
}
}
更新:
我真的很喜欢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);
?>
不幸的是,我没有使用这个递归函数。对循环的每个实例进行硬编码可以让我为每个嵌套循环做不同的事情。不过(如果我错了,请纠正我),如果多维数组的每个维度都重复相同的代码,这将很有用。
发布于 2018-09-26 07:42:59
The foreach
statement可以像您所做的那样,只迭代遍历这些值,或者遍历键和值。我怀疑这就是你要找的:
foreach ($arr6 as $k=>$test) {
echo $k . '<br>';
foreach ($test as $k1=>$testing1) {
echo '  ' . $k1 . '<br>';
foreach ($testing1 as $k2=>$testing2) {
echo '    ' . $testing2 . '<br>';
}
}
}
您收到的通知是因为您试图在数组上使用echo
,而这是不可能的。
发布于 2018-09-26 07:43:39
欢迎来到stackoverflow。这些都是注意事项,您不应该对数组使用echo,而应使用print_r($ar)或var_dump($ar)
foreach ($arr6 as $test) {
print_r($test);
foreach ($test as $testing1) {
print_r($testing1);
foreach ($testing1 as $testing2) {
print_r($testing2);
}
}
}
您还可以像这样打印密钥:
foreach ($arr6 as $mykey => $test) {
echo $mykey;
print_r($test);
发布于 2018-09-26 07:45:18
$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')));
foreach ($arr as $key=>$val){
echo $key;
foreach($val as $k=>$v){
echo $k;
foreach($v as $ku=>$vu){
echo $ku;
}
}
}
您正在回显数组而不是字符串。
https://stackoverflow.com/questions/52507975
复制相似问题