PHP如何回显3-Dim数组
After searching for an answer in this forum I found only related questions but under other context that would not apply to my case. Here's my problem:
I have a 3-dim array defined in a function like this:
$m_Array[h][$family][$iterator]
the values for
$family range from 6-10;
$iterator from 0-3 but has duplicates (0,1,2,3,1),
and the $m_Array results in values (25,26,30,31,33).
I am unable to echo the result using those indices to get these results once returned from the function. NOTE: I was able to echo when I had 2-dim $m_Array[h][$iterator] but could not use it because the last value for the iterator would replace the second in the array. Since I was able to echo the 2-dim, this is not a question on getting the return from the function or iterate over the indices. Thanks.
在此论坛中搜索答案后,我发现只有相关问题但在其他情况下不适用于我的案例 。 这是我的问题: p>
我在这样的函数中定义了一个3-dim数组: p>
$ m_Array [h] [$ family] [$ iterator]
code> pre>
p>
$ family的值范围为6-10;
$ 0来自0-3但有重复项(0,1,2,3,1),
和$ m_Array会产生值(25,26,30,31,33)。
code > pre>
我无法使用这些索引回显结果,一旦从函数返回就得到这些结果。
注意:当我有2-dim $ m_Array时,我能够回显[h ] [$ iterator]但是无法使用它,因为迭代器的最后一个值将替换数组中的第二个。
因为我能够回显2-dim,这不是从函数返回的问题或 迭代索引。
谢谢。 p>
div>
as others mentioned, you can use var_dump()
or print_r()
. If you need to access each item then you're going to need nested loops.
foreach($m_Array as $i => $h)
{
//echo $i, $key for h
foreach($h as $j => $family)
{
//echo $j, key for family
foreach($family as $k => $iterator)
{
echo $iterator;
}
}
}
Use print_r($arrayName)
to print an array. You cannot echo
an Array or Object
try this:
$keys = array_keys($h);
for($i = 0; $i < count($h); $i++) {
echo $keys[$i] . "{<br>";
foreach($h[$keys[$i]] as $key => $value) {
echo $key . " : " . $value . "<br>";
}
echo "}<br>";
}
It prints all values and keys