0

Array $test_count

array ( 0 => array ( 0 => array ( 'it_id' => '212', 'item' => 'Item 1', 'quantity' => '5', ), 1 => array ( 'it_id' => '1206', 'item' => 'Item 2', 'quantity' => '1', ), 2 => array ( 'it_id' => '212', 'item' => 'Item 1', 'quantity' => '1', ), ), )

I need to output key quantity value common value if same array it_id is same, if same item is more than one time in array. Every item have it_id(for id) with value, item(for name) with value, quantity with value, than second item in same way...

Output example for now:

Item id : 212 Key quantity: 2
Item id : 1206 Key quantity: 1

Code for output:

foreach($test_count as $tempo => $lol){
    foreach ($lol  as $value1) {
        $numbers1[$value1[it_id]]++;
    }

    foreach ($numbers1 as $key1 => $value1) {
        if ($value1 > 1){
            echo '<font style="color:red;"> Item id : '.$key1.' Key quantity: '.$value1.'</font><br/>';
        }else{
            echo 'Item id : '.$key1.' Key quantity: '.$value1.'<br/>';
        }
    }
}

For now i abble to find multiple items in this array using id_it and display how many of them is in array, but totally don't know how to count quantity, how to take more than one key from same array. To get result like this one:

Item id : 212 Key quantity: 2 Item 1 quantity: 7

Item id : 1206 Key quantity: 1 Item 2 quantity: 1

Solved, check answers

small bonus, if you wish to see result not in array but in almost userfrendly way, here is the code for table from result:

code

echo '<table rules="all" border="1">';
    foreach($out as $name => $ar) {
       foreach($ar as $v1 => $v2) {
            echo sprintf('<tr><td>%s</td><td>%s</td><td>%s</td></tr>'."\r\n",
                $name, $v1, $v2);           
        }
    }
echo '</table>';
Amimistik
  • 35
  • 6
  • 1
    You should not do `$numbers1[$value1[it_id]]++`, without making sure `$numbers1[$value1[it_id]]` is actually set, you need to properly initialize it the first time you access it. – CBroe Jul 20 '21 at 08:25
  • 1
    Just use an additional array dimension - count the number under `$numbers1[$value1[it_id]]['number']`, and sum up the amounts under `$numbers1[$value1[it_id]]['quantity']`, or something like that. – CBroe Jul 20 '21 at 08:26
  • Ty, will try it, cos was having even no idea, how to – Amimistik Jul 20 '21 at 08:36
  • 1
    @Amimistik Just giving a side note: Please indent your code from next time onwards. This makes the code more readable. Also, share a var_export() of your array instead of print_r() – nice_dev Jul 20 '21 at 08:36
  • Did you just edit your question to show an alternative answer? Please take the [tour]. – mickmackusa Jul 20 '21 at 11:50

1 Answers1

1

You just add an extra increment in your first loop to keep track of the item count, like so:

<?php
$out = array();
foreach ($data[0] as $entry) {
    $id = $entry['it_id'];
    // Ensure the output key we're going to increment actually exists...
    if (!isset($out[$id])) {
        $out[$id] = array('item_id' => $id, 'key_quantity' => 0, 'item_quantity' => 0);
    }

    $out[$id]['key_quantity'] += 1;
    $out[$id]['item_quantity'] += $entry['quantity'];
}

This will give you an $out array with the following structure:

array (
  212 => 
  array (
    'item_id' => '212',
    'key_quantity' => 2,
    'item_quantity' => 6
  ),
  1206 => 
  array (
    'item_id' => '1206',
    'key_quantity' => 1,
    'item_quantity' => 1
  )
)
Simon Brahan
  • 2,016
  • 1
  • 14
  • 22
  • maybe i'm doing wrong,i'm getting same array as input var_export ($out); output: 'array ( 0 => array ( 0 => array ( 'it_id' => '212', 'item' => 'Item 1', 'quantity' => '1', ), 1 => array ( 'it_id' => '1206', 'item' => 'Item 2', 'quantity' => '1', ), 2 => array ( 'it_id' => '212', 'item' => 'Item 1', 'quantity' => '1', ), ), )' – Amimistik Jul 20 '21 at 09:22
  • Running example here: http://sandbox.onlinephpfunctions.com/code/9b248f510c29c584f91eaccbaa65a5398b3454ae. What are you doing differently? – Simon Brahan Jul 20 '21 at 09:28
  • only one different fom your code: '$out = $test_count;' var out = my array var – Amimistik Jul 20 '21 at 09:33
  • [link]https://ibb.co/G5DCzcZ – Amimistik Jul 20 '21 at 09:36
  • What's the difference for? – Simon Brahan Jul 20 '21 at 09:37
  • but now will be trying to solve, cos code for sure have no problem, big big thank you, for your help, to find why something is not working for me littebit more easy than to create from 0. – Amimistik Jul 20 '21 at 09:38
  • Simon Brahan, don't know, all other code - same ;) but result not ;) lol, find error, my changes is '$out = $test_count;' not data but out ;) wrong variable – Amimistik Jul 20 '21 at 09:39