ToTe
ToTe

Reputation: 61

PHP Find same values in Array and create new array

I have an array like this:

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 => 
    ...

My goal is to put this array into a new array:

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 => 

At the end I want an JSON like this:

{
    "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]",
        },
    }
}
...

I want to create the JSON based on the 'hostings_id' Everything what has the same 'hostings_id' value should be together in a separated JSON.

I´ve tried a lot with foreach and array_count_values but did not get the result I want. Thank you very much for you help.

regards

Upvotes: 0

Views: 53

Answers (2)

ToTe
ToTe

Reputation: 61

thank you very much for your fast and very good response! At the end I did not need array_merge(), but you brought me back to the right path, with your wonderful explication. Sometimes I think too complicated.

This is, what works now like a charm for me:

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));
}

Again, thank you!

Upvotes: 0

Arsh Multani
Arsh Multani

Reputation: 1591

i think what you need is this:

array_merge($array1, $array2);

let's say i have the following arrays :

$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);

check the following link for more information:

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

and here is your final code including json with same hosting_id but in different json array:

<?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);

result:

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

Upvotes: 1

Related Questions