我有一个数组,如何获取此数组的值
Array
(
[0] => Array
(
[0] => raj
[1] => 1234
[2] => gov
)
[1] => Array
(
[0] => some
[1] => 1234
[2] => prv
)
[2] => Array
(
[0] => lal
[1] => 234
[2] => prv
)
)
我想要这种类型的值:raj,1234,gov
答案 0 :(得分:1)
for($i=0; $i<count($data); $i++){
echo 'type: ' . $data[$i][0] . ', ' . $data[$i][1] . ', ' . $data[$i][2] . PHP_EOL;
}
这是输出它的一种方法,结果应该是:
type: raj, 1234, gov
type: some, 1234, prv
type: lal, 234, prv
答案 1 :(得分:1)
foreach ($aArray as $aValues) {
echo "type: ".implode(", ", $aValues);
echo "<br>"; // Or alternative linebreak.
}
答案 2 :(得分:1)
简短回答:
如果上面的数组存储在变量$foo
中,那么获取第一个嵌套数组就像$foo[0]
一样简单(第二个是$foo[1]
。第三个是$foo[2]
,等等)。然后,要获取其中的值,可以使用相同的方法(括号)引用嵌套值。
$foo = /* that array */
$foo[0] // this is the first array, array([0] => 'raj', [1] => 1234, [2] => 'gov');
$foo[0][0] // this is "raj" (reference element one of the first array)
echo implode(', ',$foo[0])); // use implode and make all element of "$foo[0]"
// display, separated by commas and a space.
<强> demo 强>