1

if I have an array like this:

$array = array(
     1,
     2,
     3,
     ["thing1", "thing2", "thing3"]
);

how do I check what the lenght is of the array in the array ( ["thing1", "thing2", "thing3"] )?

2
  • $length = array_length($array[3]); Commented Feb 29, 2016 at 10:53
  • Is your array always in this format? Is the nested array always the last element? Can there be more elements within the array before it? There's a lot of variables that could come into play that will stop all the solutions provided so far from working. Commented Feb 29, 2016 at 10:54

3 Answers 3

9

You can use Array Count function count or sizeof function.

try below code:

$array = array(
    1,
    2,
    3,
    ["thing1", "thing2", "thing3"]
);

echo count($array[3]); //out put 3
echo sizeof($array[3]); //out put 3
Sign up to request clarification or add additional context in comments.

1 Comment

Out of the two count is probably preferable over sizeof if you do go with this method, sizeof is just an alias of count, and can cause confusion for some programmers since it doesn't do what many expect it to.
3

You can use the count function:

echo count($array[3]);

However if the array you want to get the length is not always in this same position you could do something like this:

foreach ($array as $element) {
    if (is_array($element) {
        echo count($element);
    }
}

Comments

0

you have to use count function here.

$array = array(
    1,
    2,
    3,
    ["thing1", "thing2", "thing3"]
);
echo count($array[3]); //out put 3

Though you can use sizeof but again sizeof() is an alias for count(), they work the same.

You may use sizeof like this..

echo sizeof($array[3]); //out put 3

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.