2016-02-12 34 views
0

我有此數組:如何獲取數組的值,但沒有指標

$order_list = array (array ("tangible", 1, 8, 1, 19000), 
         array ("tangible", 6, 2, 10, NULL), 
         array ("tangible", 1, 17, 1, 28000)); 

,我想組一起product_id ($order_list[2])基於vendor_id ($order_list[1]),我做到了。它看起來像這樣:

Array 
(
    [1] => Array 
     (
      [0] => Array 
       (
        [product_id] => 8 
        [pcs] => 1 
        [weight] => 115.00 
       ) 

      [1] => Array 
       (
        [product_id] => 17 
        [pcs] => 1 
        [weight] => 120.00 
       ) 

     ) 

    [6] => Array 
     (
      [0] => Array 
       (
        [product_id] => 2 
        [pcs] => 10 
        [weight] => 250.00 
       ) 

     ) 

) 

現在的問題是...

如何得到這個新的數組的值,而這個新的數組的索引包含vendor_id

我期待分別爲$new_array[0]$new_array[1]的結果爲1或6。所以我可以創建for循環。這仍然有可能嗎?以前感謝。

更新:我有這樣的代碼獲得的價值:

foreach ($order_array as $value) { 
    echo '<pre>'; 
    print_r($value); 
} 

但是,不幸的是我得到這個輸出結果:

Array 
(
    [0] => Array 
     (
      [product_id] => 8 
      [pcs] => 1 
      [weight] => 115.00 
     ) 

    [1] => Array 
     (
      [product_id] => 17 
      [pcs] => 1 
      [weight] => 120.00 
     ) 

) 
Array 
(
    [0] => Array 
     (
      [product_id] => 2 
      [pcs] => 10 
      [weight] => 250.00 
     ) 

) 

我仍然不能得到16 :-(

+0

哪裏是你的廠商ID? –

+0

嘗試使用foreach循環而不是循環 – Nikunj

+0

@yahoo:vendor_id($ order_list [1]),在這種情況下它的值爲1和6 –

回答

2

在foreach循環中添加關鍵字段:

$order_list = Array 
(
    1 => Array 
    (
     0 => Array 
     (
      'product_id' => 8, 
      'pcs' => 1, 
      'weight' => 115.00 
     ), 

     1 => Array 
     (
      'product_id' => 17, 
      'pcs' => 1, 
      'weight' => 120.00 
     ) 

    ), 

    6 => Array 
    (
     0 => Array 
     (
      'product_id' => 2, 
      'pcs' => 10, 
      'weight' => 250.00 
     ) 

    ) 

); 

foreach ($order_list as $vendor_id => $value) { 
    echo '<pre>'; 
    echo "Vendor Id: " . $vendor_id . '<br />'; 
    print_r($value); 
} 

輸出:

Vendor Id: 1 
Array 
(
    [0] => Array 
     (
      [product_id] => 8 
      [pcs] => 1 
      [weight] => 115 
     ) 

    [1] => Array 
     (
      [product_id] => 17 
      [pcs] => 1 
      [weight] => 120 
     ) 

) 
Vendor Id: 6 
Array 
(
    [0] => Array 
     (
      [product_id] => 2 
      [pcs] => 10 
      [weight] => 250 
     ) 

) 
+1

輝煌的解決方案。兄弟! –