1

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
8
  • 2
    You cannot have array with same keys. It is impossible. Commented Feb 14, 2016 at 8:28
  • @u_mulder why? i chose ways like this for my shopping cart bag, any other ways to do for that? Commented Feb 14, 2016 at 8:33
  • 1
    Think of it - if you have array with 2 same keys - what should be returned when you refer $pid['id']['098']? First value or second? Commented Feb 14, 2016 at 8:35
  • 1
    "098" => array(array(...), array(...)), this is one way of grouping the same "id" Commented Feb 14, 2016 at 8:36
  • @u_mulder thank's for the information dude! Commented Feb 14, 2016 at 8:39

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.