2015-11-06 74 views
0

我有一個這樣的數組:PHP查找相同的值數組,並創建新的數組

array 
    0 => 
    array (size=5) 
     'id' => string '1' (length=1) 
     'hostings_id' => string '4' (length=1) 
     'email' => string '[email protected]' (length=16) 
    1 => 
    array (size=5) 
     'id' => string '3' (length=1) 
     'hostings_id' => string '4' (length=1) 
     'email' => string '[email protected]' (length=26) 
    2 => 
    array (size=5) 
     'id' => string '5' (length=1) 
     'hostings_id' => string '4' (length=1) 
     'email' => string '[email protected]' (length=23) 
    3 => 
    array (size=5) 
     'id' => string '410' (length=3) 
     'hostings_id' => string '5' (length=1) 
     'email' => string '[email protected]' (length=13) 
    4 => 
    array (size=5) 
     'id' => string '148' (length=3) 
     'hostings_id' => string '5' (length=1) 
     'email' => string '[email protected]' (length=23) 
    5 => 
    array (size=5) 
     'id' => string '165' (length=3) 
     'hostings_id' => string '8' (length=1) 
     'email' => string '[email protected]' (length=15) 
    6 => 
    ... 

我的目標是把這個數組轉換成一個新的數組:

array 
    0 => 
    array (size=5) 
     'id' => string '1' (length=1) 
     'hostings_id' => string '4' (length=1) 
     'email' => string '[email protected]' (length=16) 
    array (size=5) 
     'id' => string '3' (length=1) 
     'hostings_id' => string '4' (length=1) 
     'email' => string '[email protected]' (length=26) 
    array (size=5) 
     'id' => string '5' (length=1) 
     'hostings_id' => string '4' (length=1) 
     'email' => string '[email protected]' (length=23) 
    1 => 
    array (size=5) 
     'id' => string '410' (length=3) 
     'hostings_id' => string '5' (length=1) 
     'email' => string '[email protected]' (length=13) 
    array (size=5) 
     'id' => string '148' (length=3) 
     'hostings_id' => string '5' (length=1) 
     'email' => string '[email protected]' (length=23) 
    2 => 
    array (size=5) 
     'id' => string '165' (length=3) 
     'hostings_id' => string '8' (length=1) 
     'email' => string '[email protected]' (length=15) 
    3 => 

最後我希望像這樣的JSON:

{ 
    "emails": { 
     "0": { 
      "id": "1", 
      "hostings_id": "4", 
      "email": "[email protected]", 
     }, 
     "1": { 
      "id": "3", 
      "hostings_id": "4", 
      "email": "[email protected]", 
     }, 
     "2": { 
      "id": "5", 
      "hostings_id": "4", 
      "email": "[email protected]", 
     }, 
    } 
} 

{ 
    "emails": { 
     "0": { 
      "id": "410", 
      "hostings_id": "5", 
      "email": "[email protected]", 
     }, 
     "0": { 
      "id": "148", 
      "hostings_id": "5", 
      "email": "[email protected]", 
     }, 
    } 
} 
... 

我想基於「hostings_id」 一切什麼有SA創建JSON我'hostings_id'值應該在一個單獨的JSON中。

我已經嘗試了很多foreach和array_count_values,但沒有得到我想要的結果。 非常感謝您的幫助。

問候

+0

這就是你需要的array_merge($ array1,$ array2); –

回答

1

我想你需要的是這樣的:

array_merge($array1, $array2); 

比方說,我有以下陣列:

$array1 = array('id'=>1,'name'=>'james'); 

$array2 = array('address'=>'usa','phone'=>'912092091'); 

// merge then and put all together 

$array_final = array_merge($array1, $array2); 

print_r($array_final); 

查看以下鏈接瞭解更多信息:

http://php.net/manual/it/function.array-merge.php

,這裏是你的最終代碼,包括與同hosting_id但在不同的JSON數組JSON:

<?php 
$array1 = array('0'=>array('id'=>5,'hosting_id'=>'4','email'=>'[email protected]'),'1'=>array('id'=>6,'hosting_id'=>'5','email'=>'[email protected]'),'2'=>array('id'=>8,'hosting_id'=>'4','email'=>'[email protected]'),'3'=>array('id'=>10,'hosting_id'=>'5','email'=>'[email protected]'),'4'=>array('id'=>11,'hosting_id'=>'5','email'=>'[email protected]')); 
$array2 = array('0'=>array('id'=>78,'hosting_id'=>'4','email'=>'[email protected]'),'1'=>array('id'=>96,'hosting_id'=>'5','email'=>'[email protected]'),'2'=>array('id'=>78,'hosting_id'=>'4','email'=>'[email protected]'),'3'=>array('id'=>110,'hosting_id'=>'5','email'=>'[email protected]'),'4'=>array('id'=>111,'hosting_id'=>'5','email'=>'[email protected]')); 

// merge then and put all together 

$array_final = array_merge($array1, $array2); 

$out=array(); 
foreach($array_final as $x){ 
    $out[$x['hosting_id']]['hosting_id']=$x['hosting_id']; 
    $out[$x['hosting_id']]['details'][]=array('id'=>$x['id'],'hosting_id'=>$x['hosting_id'],'email'=>$x['email']); 
} 

echo json_encode($out); 

結果:

{ 
4: { 
hosting_id: "4", 
details: [ 
{ 
id: 5, 
hosting_id: "4" 
}, 
{ 
id: 8, 
hosting_id: "4" 
}, 
{ 
id: 78, 
hosting_id: "4" 
}, 
{ 
id: 78, 
hosting_id: "4" 
} 
] 
}, 
5: { 
hosting_id: "5", 
details: [ 
{ 
id: 6, 
hosting_id: "5" 
}, 
{ 
id: 10, 
hosting_id: "5" 
}, 
{ 
id: 11, 
hosting_id: "5" 
}, 
{ 
id: 96, 
hosting_id: "5" 
}, 
{ 
id: 110, 
hosting_id: "5" 
}, 
{ 
id: 111, 
hosting_id: "5" 
} 
] 
} 
} 
0

非常感謝你爲你的速度快,很好的反響! 最後,我並不需要array_merge(),但是你讓我回到了正確的道路上,並帶着您的精彩解釋。 有時我覺得太複雜了。

這是,什麼現在就像一個魅力對我來說:

foreach($result as $x){ 
    $out[$x['hostings_id']][] = array(
     'email' => $x['email'], 
     'login' => $x['login'], 
     'password' => $x['passwort'] 
    ); 
} 

$hosting = new Hosting(); 

foreach($out as $key => $value) { 
    $result = $hosting->insertJsonIntoRowEmails($key, json_encode($value)); 
} 

再次感謝您!

+0

很高興它以任何方式幫助你,,, :),其餘的好運。 –