2

I'm new to php and its developing . I declared php array:

<?php

     $chk_group[] =array(
         '1' => 'red',
         '2' => 'thi',
         '3' => 'aaa',
         '4' => 'bbb',
         '5' => 'ccc'      
     );

     var_dump($chk_group);

     //continue for loop
     for ($i = 0 ; $i < count($chk_group); $i++) {
         echo count($chk_group);
     }

 ?>

here i'm getting count = 1 please help me to get count of array.

1
  • 3
    In using $chk_group[] = array(...) rather than $chk_group = array(...), you've made $chk_group a multi-dimensional array, with a single entry at the top level Commented Dec 20, 2014 at 15:06

4 Answers 4

3

You have created a multi dimentional array by your this assigment

$chk_group[] = array(
         '1' => 'red',
         '2' => 'thi',
         '3' => 'aaa',
         '4' => 'bbb',
         '5' => 'ccc' 
     );

can you try without the brackets as :

$chk_group = array(
         '1' => 'red',
         '2' => 'thi',
         '3' => 'aaa',
         '4' => 'bbb',
         '5' => 'ccc' 
      );
Sign up to request clarification or add additional context in comments.

Comments

2

You need to change $chk_group[] to $chk_group in your first line.

In PHP syntax, $chk_group[] = means push the right had value to an array called $chk_group. Your entire array is being stored to $chk_group[0]

What you need instead is:

 $chk_group[] =array(
     '1' => 'red',
     '2' => 'thi',
     '3' => 'aaa',
     '4' => 'bbb',
     '5' => 'ccc' 
 );

Comments

2

try

count($chk_group[0]);

or

$chk_group =array('1' => 'red',
                           '2' => 'thi',
         '3' => 'aaa',
         '4' => 'bbb',
         '5' => 'ccc' 



     );

 count($chk_group);

Comments

1

As mentioned in the answers, you need to remove the extra [] sign, so that assignment in front of the = sign, is recognized as the variable. With this syntax, you say that first element of your array, is another array.

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.