Rizlan Tamima
Rizlan Tamima

Reputation: 71

Php count multidimension array

here is my array :

$pid = array("id"=>array(
 "098"=> array(
              array("size"=>25,"variant"=>"0925","qty"=>1),
              array("size"=>26,"variant"=>"0926","qty"=>2)
            ),
 "099"=> array(
              array("size"=>25,"variant"=>"0726","qty"=>1)
            )
         )
       );

can i count how much that array with different id? can i count how much that size array per id?

i just wanna see like this :

ID = 2
size of 098 = 2
size of 099 = 1

Upvotes: 1

Views: 44

Answers (2)

hherger
hherger

Reputation: 1680

This corresponds to your array:

<?php
// Your array
$pid = array("id"=>array(
    "098"=> array(
        array("size"=>25,"variant"=>"0925","qty"=>1),
        array("size"=>26,"variant"=>"0926","qty"=>2)
    ),
    "099"=> array(
        array("size"=>25,"variant"=>"0726","qty"=>1)
    )
) );

// The relevant code
foreach ($pid as $id => $items) {
    echo $id . ' = ' . count($items) . '<br />';
    foreach ($items as $key1 => $item) {
        echo 'size of ' . $key1 . ' = ' . count($item) . ' <br />';
    }
}
?>

The result:

id = 2
size of 098 = 2
size of 099 = 1

Upvotes: 1

santosh
santosh

Reputation: 787

This Gives You desire Output..

       echo 'ID ='.count($pid['id']);
        foreach ($pid['id'] as $key => $res) {
          echo 'size of ' .$key.'= ' .count($res);
         }

Output

ID =2

size of 098= 2

size of 099= 1

Upvotes: 1

Related Questions