Php 我可以从多维数组中提取特定数据
我试图从多维数组中提取数据,但每次尝试写入正确的路径时都会出现此错误。我不知道是什么问题,因为路径是正确的 错误:Php 我可以从多维数组中提取特定数据,php,mysql,arrays,multidimensional-array,Php,Mysql,Arrays,Multidimensional Array,我试图从多维数组中提取数据,但每次尝试写入正确的路径时都会出现此错误。我不知道是什么问题,因为路径是正确的 错误: Notice: Undefined index: shipper in C:\xampp\htdocs\xml\dood.php on line 31 Notice: Undefined index: shipper in C:\xampp\htdocs\xml\dood.php on line 31 这是数组 Array ( [Shp] => Ar
Notice: Undefined index: shipper in C:\xampp\htdocs\xml\dood.php on line 31
Notice: Undefined index: shipper in C:\xampp\htdocs\xml\dood.php on line 31
这是数组
Array
(
[Shp] => Array
(
[test] => Array
(
[shipper] => Array
(
[customer] => Array
(
[address] => Array
(
[ad1] => new road
[ad2] => newyork
[company] => none
[city] => JO
)
[newlang] =>
)
)
[reciver] => Array
(
[customer] => Array
(
[address] => Array
(
[ad1] => new road
[ad2] => newyork
[company] => none
[city] => JO
)
[newlang] =>
)
)
[test] => Array
(
[shipper] => Array
(
[customer] => Array
(
[address] => Array
(
[ad1] => new road
[ad2] => newyork
[company] => none
[city] => JO
)
[newlang] =>
)
)
[reciver] => Array
(
[customer] => Array
(
[address] => Array
(
[ad1] => new road
[ad2] => newyork
[AddrLn3] => newyork
[company] => none
[city] => JO
)
[newlang] =>
)
)
)
)
)
)
php代码
$arr = $array; //Set this to your converted xml
$comps = $arr['Shp']['test'];
foreach($comps as $comp){
echo $comp['shipper']['customer']['address']['ad1'];
}
如何修复此错误
请帮助我,我尽了一切可能代码中有一个额外的键。移除它
$arr = $array; //Set this to your converted xml
$comps = $arr['Shp']['test'];
foreach($comps as $comp){
echo $comp['customer']['address']['ad1'];
}
如果您想添加特定于托运人的操作
foreach($comps as $key => $comp){
if ($key == 'shipper') {
echo $comp['customer']['address']['ad1'];
}
}
在您的阵列中有两组发货人和接收人。一个是外部的,另一个是内部的。见下文
Array
(
[Shp] => Array
(
[test] => Array
(
#####Top shipper array
[shipper] => Array
(
[customer] => Array
(
[address] => Array(.....)
[newlang] =>
)
)
#####Top reciver array
[reciver] => Array
(
[customer] => Array
(
[address] => Array(.....)
[newlang] =>
)
)
#####Sub array - test
[test] => Array
(
#####Sub shipper array
[shipper] => Array
(
[customer] => Array
(
[address] => Array(.....)
[newlang] =>
)
)
#####Sub reciver array
[reciver] => Array
(
[customer] => Array
(
[address] => Array(.....)
[newlang] =>
)
)
)
)
)
)
如果您想遍历内部数组,可以使用上面的代码
如果你想遍历内部数组,请使用下面的代码
$arr = $array; //Set this to your converted xml
$comps = $arr['Shp']['test']['test];
foreach($comps as $key => $comp){
if ($key == 'shipper') {
echo $comp['customer']['address']['ad1'];
}
}
或者,如果您希望遍历所有数据并在可用时仅打印数据
$arr = $array; //Set this to your converted xml
$comps = $arr['Shp']['test']['test];
foreach($comps as $key => $comp){
if (isset($comp['shipper'])) {
echo $comp['shipper']['customer']['address']['ad1'];
}
}
要修复错误,请确定$comp变量/数组中的内容。发生错误是因为您所要求的内容不存在
这是转换为arrayso的xml,所以我不能选择shipper,因为我还有一个接收者来区分这些值,因为它们具有相同的密钥名称!更新的应答器它工作得很好,但对于一个路径,我的意思是它不会在所有数组中循环,因为应该有2个ad1值,而不仅仅是一个查看更新的应答器!谢谢你的努力,我真的很感激。它现在可以完美地工作了。谢谢你,伙计。一个问题。你做程序员多久了?我看到前两个键没有出现在路径中,还有其他的吗?请告诉我,我很难说,因为我看不到你的结果。如果看不到前两个键,则表示您没有引用您认为的内容。请在主数组上尝试var_转储。从代码中,您希望所有内容都位于$arr=$array;所以在$arr上做一个var_转储,看看你得到了什么。如果你发现我的答案对解决你的问题有帮助,请检查我的答案