在php中的多数组中使用for each
您好,我有下一个数组,我想打印每个id及其旁边的路径在php中的多数组中使用for each,php,arrays,arraylist,Php,Arrays,Arraylist,您好,我有下一个数组,我想打印每个id及其旁边的路径 $data = array('id' => array('1','2','3','4'), 'path' => array('pathto1', 'pathto2', 'pathto3', 'pathto4') ); 我使用foreach,但会出现错误 foreach ($data as $row){ echo $row['id']." ".$row['path']."
$data = array('id' => array('1','2','3','4'),
'path' => array('pathto1', 'pathto2', 'pathto3', 'pathto4')
);
我使用foreach,但会出现错误
foreach ($data as $row){
echo $row['id']." ".$row['path']."<br/>";
}
foreach($data作为$row){
echo$row['id']。“.$row['path']。”
”;
}
那你就可以做了
foreach($row AS $key=>$value){
echo $key.' '.$value.'<br>';
}
foreach($key=>$value的行){
回显$key.'.$value.'
';
}
那你就可以做了
foreach($row AS $key=>$value){
echo $key.' '.$value.'<br>';
}
foreach($key=>$value的行){
回显$key.'.$value.'
';
}
那你就可以做了
foreach($row AS $key=>$value){
echo $key.' '.$value.'<br>';
}
foreach($key=>$value的行){
回显$key.'.$value.'
';
}
那你就可以做了
foreach($row AS $key=>$value){
echo $key.' '.$value.'<br>';
}
foreach($key=>$value的行){
回显$key.'.$value.'
';
}
foreach
在所有元素上循环,将元素或其键和值作为局部变量提供给您:
就你而言:
foreach ($data as $row){
// $row first holds array('1', '2','3','4')
// in the second loop it holds array('pathto1', ..., 'pathto4')
}
如果您还需要钥匙,可以执行以下操作:
foreach ($data as $key=>$row){
// $row first holds array('1', '2','3','4'), $key holds "id"
// in the second loop $row holds array('pathto1', ..., 'pathto4')
// and $key holds "path"
}
要想得到你想要的结果,你需要另一种定义数组的方法(正如@Barmar所建议的),你的
foreach
会很好地工作。foreach
在所有元素上循环,给你元素或者它的键和值作为局部变量:
就你而言:
foreach ($data as $row){
// $row first holds array('1', '2','3','4')
// in the second loop it holds array('pathto1', ..., 'pathto4')
}
如果您还需要钥匙,可以执行以下操作:
foreach ($data as $key=>$row){
// $row first holds array('1', '2','3','4'), $key holds "id"
// in the second loop $row holds array('pathto1', ..., 'pathto4')
// and $key holds "path"
}
要想得到你想要的结果,你需要另一种定义数组的方法(正如@Barmar所建议的),你的
foreach
会很好地工作。foreach
在所有元素上循环,给你元素或者它的键和值作为局部变量:
就你而言:
foreach ($data as $row){
// $row first holds array('1', '2','3','4')
// in the second loop it holds array('pathto1', ..., 'pathto4')
}
如果您还需要钥匙,可以执行以下操作:
foreach ($data as $key=>$row){
// $row first holds array('1', '2','3','4'), $key holds "id"
// in the second loop $row holds array('pathto1', ..., 'pathto4')
// and $key holds "path"
}
要想得到你想要的结果,你需要另一种定义数组的方法(正如@Barmar所建议的),你的
foreach
会很好地工作。foreach
在所有元素上循环,给你元素或者它的键和值作为局部变量:
就你而言:
foreach ($data as $row){
// $row first holds array('1', '2','3','4')
// in the second loop it holds array('pathto1', ..., 'pathto4')
}
如果您还需要钥匙,可以执行以下操作:
foreach ($data as $key=>$row){
// $row first holds array('1', '2','3','4'), $key holds "id"
// in the second loop $row holds array('pathto1', ..., 'pathto4')
// and $key holds "path"
}
要想得到你想要的结果,你需要另一种定义数组的方法(正如@Barmar所建议的),你的
foreach
会很好地工作。你的数组填错了。应该是:
$data = array(array('id' => 1, path => 'pathto1'),
array('id' => 2, path => 'pathto2'),
array('id' => 3, path => 'pathto3'),
array('id' => 4, path => 'pathto4'),
);
有了这个,你的循环就可以工作了。要使用现有阵列,它将是:
foreach ($data['id'] as $index => $id) {
echo $id." ".$data['path'][$index]."<br/>";
}
foreach($data['id']as$index=>$id){
echo$id.“”.$data['path'][$index]。“
”;
}
我通常建议修复阵列结构。最好将所有相关数据保存在一个子数组中,而不是试图使并行数组保持同步。您的数组填错了。应该是:
$data = array(array('id' => 1, path => 'pathto1'),
array('id' => 2, path => 'pathto2'),
array('id' => 3, path => 'pathto3'),
array('id' => 4, path => 'pathto4'),
);
有了这个,你的循环就可以工作了。要使用现有阵列,它将是:
foreach ($data['id'] as $index => $id) {
echo $id." ".$data['path'][$index]."<br/>";
}
foreach($data['id']as$index=>$id){
echo$id.“”.$data['path'][$index]。“
”;
}
我通常建议修复阵列结构。最好将所有相关数据保存在一个子数组中,而不是试图使并行数组保持同步。您的数组填错了。应该是:
$data = array(array('id' => 1, path => 'pathto1'),
array('id' => 2, path => 'pathto2'),
array('id' => 3, path => 'pathto3'),
array('id' => 4, path => 'pathto4'),
);
有了这个,你的循环就可以工作了。要使用现有阵列,它将是:
foreach ($data['id'] as $index => $id) {
echo $id." ".$data['path'][$index]."<br/>";
}
foreach($data['id']as$index=>$id){
echo$id.“”.$data['path'][$index]。“
”;
}
我通常建议修复阵列结构。最好将所有相关数据保存在一个子数组中,而不是试图使并行数组保持同步。您的数组填错了。应该是:
$data = array(array('id' => 1, path => 'pathto1'),
array('id' => 2, path => 'pathto2'),
array('id' => 3, path => 'pathto3'),
array('id' => 4, path => 'pathto4'),
);
有了这个,你的循环就可以工作了。要使用现有阵列,它将是:
foreach ($data['id'] as $index => $id) {
echo $id." ".$data['path'][$index]."<br/>";
}
foreach($data['id']as$index=>$id){
echo$id.“”.$data['path'][$index]。“
”;
}
我通常建议修复阵列结构。最好将所有相关数据放在一个子数组中,而不是试图使并行数组保持同步。会出现什么错误?这一个未定义的索引:id未定义的索引:路径您试图获取什么?$行的值为
数组('1'、'2'、'3'、'4'))
不是id
为什么不使用数组键作为id。如果有$a=array(0,'pathto1','pathto2')
您可以使用$a[$id]
。会出现什么错误?这一个未定义的索引:id未定义的索引:path您试图获取什么?$row的值为数组('1','2','3','4')
而不是id
为什么不使用数组键作为id。如果您有$a=array(0,'pathto1','pathto2')
您可以使用$a[$id]
。会出现什么错误?这一个未定义的索引:id未定义的索引:path您试图获取什么?$row的值为数组('1','2','3','4')
而不是id
为什么不使用数组键作为id。如果您有$a=array(0,'pathto1','pathto2')
您可以使用$a[$id]
。会出现什么错误?这一个未定义的索引:id未定义的索引:path您试图获取什么?$row的值为数组('1','2','3','4')
而不是id
为什么不使用数组键作为id。如果您有$a=array(0,'pathto1','pathto2')
您可以使用$a[$id]
。您的第一个循环看起来是错误的$id=1
匹配$data['path'][0]
。很好的呼叫@Barmar,修复了它。如果id不连续怎么办?如果?你看到我的补丁了吗?;)我在修复之前写的。你的第一个循环看起来是错误的$id=1
匹配$data['path'][0]
。很好的呼叫@Barmar,修复了它。如果id不连续怎么办?如果?你看到我的补丁了吗?;)我在修复之前写的。你的第一个循环看起来是错误的$id=1
匹配$data['path'][0]
。很好的呼叫@Barmar,修复了它。如果id不连续怎么办?如果?你看到我的补丁了吗?;)我在修复之前写的。你的第一个循环看起来是错误的$id=1
匹配$data['path'][0]
。很好的呼叫@Barmar,修复了它。如果id不连续怎么办?如果?你看到我的补丁了吗?;)我在修复之前写的。