简体   繁体   中英

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

$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. The error occurs because what you are asking for is not there.

  1. Comment out your line 31

  2. Then echo the var_dump command. 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']; echo var_dump($comp); }

Look up the var_dump command in the php reference manual

  1. Once you can see what the content of the array is, you should have no trouble determining how to access the content.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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