PHP在Array中查找相同的值并创建新数组

时间:2015-11-06 22:15:44

标签: php arrays json

我有一个这样的数组:

array 
  0 => 
    array (size=5)
      'id' => string '1' (length=1)
      'hostings_id' => string '4' (length=1)
      'email' => string 'info@test.de' (length=16)
    array (size=5)
      'id' => string '3' (length=1)
      'hostings_id' => string '4' (length=1)
      'email' => string 'kun@test.de' (length=26)
    array (size=5)
      'id' => string '5' (length=1)
      'hostings_id' => string '4' (length=1)
      'email' => string 'ni@test.de' (length=23)
  1 => 
    array (size=5)
      'id' => string '410' (length=3)
      'hostings_id' => string '5' (length=1)
      'email' => string 'spam@email.de' (length=13)
    array (size=5)
      'id' => string '148' (length=3)
      'hostings_id' => string '5' (length=1)
      'email' => string 'e@email.de' (length=23) 
   2 =>   
    array (size=5)
      'id' => string '165' (length=3)
      'hostings_id' => string '8' (length=1)
      'email' => string 'mobil@newmail.de' (length=15)
  3 => 

我的目标是将此数组放入一个新数组中:

{
    "emails": {
        "0": {
            "id": "1",
            "hostings_id": "4",
            "email": "info@test.de",
        },
        "1": {
            "id": "3",
            "hostings_id": "4",
            "email": "kun@test.de",
        },
        "2": {
            "id": "5",
            "hostings_id": "4",
            "email": "ni@test.de",
        },
    }
}

{
    "emails": {
        "0": {
            "id": "410",
            "hostings_id": "5",
            "email": "spam@email.de",
        },
        "0": {
            "id": "148",
            "hostings_id": "5",
            "email": "e@email.de",
        },
    }
}
...

最后我想要一个像这样的JSON:

{{1}}

我想基于'hostings_id'创建JSON 具有相同“hostings_id”值的所有东西应该在一个单独的JSON中。

我已经尝试了很多foreach和array_count_values,但没有得到我想要的结果。 非常感谢你的帮助。

问候

2 个答案:

答案 0 :(得分: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

这是你的最终代码,包括json和相同的hosting_id但是在不同的json数组中:

<?php
$array1 = array('0'=>array('id'=>5,'hosting_id'=>'4','email'=>'example@sample.com'),'1'=>array('id'=>6,'hosting_id'=>'5','email'=>'example@sample.com'),'2'=>array('id'=>8,'hosting_id'=>'4','email'=>'example@sample.com'),'3'=>array('id'=>10,'hosting_id'=>'5','email'=>'example@sample.com'),'4'=>array('id'=>11,'hosting_id'=>'5','email'=>'example@sample.com'));
$array2 = array('0'=>array('id'=>78,'hosting_id'=>'4','email'=>'example@sample.com'),'1'=>array('id'=>96,'hosting_id'=>'5','email'=>'example@sample.com'),'2'=>array('id'=>78,'hosting_id'=>'4','email'=>'example@sample.com'),'3'=>array('id'=>110,'hosting_id'=>'5','email'=>'example@sample.com'),'4'=>array('id'=>111,'hosting_id'=>'5','email'=>'example@sample.com'));

// 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"
}
]
}
}

答案 1 :(得分: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));
}

再次,谢谢!