我可以从多维数组中提取特定数据

时间:2016-10-26 11:58:59

标签: 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] => 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 code

$arr = $array; //Set this to your converted xml
$comps = $arr['Shp']['test'];

foreach($comps as $comp){
     echo $comp['shipper']['customer']['address']['ad1'];
}

如何解决此错误?

请帮助我尽一切努力

2 个答案:

答案 0 :(得分:2)

代码中有一个额外的键。删除它

$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'];
    } 
}

答案 1 :(得分:2)

要修复错误,请确定$ comp变量/数组中的内容。发生错误是因为您要求的不存在。

  1. 评论您的第31行

  2. 然后回显var_dump命令。这将显示数组中的实际内容。这将以可读格式显示您的数组内容。

    foreach($ comps as $ comp){    // echo $ comp ['shipper'] ['customer'] ['address'] ['ad1'];    echo var_dump($ comp); }

  3. php reference manual

    中查找var_dump命令
    1. 一旦你可以看到数组的内容是什么,你应该毫不费力地确定如何访问内容。