Ahmadz Issa Ahmadz Issa - 1 month ago 6
MySQL Question

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

Ima Ima
Answer

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