Eduardorph
Eduardorph

Reputation: 153

Add a value from one array to another when they have an equal specific value

I have these two arrays:

$ar1 = array(
    0 => array(
        'name' => 'MIRIAN',
        'total' => '34'
    ),
    1 => array(
        'name' => 'THIAGO',
        'total' => '29'
    ),
    2 => array(
        'name' => 'EDUARDO',
        'total' => '3'
    )
);


$ar2 = array(
    0 => array(
        'operator' => 'THIAGO',
        'totalop' => '703'
    ),
    1 => array(
        'operator' => 'MIRIAN',
        'totalop' => '180'
    )

);

And i want to create a third like this, but i don't know what should i do:

$ar3 = array(
    0 => array(
        'name' => 'MIRIAN',
        'total' => '34'
        'totalop' => '180'

    ),
    1 => array(
        'name' => 'THIAGO',
        'total' => '29',
        'totalop' => '703'
    ),
    2 => array(
        'name' => 'EDUARDO',
        'total' => '3'
    )
);

What is the best way to do this, I tried many ways, but without success?

Thanks for any help

Upvotes: 0

Views: 57

Answers (3)

Don't Panic
Don't Panic

Reputation: 41810

Create $ar3 by reindexing $ar1 by name using array_column. Then iterate $ar2 and append the totalop values to the corresponding keys in $ar3.

$ar3 = array_column($ar1, null, 'name');
foreach ($ar2 as $x) {
    $ar3[$x['operator']]['totalop'] = $x['totalop'];
}

Upvotes: 0

Georges O.
Georges O.

Reputation: 992

I will complete previous answers by examples :)

<?php

$ar1 = array(
    '#MIRIAN' => array(
        'name' => 'MIRIAN',
        'total' => '34'
    ),
    '#THIAGO' => array(
        'name' => 'THIAGO',
        'total' => '29'
    ),
    '#EDUARDO' => array(
        'name' => 'EDUARDO',
        'total' => '3'
    )
);


$ar2 = array(
    '#THIAGO' => array(
        'operator' => 'THIAGO',
        'totalop' => '703'
    ),
    '#MIRIAN' => array(
        'operator' => 'MIRIAN',
        'totalop' => '180'
    )

);

?>

I modified the $ar1 and $ar2: I added an id key (use any unique value). Sometime this is so simple to just edit the id and avoid a big algorithm :)

Merge:

<?php var_dump( array_merge($ar1, $ar2) ); ?>

Result - you loose the second dim:

array (size=3)
  '#MIRIAN' => 
    array (size=2)
      'operator' => string 'MIRIAN' (length=6)
      'totalop' => string '180' (length=3)
  '#THIAGO' => 
    array (size=2)
      'operator' => string 'THIAGO' (length=6)
      'totalop' => string '703' (length=3)
  '#EDUARDO' => 
    array (size=2)
      'name' => string 'EDUARDO' (length=7)
      'total' => string '3' (length=1)

Merge Recursively:

<?php var_dump( array_merge_recursive($ar1, $ar2) ); ?>

Expected result ! (but we have two name/operators)

array (size=3)
  '#MIRIAN' => 
    array (size=4)
      'name' => string 'MIRIAN' (length=6)
      'total' => string '34' (length=2)
      'operator' => string 'MIRIAN' (length=6)
      'totalop' => string '180' (length=3)
  '#THIAGO' => 
    array (size=4)
      'name' => string 'THIAGO' (length=6)
      'total' => string '29' (length=2)
      'operator' => string 'THIAGO' (length=6)
      'totalop' => string '703' (length=3)
  '#EDUARDO' => 
    array (size=2)
      'name' => string 'EDUARDO' (length=7)
      'total' => string '3' (length=1)

By a loop

<?php
$return = $ar1;

foreach( $ar2 as $k2 => $v2 ) {
  $isFoundKey = null;
  foreach( $return as $k2_2 => $v2_2 ) {
    if( isset($v2['operator'], $v2_2['name']) && $v2['operator'] == $v2_2['name'] )
        $isFoundKey = $k2_2;
  }

  if( !is_null($isFoundKey) )
    $return[$isFoundKey] = array_merge($return[$isFoundKey], $v2);
  else
    $return[$k2] = $v2;
}

var_dump($return);

?>

Result:

array (size=3)
  '#MIRIAN' => 
    array (size=4)
      'name' => string 'MIRIAN' (length=6)
      'total' => string '34' (length=2)
      'operator' => string 'MIRIAN' (length=6)
      'totalop' => string '180' (length=3)
  '#THIAGO' => 
    array (size=4)
      'name' => string 'THIAGO' (length=6)
      'total' => string '29' (length=2)
      'operator' => string 'THIAGO' (length=6)
      'totalop' => string '703' (length=3)
  '#EDUARDO' => 
    array (size=2)
      'name' => string 'EDUARDO' (length=7)
      'total' => string '3' (length=1)

Now, you just need to clean the data :) (keep only name or operator for example - See @ofca answer)

Upvotes: 2

ofca
ofca

Reputation: 661

Solution:

$ar1 = array(
    0 => array(
        'name' => 'MIRIAN',
        'total' => '34'
    ),
    1 => array(
        'name' => 'THIAGO',
        'total' => '29'
    ),
    2 => array(
        'name' => 'EDUARDO',
        'total' => '3'
    )
);


$ar2 = array(
    0 => array(
        'operator' => 'THIAGO',
        'totalop' => '703'
    ),
    1 => array(
        'operator' => 'MIRIAN',
        'totalop' => '180'
    )

);


$index = [];
$ar1Key = 'name';
$ar2Key = 'operator';

foreach ($ar1 as $item) {
    $index[$item[$ar1Key]] = $item;
}

foreach ($ar2 as $item) {
    $pk = $item[$ar2Key];

    unset($item[$ar2Key]);

    if (isset($index[$pk])) {
        $index[$pk] = array_merge($index[$pk], $item);
    } else {
        $index[] = $item;
    }
}

$ar3 = array_values($index);

print_r($ar3);

Upvotes: -1

Related Questions