2015-11-02 116 views
1

場景:如果匹配,則用PHP中另一個數組的值替換數組中的值。多維數組

我有這2個數組

array1

Array 
(
[1] => Array 
    (
     [label] => pending 
     [fillColor] => #468847 
     [data] => 50 
    ) 

[2] => Array 
    (
     [label] => dispatched 
     [fillColor] => #6ecf70 
     [data] => 10 
    ) 

[3] => Array 
    (
     [label] => delivered 
     [fillColor] => #f89406 
     [data] => 1 
    ) 

[4] => Array 
    (
     [label] => invoiced 
     [fillColor] => #3a87ad 
     [data] => 2 
    ) 

) 

array2

Array 
(
[1] => Array 
    (
     [label] => pending 
     [fillColor] => #468847 
     [data] => Array 
      (
       [0] => 1 
      ) 

    ) 

) 

我需要的結果是

Array 
(
[1] => Array 
    (
     [label] => pending 
     [fillColor] => #468847 
     [data] => Array 
      (
       [0] => 50 
       [1] => 1 
      ) 
    ) 

[2] => Array 
    (
     [label] => dispatched 
     [fillColor] => #6ecf70 
     [data] => Array 
      (
       [0] => 10 
       [1] => 0 
      ) 
    ) 

[3] => Array 
    (
     [label] => delivered 
     [fillColor] => #f89406 
     [data] => Array 
      (
       [0] => 1 
       [1] => 0 
      ) 
    ) 

[4] => Array 
    (
     [label] => invoiced 
     [fillColor] => #3a87ad 
     [data] => Array 
      (
       [0] => 2 
       [1] => 0 
      ) 
    ) 

) 

只有4個標籤:

  • pending
  • dispatched
  • delivered
  • invoiced

請注意,數組只是一個例子。可能發生的情況是,第一個數組根本沒有值,或者只有2個,第二個數組有3個值或沒有值。

因爲那上面的約束,我想用array_replace並具有陣列的所謂

base_array = ["pending", "dispatched", "delivered", "invoiced"] 

我試圖環路base_array和嘗試,如果標籤存在於array1array2匹配。

基本上,如果鍵(這是標記)不以任何的array1array2然後存在替換的值將所得數組中爲0。

我已經試過

foreach($base_array as $key => $value) { 
    if(in_array($key, $array1[$key])) { 
     $array[$key] = $array1[$key]; 
    } 
} 

,但它看起來像我失去了對這些多維數組和更換。任何幫助將非常感激。謝謝。

+0

你能解釋一下你的結果陣列數據的關鍵?你的預期結果是什麼? – rocky

+0

期望的數組結果在上面。謝謝。 –

+0

謝謝你的時間。我已經重寫了SQL查詢並返回更好的數組。 –

回答

1

從我從你的問題了解你可以做這樣的: -

$array = array(
    '1' => Array 
     (
     'label' => 'pending', 
     'fillColor' => '#468847', 
     'data' => '50' 
    ), 
    '2' => Array 
     (
     'label' => 'dispatched', 
     'fillColor' => '#6ecf70', 
     'data' => '10' 
    ), 
    '3 ' => Array 
     (
     'label' => 'delivered', 
     'fillColor' => '#f89406', 
     'data' => '1' 
    ), 
    '4' => Array 
     (
     'label' => 'invoiced', 
     'fillColor' => '#3a87ad', 
     'data' => '2' 
    ), 
); 

$array2 = array 
    (
    '1' => Array 
     (
     'label' => 'pending', 
     'fillColor' => '#468847', 
     'data' => array 
      (
      '0' => '1' 
     ) 
    ) 
); 

$temp = array(); 
$i = 0; 
foreach ($array as $key => $value) { 

    $temp[$key]['label'] = $value['label']; 
    $temp[$key]['fillColor'] = $value['fillColor']; 

    foreach ($array2 as $key2 => $value2) { 

     if ($value['fillColor'] == $value2['fillColor'] && $value['label'] == $value2['label']) { 
      $temp[$key]['data'][] = $value['data']; 
      if (isset($value2['data'][$i])) { 
       $temp[$key]['data'][] = $value2['data'][$i]; 
      } 
     } else { 
      $temp[$key]['data'][] = $value['data']; 
      if (!isset($value2['data'][$i])) { 
       $temp[$key]['data'][] = 0; 
      } 
     } 
     $i++; 
    } 
} 

echo '<pre>'; 
print_r($temp); 
+0

它正在爲我給出的數組工作,但不是一般情況。 print_r($ temp)return Array([1] => Array([label] => pending [fillColor] =>#468847 [data] => Array([0] => 50 [1] => 1)) [2] => Array([label] => dispatched [fillColor] =>#6ecf70)[3] => Array([label] => delivered [fillColor] =>#f89406)[4] => Array([標籤] =>發票[fillColor] =>#3a87ad)) –

+0

請解釋您的預期結果,以便我可以幫助更多 – rocky

+0

預期的臨時數組是將「數據」作爲一個數組,如果沒有找到0。感謝您的時間。我已經重寫了SQL查詢並返回更好的數組。 –

0

嘗試了這一點:

$array1 = array(
    array(
     'label'  => 'pending', 
     'fillColor' => '#468847', 
     'data'  => '50' 
    ), 
    array(
     'label'  => 'dispatched', 
     'fillColor' => '#468847', 
     'data'  => '10' 
    ), 
    array(
     'label'  => 'delivered', 
     'fillColor' => '#468847', 
     'data'  => '8' 
    ), 
    array(
     'label'  => 'invoiced', 
     'fillColor' => '#468847', 
     'data'  => '5' 
    ) 

); 
$array2 = array(
    array(
     'label'  => 'pending', 
     'fillColor' => '#468847', 
     'data'  => array() 
    ), 
    array(
     'label'  => 'dispatched', 
     'fillColor' => '#6ecf70', 
     'data'  => array() 
    ), 
    array(
     'label'  => 'delivered', 
     'fillColor' => '#f89406', 
     'data'  => array() 
    ), 
    array(
     'label'  => 'invoiced', 
     'fillColor' => '#3a87ad', 
     'data'  => array() 
    ) 
); 

foreach ($array1 as $order) { 
    foreach ($array2 as $key => $group) { 
     if ($order['label'] == $group['label']) { 
      array_push($array2[$key]['data'], $order['data']); 
     } 
    } 
} 

var_dump($array2); 
+0

謝謝你的時間。我已經重寫了SQL查詢並返回更好的數組。順便說一句,它不工作。 –

+0

代碼工程我多次測試https://ideone.com/YyAZKP – Fluinc

+0

它的工作原理,但沒有給出預期的結果。見上面的答案。 –

相關問題