-1

i have two arrays :-

$a1=array(1,1,2,3,1);<br>
$a2=array("m","m","s","xl","s");

i want this as output what should i do :-

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => m
            [2] => 2 //this is count of m
        )
    [1] => Array
        (
            [0] => 1
            [1] => s
            [2] => 1 //this is count of s
        )
    [2] => Array
        (
            [0] => 2
            [1] => s
            [2] => 1 //this is count of s
        )
    [3] => Array
        (
            [0] => 3
            [1] => xl
            [2] => 1 //this is count of xl
        )
)
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Welcome to Stack Overflow. Please read [the help pages](http://stackoverflow.com/help), take [the SO tour](http://stackoverflow.com/tour), read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Lastly please learn how to create a [mcve] of your own attempt to show us, and describe what problems you have with it. – Some programmer dude Nov 06 '19 at 12:05
  • How is it to know which letter from `a2` to count? As in, what is the reason to count `s` twice, and `m` and `xl` only once – Brett Gregson Nov 06 '19 at 12:11
  • Please be online and hang around this site if you want an answer to your question. – nice_dev Nov 06 '19 at 12:15
  • a1 is product id and a2 is product size we need it for ecommerce i hope u get some kinda idea with that. Like based on keys – hemant singh Nov 06 '19 at 12:23
  • a1 is my product id and a2 is my product size when i click on add to cart it send product_id in array like a1 and also send size like array a2 now i want so i want to map a1 to a2 if product size is same then it count and give product id with its size count like i show on my question if not then i will go ahead for another it – hemant singh Nov 06 '19 at 12:34
  • So you want to group based on id _and_ size, and determine the number of items in those groups at the same time? What about the order - does it matter, or can they be in any arbitrary order in the result? And, most importantly - what have you tried so far? This is not a code-writing service, so you should present your own attempt first of all. – 04FS Nov 06 '19 at 12:37
  • i tried this but it dint come up with satisfied ans – hemant singh Nov 06 '19 at 12:41
  • $a1=array(1,1,2,3,1); $a2=array("m","m","s","xl","s"); $a1c = array_unique($a1); $a2c = array_unique($a2); foreach ($a2c as $i => $key) { $at = array(); $k = array_keys($a2, $key); foreach($a1 as $i1 => $val) { if(in_array($i1, $k)) { array_push($at, $val); } } $a3["$key"] = $at; } //$arr = array(); $test = 'm'; function asd ($test, $a4) { $c = 0; foreach($a4 as $key => $value) { if($a2[$value] == 'm') { $c++; } } } print_r($a3); – hemant singh Nov 06 '19 at 12:42
  • order doesnt matter – hemant singh Nov 06 '19 at 12:44

1 Answers1

0

You could do this by looping over your input arrays, and directly putting an element [1, m, 1] into your result array based on the first set of values ($a1[0] and $a1[0]). Then in the next round, you would have to check if your result array already contains an item with the current product id and size - if so, you increment the counter there, if not, you need to create a new element. But checking if such an item already exists is going to be a bit painful then, because basically you would have to loop over all existing items each time again to do so.

I prefer to go with a different, temporary structure first to gather the necessary data, and then transform it into the desired result in a second step.

$a1=array(1,1,2,3,1);
$a2=array("m","m","s","xl","s");

$temp = [];
foreach($a1 as $index => $product_id) {
  $size = $a2[$index];
  // if an entry for given product id and size combination already exists, then the current
  // counter value is incremented by 1; otherwise it gets initialized with 1
  $temp[$product_id][$size] = isset($temp[$product_id][$size]) ? $temp[$product_id][$size]+1 : 1;
}

That gives a $temp array of the following form:

array (size=3)
  1 => 
    array (size=2)
      'm' => int 2
      's' => int 1
  2 => 
    array (size=1)
      's' => int 1
  3 => 
    array (size=1)
      'xl' => int 1

You see the product id is the key on the top level, then the size is the key on the second level, and the value on the second level is the count for that combination of product id and size.

Now we transform that into your desired result structure:

$result = [];
foreach($temp as $product_id => $size_data) {
  foreach($size_data as $size => $count) {
    $result[] = [$product_id, $size, $count];
  }
}
var_dump($result);
04FS
  • 5,660
  • 2
  • 10
  • 21