我有类似的数据。此数组是数据库查询的结果,我想获得行和列的索引。我试图获取每一行的索引,但仍失败了。所以,谁能帮我吗?
查询结果
array(
(int) 0 => array(
'B' => array(
'company' => 'ABC'
),
'User' => array(
'company' => 'abc'
),
(int) 0 => array(
'date_part' => '3',
'jumlah' => null,
'jumbuy' => '50990',
'admin' => '50010'
),
(int) 1 => array(
'date_part' => '4',
'jumlah' => null,
'jumbuy' => '98990',
'admin' => '2010'
)
),
(int) 1 => array(
'B' => array(
'company' => 'BCD'
),
'User' => array(
'company' => 'bcd'
),
(int) 0 => array(
'date_part' => '3',
'jumlah' => null,
'jumbuy' => '65000',
'admin' => '5000'
),
(int) 1 => array(
'date_part' => '4',
'jumlah' => null,
'jumbuy' => '9000',
'admin' => '5000'
)
),
(int) 3 => array(
'B' => array(
'company' => 'CDE'
),
'User' => array(
'company' => 'cde'
),
(int) 0 => array(
'date_part' => '4',
'jumlah' => null,
'jumbuy' => '34566',
'admin' => '2010'
)
)
);
获取索引
for ($row = 0; $row < count($array); $row++) {
for($col = 0; $col < count(.....); $col++ ) {
echo "Baris [row] kolom [colum]"; // output row and column
}
}
以下代码将为您提供此给定数组的所有索引。
我在我的本地主机中检查了给定的数组。
,它为我们提供了此数组中的所有键和值。
尝试这个
<?php
foreach($array as $arr=>$value )
{
foreach($value as $ar=>$a)
{
echo $ar."<br>";
foreach($a as $res =>$r)
{
echo $res.": ";
echo $r;
echo "<br>";
}
}
}
?>
使用echo
ES键使用嵌套foreach
:
foreach ($array as $row => $v) {
foreach ($v as $col => $val) {
echo 'row: ' . $row . ', col: ' . $col . '<br>';
}
}
使用以下
foreach ($values as $inde => $value) {
foreach ($value as $key => $result) {
echo '['. $inde.'] ---' .$key . '<br>';
}
}
输出将为
[0] ---B
[0] ---User
[0] ---0
[0] ---1
[1] ---B
[1] ---User
[1] ---0
[1] ---1
[3] ---B
[3] ---User
[3] ---0