0

Here is array #1

Array
(
    [0] => Array
        (
            [first] => LightSpeed
            [last] => Administrator
        )

    [1] => Array
        (
            [first] => Tyler
            [last] => Nichol
        )

Here is array #2

Array
(
    [I-10] => Array
        (
            [user] => 2
            [total] => 46.64
        )

    [I-11] => Array
        (
            [user] => 2
            [total] => -46.64
        )

I just want to add [total] => $value to the first array so it looks like.

Array
(
    [0] => Array
        (
            [first] => LightSpeed
            [last] => Administrator
            [total] => 46.64
        )

    [1] => Array
        (
            [first] => Tyler
            [last] => Nichol
            [total] => -46.64
        )

Pretty sure it is array_push but not sure how to loop it. Any suggestions? Thanks!

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Tyler Nichol
  • 635
  • 2
  • 7
  • 28

3 Answers3

0

It's a bit low tech, but I'd just loop through your array and insert the total item. Assuming you're just matching to the second array on the position of the items in the arrays:

$vals = array_values($arr2);
foreach($arr1 as $i=>$item) {
    $arr1[$i]['total'] = $vals[$i]['total'];
}
Tieran
  • 976
  • 6
  • 9
0

You don't have to loop all the time .. array_merge can do the trick

function superMerge($a, $b) {
    $a['total'] = $b['total'];
    return $a;
}

$array1 = array(0 => Array("first" => "LightSpeed","last" => "Administrator"),1 => Array("first" => "Tyler","last" => "Nichol"));
$array2 = array("I-10" => Array("user" => 2,"total" => 46.64),"I-11" => Array("user" => 2,"total" => - 46.64));

var_dump(array_map("superMerge", $array1, $array2));

Output

array
  0 => 
    array
      'first' => string 'LightSpeed' (length=10)
      'last' => string 'Administrator' (length=13)
      'total' => float 46.64
  1 => 
    array
      'first' => string 'Tyler' (length=5)
      'last' => string 'Nichol' (length=6)
      'total' => float -46.64
Baba
  • 94,024
  • 28
  • 166
  • 217
0

Your array is not well formatted so I have done that in my answer. You might want to update your question

<?php
$arr1 = Array
(
     Array
        (
            'first' => 'LightSpeed',
            'last' => 'Administrator'
        ),

     Array
        (
            'first' => 'Tyler',
            'last' => 'Nichol'
        )
);

$arr2 = Array
(
    'I-10' => Array
        (
            'user' => 2,
            'total' => 46.64
        ),

    'I-11' => Array
        (
            'user' => 2,
            'total' => -46.64
        )
);


$n = count($arr1);
$i = 0;
foreach($arr2 as $arr)
{
   $arr1[$i]['total'] = $arr['total'];
   $i++;
}

var_dump($arr1);
?>

Result of var_dump

array
  0 => 
    array
      'first' => string 'LightSpeed' (length=10)
      'last' => string 'Administrator' (length=13)
      'total' => float 46.64
  1 => 
    array
      'first' => string 'Tyler' (length=5)
      'last' => string 'Nichol' (length=6)
      'total' => float -46.64
codingbiz
  • 26,179
  • 8
  • 59
  • 96