skip to Main Content

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

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?

2

Answers


  1. Here are three examples of how it can be done:

    Data:

    $a = array(
      2,
      3,
      10,
      3,
      4,
      8,
      5,
      1,
      1
    );
    
    $b = array(
      "Hello world poduct",
      "Test Product",
      "Hello world poduct",
      "Hello world poduct",
      "Test Product",
      "Test Product",
      "Test Product",
      "Test Product",
      "Test Product",
    );
    
    1. (foreach)
    #foreach usage
    $result = [];
    $numbers = $a;
    $names = $b;
    foreach ($names as $index => $name) {
        $result[$name] ??= 0;
        $result[$name] += $numbers[$index];
    }
    var_export($result, false);
    
    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/OeWln

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

    Login or Signup to reply.
  2. An even simpler approach:

    $arr_a = [2, 3, 10, 3, 4, 8, 5, 1, 1];
    
    $arr_b = [
      "Hello world poduct",
      "Test Product",
      "Hello world poduct",
      "Hello world poduct",
      "Test Product",
      "Test Product",
      "Test Product",
      "Test Product",
      "Test Product",
    ];
    
    $result = [];
    foreach ($arr_b as $key => $product) {
      $result[$product] = ($result[$product] ?? 0) + $arr_a[$key];
    
    }
    
    var_dump($result);
    

    This gives you:

    array(2) {
      ["Hello world poduct"]=>
      int(15)
      ["Test Product"]=>
      int(22)
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search