Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

array_combine() not giving the expected result in php

I have two arrays as follows, Array a represents the quntites while array b stores the product names

Array A

array:9 [â–Ľ
  0 => 2
  1 => 3
  2 => 10
  3 => 3
  4 => 4
  5 => 8
  6 => 5
  7 => 1
  8 => 1
]

Array B

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

array:9 [â–Ľ
  0 => "Hello world poduct"
  1 => "Test Product"
  2 => "Hello world poduct"
  3 => "Hello world poduct"
  4 => "Test Product"
  5 => "Test Product"
  6 => "Test Product"
  7 => "Test Product"
  8 => "Test Product"
]

Now I’m trying to combine these two arrays and get the following output,

Array 
  ( 
       [Hello world poduct] => 2
       [Test Product]       => 3 
       [Hello world poduct] => 10
       [Hello world poduct] => 3
       [Test Product]       => 4 
       [Test Product]       => 8 
       [Test Product]       => 5 
       [Test Product]       => 1 
       [Test Product]       => 1 
)

I tried using PHP’s array_combine() function

array_combine($arr_a,$arr_b)

This is not giving me the expected output…What changes should I make in order to take the expected result…or what would be the correct approach creating new array with expected way…

UPDATE

My final goal is to take the products and their total as mentioned bellow,

Array(
  [Test Product] => 22 
  [Hello world poduct] => 15 
)

As @foobar mentioned in the comments, since my keys are not unique, what coould be the better way doing this?

>Solution :

Here are two examples of how it can be done:

Data:

$a = array(
  0 => 2,
  1 => 3,
  2 => 10,
  3 => 3,
  4 => 4,
  5 => 8,
  6 => 5,
  7 => 1,
  8 => 1
);

$b = array(
  0 => "Hello world poduct",
  1 => "Test Product",
  2 => "Hello world poduct",
  3 => "Hello world poduct",
  4 => "Test Product",
  5 => "Test Product",
  6 => "Test Product",
  7 => "Test Product",
  8 => "Test Product",
);
  1. (for loop)
#for usage
$result = [];
for($i=0;$i<count($a);$i++){
    if(!isset($result[$b[$i]])){
        $result[$b[$i]] = 0;
    }
    $result[$b[$i]] += $a[$i];
}
var_export($result);
  1. (array walk)
#array_walk
$result = [];
array_walk($a,function($v,$i) use (&$result,$b) {
    if(!isset($result[$b[$i]])){
        $result[$b[$i]] = 0;
    }
    $result[$b[$i]] += $v;
});
var_export($result);

Live demo: https://3v4l.org/Fh5Xj

Relevant documentation: https://www.php.net/manual/en/function.array-walk.php

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading