2017-04-05 87 views
0

我使用var_dump($ fields)來輸出下面的PHP數組;將具有匹配鍵值的數組移動到新的分組數組

array() { 
    [ 0 ] => array() { 
     [ "state" ] => string(3) "nsw" 
     [ "address" ] => string(13) "8 Test Street" 
     [ "phone" ] => string(12) "0000 000 000" 
     [ "email" ] => string(15) "[email protected]" 
    } 
    [ 1 ] => array() { 
     [ "state" ] => string(2) "wa" 
     [ "address" ] => string(13) "5 Test Street" 
     [ "phone" ] => string(12) "0000 000 000" 
     [ "email" ] => string(15) "[email protected]" 
    } 
    [ 2 ] => array() { 
     [ "state" ] => string(3) "qld" 
     [ "address" ] => string(13) "3 Test Street" 
     [ "phone" ] => string(12) "0000 000 000" 
     [ "email" ] => string(15) "[email protected]" 
    } 
    [ 3 ] => array() { 
     [ "state" ] => string(3) "nsw" 
     [ "address" ] => string(13) "1 Test Street" 
     [ "phone" ] => string(12) "0000 000 000" 
     [ "email" ] => string(15) "[email protected]" 
    } 
} 

我想從所有的「狀態」鍵創建新陣列呈現在$字段和對應的值以上,從而其像下面移動。

array() { 
    [ "nsw" ] => array() { 
     [0] => array() { 
      [ "address" ] => string(13) "8 Test Street" 
      [ "phone" ] => string(12) "0000 000 000" 
      [ "email" ] => string(15) "[email protected]" 
     } 
     [1] => array() { 
      [ "address" ] => string(13) "1 Test Street" 
      [ "phone" ] => string(12) "0000 000 000" 
      [ "email" ] => string(15) "[email protected]" 
     } 
    } 
    [ "wa" ] => array() { 
     [0] => array() { 
      [ "address" ] => string(13) "5 Test Street" 
      [ "phone" ] => string(12) "0000 000 000" 
      [ "email" ] => string(15) "[email protected]" 
     } 
    } 
    [ "qld" ] => array() { 
     [0] => array() { 
      [ "address" ] => string(13) "3 Test Street" 
      [ "phone" ] => string(12) "0000 000 000" 
      [ "email" ] => string(15) "[email protected]" 
     } 
    } 
} 

任何人都可以請大家幫忙,一直在尋找這個答案沒有運氣。

謝謝!

回答

1

只是一點點邏輯。

<?php 

$fields = [ 
    [ 
     "state" => "nsw", 
     "address" => "8 Test Street", 
     "phone" => "0000 000 000", 
     "email" => "[email protected]", 
    ], 
    [ 
     "state" => "wa", 
     "address" => "5 Test Street", 
     "phone" => "0000 000 000", 
     "email" => "[email protected]", 
    ], 
    [ 
     "state" => "qld", 
     "address" => "3 Test Street", 
     "phone" => "0000 000 000", 
     "email" => "[email protected]", 
    ], 
    [ 
     "state" => "nsw", 
     "address" => "1 Test Street", 
     "phone" => "0000 000 000", 
     "email" => "[email protected]", 
    ] 
]; 

$new = []; 
foreach ($fields as $field) { 
    $state = $field['state']; 
    unset($field['state']); 
    $new[$state][] = $field; 
} 

print_r($new); 

Array 
(
    [nsw] => Array 
     (
      [0] => Array 
       (
        [address] => 8 Test Street 
        [phone] => 0000 000 000 
        [email] => [email protected] 
       ) 

      [1] => Array 
       (
        [address] => 1 Test Street 
        [phone] => 0000 000 000 
        [email] => [email protected] 
       ) 

     ) 

    [wa] => Array 
     (
      [0] => Array 
       (
        [address] => 5 Test Street 
        [phone] => 0000 000 000 
        [email] => [email protected] 
       ) 

     ) 

    [qld] => Array 
     (
      [0] => Array 
       (
        [address] => 3 Test Street 
        [phone] => 0000 000 000 
        [email] => [email protected] 
       ) 

     ) 

) 
+0

謝謝亞歷山大!非常感激。 –

0

這應該乾淨利落。

$grouped = array_reduce($addresses, function ($grouped, $address) { 
    return array_merge_recursive($grouped, [ 
     $address['state'] => [ $address ], 
    ]); 
}, []);