0

Assume I have two arrays as below

array:2 [
  0 => {#1995
    +"id": 6
    +"sales_target_amount": "30000.00"
  }
  1 => {#1996
    +"id": 10
    +"sales_target_amount": "1000.00"
  }
]

second array

array:2 [
  0 => {#1994
    +"sales_total": "4165.80"
    +"staff_id": 6
  }
  1 => {#1513
    +"sales_total": "1335.60"
    +"staff_id": 10
  }
]

I'm trying to insert the first array sales_target_amount into the second array if the id is matched to the staff_id.

code

  $sum = array_merge($second_array[],$firs_array);

The code above will merge the two array together but not insert the items based on the id.

Expected Result

array:2 [
      0 => {#1994
        +"sales_total": "4165.80"
        +"staff_id": 6
        +"sales_target_amount": "30000.00"
      }
      1 => {#1513
        +"sales_total": "1335.60"
        +"staff_id": 10
        +"sales_target_amount": "1000.00"
      }
    ]
1
  • you want to merge two arrays on a condition, but where you have put the condition? I believe you would have opted for traversing both array and merge them if id matches. Hope you may know that much programming. Commented May 30, 2019 at 5:21

4 Answers 4

1

Here is the snippet, please see inline doc for explanation

$arr = [
    0 => ["sales_total" => "4165.80", "staff_id" => 6],
    1 => ["sales_total" => "1335.60", "staff_id" => 10],
];
$arr1 = [
    0 => ["id" => 6, "sales_target_amount" => "30000.00"],
    1 => ["id" => 10, "sales_target_amount" => "1000.00"],
];
//NULL to return complete arrays or objects and staff id will be key
$arr = array_column($arr, null, 'staff_id');
// mapping id sales target amount combination
$temp = array_column($arr1, 'sales_target_amount', 'id');
foreach ($arr as $key => $value) {
    $arr[$key]['sales_target_amount'] = $temp[$key]; // putting sales_target_amount for id
}

array_column — Return the values from a single column in the input array

Demo Link.

Sign up to request clarification or add additional context in comments.

Comments

0

Use hashed map to group values by staff_id

<?php
$arr1 = [
    [
        'id' => 6,
        'sales_target_amount' => '30000.00'
    ],
    [
        'id' => 10,
        'sales_target_amount' => '1000.00'
    ]
];

$arr2 = [
    [
        'sales_total' => '4165.80',
        'staff_id' => 6
    ],
    [
        'sales_total' => '1335.60',
        'staff_id' => 10
    ]
];

$results = [];

foreach ($arr1 as $value) {
    $results[$value['id']] = [
        'sales_target_amount' => $value['sales_target_amount']
    ];
}

foreach ($arr2 as $value) {
    $results[$value['staff_id']] = array_merge($results[$value['staff_id']], $value);
}

print_r(array_values($results));

Comments

0

array_merge() do not work with multidimensional array. Try to use array_merge_recursive().

And you not need use square brackets in this context. Run function just like

$sum = array_merge_recursive($second_array, $firs_array);

Comments

0

Sort answer using this library

$array = array_merge_recursive(
  Arr::flattenSingle(Arr::group($array1, 'id')), 
  Arr::flattenSingle(Arr::group($array2, 'staff_id'))
);

What it does underneath is to first group your arrays by column you want them merged by. Then because every element in group will have only one corresponding array (cause they are grouped by unique id) flatten those groups to build array with elements like id => array_with_that_id. And then merge those flattened groups by keys which in these case are ids.

Overall this will produce

Array (
  [6] => Array (
    [id] => 6
    [sales_total] => 4165.8
    [staff_id] => 6
    [sales_target_amount] => 30000
  )
  [10] => Array (
    [id] => 10
    [sales_total] => 1335.6
    [staff_id] => 10
    [sales_target_amount] => 1000
  ) 
)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.