简体   繁体   English

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

[英]I can I pull a specific data from multidimensional array

I'm trying to pull out the data from multidimensional array, but I get this error each time I'm trying to write the correct path. 我试图从多维数组中提取数据,但每次尝试编写正确的路径时都会遇到此错误。 I don't know what's the problem since the path is correct. 由于路径正确,我不知道出了什么问题。

The error: 错误:

   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

This is the array 这是数组

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 PHP代码

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

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

How do I fix this error ? 如何解决此错误?

Please help I tried everything possible 请帮我尽一切可能

You have an extra key in the code. 您在代码中有一个额外的密钥。 Remove it 去掉它

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

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

And if you want to add actions specific to shipper 如果您要添加特定于托运人的操作

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

In your array there is two set of shipper and reciver. 在您的阵列中,有两组托运人和收货人。 One is outer and another is inner. 一个是外部,另一个是内部。 See below 见下文

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] => 
                                        )
                                )
                        )
                )
        )
)

If you want to iterate through inner array you can use above code. 如果要遍历内部数组,可以使用上面的代码。

If u wanna iterate through inner array use code below 如果您想遍历内部数组,请使用以下代码

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

Or if you wanna iterate through all and print only data when available 或者,如果您想遍历所有内容并仅在可用时打印数据

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

To fix the error, determine what is in your $comp variable/array. 要解决该错误,请确定$ comp变量/数组中的内容。 The error occurs because what you are asking for is not there. 发生错误是因为您所要求的不存在。

  1. Comment out your line 31 注释掉您的第31行

  2. Then echo the var_dump command. 然后回显var_dump命令。 This will show you what is actually in your array. 这将向您显示阵列中实际存在的内容。 This will display your array content in a readable format. 这将以可读格式显示您的阵列内容。

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

Look up the var_dump command in the php reference manual php参考手册中查找var_dump命令

  1. Once you can see what the content of the array is, you should have no trouble determining how to access the content. 一旦看到阵列的内容,就可以轻松确定如何访问该内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM