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

)
7
  • Can you edit your question to show the content of $_POST['variable'] ? It would help. Commented Sep 4, 2020 at 10:12
  • yes let me do it Commented Sep 4, 2020 at 10:16
  • I believe your question is answered here: stackoverflow.com/a/5881484/3772849 Commented Sep 4, 2020 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 Commented Sep 4, 2020 at 10:31
  • You are merging associative arrays together, array_merge overwrites duplicate keys as stated in the documentation. What's the expected result ? Commented Sep 4, 2020 at 10:33

2 Answers 2

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>";
Sign up to request clarification or add additional context in comments.

1 Comment

its not returning the data in actual format, it its the foreach loop and every time it merge the arrays..
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));

2 Comments

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
@UmairMehmood I have updated my answer to work for your case, check it out

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.