-1

i want to combine the values of array having same index name in foreach loop..

i tried array_combine but it returns the single array.

$data = $_POST['variable']; //it contain the values in an array

$result=array();
  foreach ($data as $mycat){
    $result = array_merge($result, $mycat);
  }
  echo "<pre>";print_r($result);echo "</pre>";

it returns only data in single array

Array
(
    [vendor] => 1-Open Market
    [priority] => 2
    [demand_for_id] => 9
    [ims_allocation_details_id] => 148
    [temp_demand_id] => 1
)

as shown in attached picture item names are same, so when item names are same i want to combine the total values in foreach and insert only one record into database instead to two enter image description here

the contents of $_POST['variable']; are

Array
(
    
    [2] => Array
        (
            [vendor] => 1-Open Market
            [temp_demand_id] => 6
            [priority] => 1
            [item_name] => BAJRA MOTI
            [amount] => 1000
            [demand_for_id] => 9
            [ims_allocation_details_id] => 153
        )

    [1] => Array
        (
            [vendor] => 1-Open Market
            [temp_demand_id] => 1
            [priority] => 2
            [item_name] => BAJRA MOTI
            [amount] => 2500
            [demand_for_id] => 9
            [ims_allocation_details_id] => 148
        )

)
  • Can you edit your question to show the content of `$_POST['variable']` ? It would help. – AymDev Sep 04 '20 at 10:12
  • yes let me do it – Umair Mehmood Sep 04 '20 at 10:16
  • I believe your question is answered here: https://stackoverflow.com/a/5881484/3772849 – wajdi_jurry Sep 04 '20 at 10:20
  • @wajdi_jurry dear i checked you suggested link but the solution on that question isn't what i need, as you can see the $_POST['content']; i have same item_name so what i need is combile the total_amount 1000+2500 = 3500 and insert into db only once in foreach loop instead of creating two entries into database – Umair Mehmood Sep 04 '20 at 10:31
  • You are merging associative arrays together, array_merge overwrites duplicate keys as stated in the [documentation](https://www.php.net/manual/en/function.array-merge). What's the expected result ? – AymDev Sep 04 '20 at 10:33
  • @AymDev ok so can you please suggest me something that can help me with this situation – Umair Mehmood Sep 04 '20 at 10:58
  • I can't as I don't know what is the expected result. – AymDev Sep 04 '20 at 11:31

2 Answers2

0

You should replace

$result = array_merge($result, $mycat);

with

$result[] = array_merge($result, $mycat);

and it will not be single

UPDATE

$result = array();
  foreach ($data as $mycat) {
      if(!isset($result[$mycat['item_name']])) {
        $result[$mycat['item_name']] = $mycat;
      } else {
          //do if needed
      }
     
      
  }
  echo "<pre>";print_r($result);echo "</pre>";
Umid
  • 1
  • 2
0

You can create a custom function to solve your problem.

Example:

<?php
$array = [
    [
        'vendor' => '1-Open Market',
        'temp_demand_id' => 6,
        'priority' => 1,
        'item_name' => 'BAJRA MOTI',
        'amount' => 1000,
        'demand_for_id' => 9,
        'ims_allocation_details_id' => 153,
    ],
    [
        'vendor' => '1-Open Market',
        'temp_demand_id' => 1,
        'priority' => 2,
        'item_name' => 'BAJRA MOTI',
        'amount' => 2500,
        'demand_for_id' => 9,
        'ims_allocation_details_id' => 148,
    ],
    [
        'vendor' => '1-Open Market',
        'temp_demand_id' => 5,
        'priority' => 3,
        'item_name' => 'BAJRA MOTI',
        'amount' => 1000,
        'demand_for_id' => 11,
        'ims_allocation_details_id' => 200,
  ],
];

function array_merge_recursive_custom($array) {
    $processed = null;
    foreach ($array as &$subArray) {
        if (empty($processed)) {
            $processed = $subArray;
            continue;
        }
        foreach ($subArray as $key => $value) {
            if (is_numeric($value)) {
                $subArray[$key] += $processed[$key];
            }
        }
        $processed = $subArray;
    }
    
    return end($array);
}

var_dump(array_merge_recursive_custom($array));
wajdi_jurry
  • 268
  • 2
  • 9
  • dear @wajdi_jurry thank for your quick help but sorry to say my arrays are not limited to two or three, they are dynamically generated on the bases of check box, if i check two check box and post the form it will generate two arrays if i check more then two check boxes it will generate more then two arrays and all arrays data posted in $data variable – Umair Mehmood Sep 04 '20 at 12:27
  • @UmairMehmood I have updated my answer to work for your case, check it out – wajdi_jurry Sep 05 '20 at 09:42