当具有相同的特定值时,将值从一个数组添加到另一个数组

时间:2016-10-28 20:19:49

标签: php

我有这两个数组:

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

);

我想创建第三个像这样的人,但我不知道该怎么办:

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

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

这样做的最佳方法是什么,我尝试了很多方法,但没有成功?

感谢您的帮助

3 个答案:

答案 0 :(得分:2)

我将通过示例完成以前的答案:)

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

);

?>

我修改了$ar1$ar2:我添加了一个id键(使用任何唯一值)。有时这只是编辑id并避免使用大算法这么简单:)

合并:

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

结果 - 你放松了第二个暗淡:

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)

递归合并:

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

预期结果! (但我们有两个名字/经营者)

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)

循环

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

?>

结果:

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)

现在,您只需要清理数据:)(仅保留名称或运算符 - 请参阅@ofca答案)

答案 1 :(得分:0)

通过使用$ar3按名称重新编号$ar1来创建array_column。然后迭代$ar2并将totalop值附加到$ar3中的相应键。

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

答案 2 :(得分:-1)

解决方案:

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