Viewed   67 times

Let's say I have following arrays:

Array
    (
        [0] => Array
            (
                [id] => 5
                [name] => Education
            )

        [1] => Array
            (
                [id] => 4
                [name] => Computers
            )

        [3] => Array
            (
                [id] => 7
                [name] => Science

        [4] => Array
            (
                [id] => 1
                [name] => Sports
            )
    )

And the second one:

Array
    (
        [0] => Array
            (
                [id] => 1
                [title] => Sport
            )

        [1] => Array
            (
                [id] => 7
                [title] => Sci
            )

        [3] => Array
            (
                [id] => 4
                [title] => Comp

        [4] => Array
            (
                [id] => 5
                [title] => Edu
            )
    )

And desired output is:

Array
    (
        [0] => Array
            (
                [id] => 5
                [name] => Education
                [title] => Edu
            )

        [1] => Array
            (
                [id] => 4
                [name] => Computers
                [title] => Comp
            )

        [3] => Array
            (
                [id] => 7
                [name] => Science
                [title] => Sci

        [4] => Array
            (
                [id] => 1
                [name] => Sports
                [title] => Sport
            )
    )

I have managed to merge these arrays with simply:

foreach($first as $key => $value){
    $result[$key] = array_merge($first[$key], $second[$key]);
}

But the output is not combined correctly:

Array
    (
        [0] => Array
            (
                [id] => 5
                [name] => Education
                [title] => Sport
            )

        [1] => Array
            (
                [id] => 4
                [name] => Computers
                [title] => Sci
            )

        [3] => Array
            (
                [id] => 7
                [name] => Science
                [title] => Comp

        [4] => Array
            (
                [id] => 1
                [name] => Sports
                [title] => Edu
            )
    )

The problem is I would like to merge these arrays on the same id. Desired output sorting should be same as in the first array.

How can I achieve this? Any help is much appreciated.

 Answers

2

You can just do a nested loop and check if the id values match, then add title to $first (or name to $second)

foreach($first as $key => $value){
    foreach($second as $value2){
        if($value['id'] === $value2['id']){
            $first[$key]['title'] = $value2['title'];
        }               
    }
}
Friday, September 9, 2022
3

Try this solution. You can add any count of arrays. But keep names as $arr1-$maxArraysCount

$arr1 = array(
    "Friday" => array(
        "Breakfast" => 32,
        "Lunch" => 45
    ),
    "Sunday" => array(
        "Lunch" => 12
    )
);

$arr2 = array(
    "Sunday" => array(
        "Breakfast" => 7,
        "Lunch" => 3
    ),
    "Monday" => array(
        "Breakfast" => 12
    )
);

$arr3 = array(
    "Monday" => array(
        "Breakfast" => 31
    )
);


$maxArraysCount = 8;
$return = array();
for($i = 1; $i < $maxArraysCount; $i++){
    $arr = 'arr' . $i;
    if(isset($$arr) && is_array($$arr)){
        foreach ($$arr as $day => $value) {
            foreach ($value as $eat => $count) {
                if(!isset($return[$day][$eat])) $return[$day][$eat] = 0;
                $return[$day][$eat] = $count + $return[$day][$eat];
            }
        }
    }
}

echo "<pre>";print_r($return);

Here is output:

Array
(
    [Friday] => Array
        (
            [Breakfast] => 32
            [Lunch] => 45
        )

    [Sunday] => Array
        (
            [Lunch] => 15
            [Breakfast] => 7
        )

    [Monday] => Array
        (
            [Breakfast] => 43
        )

)
Saturday, September 24, 2022
3

This is what I would do:

<?php
$promo = Array (
   Array ('insertDate' => '2014-11-10 11:23:08', 'keyword' => 'promo', 'mediaClass' => 'image', 'mediaURL' => 'http://image2.jpg', 'promoURL' => 'http://www.google.com'),
   Array ('insertDate' => '2014-11-10 11:23:38', 'keyword' => 'promo', 'mediaClass' => 'image', 'mediaURL' => 'http://image5.jpg', 'promoURL' => 'http://www.google.com')
 );

$media = Array (
  Array ('insertDate' => '2014-11-10 11:22:58', 'keyword' => 'standard', 'mediaClass' => 'image', 'mediaURL' => 'http://image1.jpg', 'promoURL' => '', ),
  Array ('insertDate' => '2014-11-10 11:23:18', 'keyword' => 'standard', 'mediaClass' => 'image', 'mediaURL' => 'http://image3.jpg', 'promoURL' => '', ),
  Array ('insertDate' => '2014-11-10 11:23:28', 'keyword' => 'standard', 'mediaClass' => 'image', 'mediaURL' => 'http://image4.jpg', 'promoURL' => '', ),
  Array ('insertDate' => '2014-11-10 11:23:48', 'keyword' => 'standard', 'mediaClass' => 'image', 'mediaURL' => 'http://image6.jpg', 'promoURL' => '', )
);

//sort the promo in random order. This ensures they go into media in random order
shuffle($promo);

//get random keys to insert the promo image before
$randKeys = array_rand($media, count($promo));
//sort by the random key value in reverse. By inserting in reverse order,
//we won't have an issue with needing to increment keys to prevent two promo
//images ending up next to each other. Also, this is why we shuffle the promo
//array above, so the promo images go in random order because this is no longer random.
rsort($randKeys);

//loop over the random keys and insert the next promo image before each key
foreach($randKeys as $key){
    //get the first promo image and remove it
    $promoImage = array_shift($promo);
    //splice the promo image into the media array
    array_splice($media, $key, 0, array($promoImage));
}

//display
print_r($media);

Only thing to note is that both my media and promo arrays are numeric index based starting at zero and contiguous which is default for an array and not like the arrays you posted where they 1 and 4 keys are removed from the media array.

Tuesday, October 4, 2022
 
4
function calculateDifference($array1, $array2){
  $difference = array();
  foreach($array1 as $key => $value){
    if(isset($array2[$key])){
      $difference[$key] = abs($array1[$key] - $array2[$key]);
    }else{
      $difference[$key] = $value;
    }
  }
  foreach($array2 as $key => $value){
    if(isset($array1[$key])){
      $difference[$key] = abs($array1[$key] - $array2[$key]);
    }else{
      $difference[$key] = $value;
    }
  }
  return $difference;
}
Tuesday, August 2, 2022
 
5

Try this :

$res      = array();
foreach($your_array as $val){
   $res[$val['employee']]['employee']         = $val['employee'];
   $res[$val['employee']]['first_name']       = $val['first_name'];
   $res[$val['employee']]['surname']          = $val['surname'];
   if($val['type'] == 0){
      if(array_key_exists("totaltime_type_0",$res[$val['employee']])){
         $res[$val['employee']]['totaltime_type_0'] += $val['totaltime'];
      }
      else{
         $res[$val['employee']]['totaltime_type_0'] = $val['totaltime'];
      }
   }
   if($val['type'] == 1){
      if(array_key_exists("totaltime_type_1",$res[$val['employee']])){
         $res[$val['employee']]['totaltime_type_1'] += $val['totaltime'];
      }
      else{
         $res[$val['employee']]['totaltime_type_1'] = $val['totaltime'];
      }
   }
}

echo "<pre>";
print_r(array_values($res));
Thursday, September 1, 2022
 
texas
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :