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>';
5
  • 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. Commented Jul 20, 2021 at 8: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. Commented Jul 20, 2021 at 8:26
  • Ty, will try it, cos was having even no idea, how to Commented Jul 20, 2021 at 8: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() Commented Jul 20, 2021 at 8:36
  • Did you just edit your question to show an alternative answer? Please take the tour. Commented Jul 20, 2021 at 11:50

1 Answer 1

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

7 Comments

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', ), ), )'
Running example here: sandbox.onlinephpfunctions.com/code/…. What are you doing differently?
only one different fom your code: '$out = $test_count;' var out = my array var
What's the difference for?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.