2016-10-26 80 views
-1

我試圖從多維數組中提取數據,但每次嘗試寫入正確的路徑時都會收到此錯誤。由於路徑正確,我不知道有什麼問題。我可以從多維數組中提取特定數據

錯誤:

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

如何解決這個問題?

請幫我試過一切可能的東西

+0

此的XML轉換爲數組 –

回答

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

在你的陣列有兩套託運人和reciver的。一個是外在的,另一個是內在的。見下面

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

,所以我不能選擇發貨人,因爲我有一個接收器,以及值之間進行區分,因爲它們具有相同的鍵名 –

+0

見!更新回答 – Ima

+0

它工作的很好,但是對於一條路徑我的意思是它不會在所有數組中循環,因爲應該有2個ad1值不只一個 –

2

要修正這個錯誤,確定是什麼你的$ comp變量/數組。發生錯誤是因爲你所要求的不在那裏。

  1. 評論了您的線路31

  2. 然後呼應的var_dump命令。這會告訴你你陣列中的實際內容。這將以可讀格式顯示您的數組內容。

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

在中查找的var_dump命令php reference manual

  • 一旦你可以看到數組的內容是什麼,你應該怎麼沒有煩惱確定訪問內容。
  • +0

    我看到前兩個鍵沒有出現在路徑中,請問有什麼其他請告訴我 –

    +0

    我很難說,因爲我沒有看到你的結果。如果你沒有看到前兩個鍵,那麼你沒有參考你的想法。 – Claus

    +0

    在你的主數組上嘗試var_dump。 – Claus