PHP Array: Count or Sizeof

PHP array: count or sizeof?

I would use count() if they are the same, as in my experience it is more common, and therefore will cause less developers reading your code to say "sizeof(), what is that?" and having to consult the documentation.

I think it means sizeof() does not work like it does in C (calculating the size of a datatype). It probably made this mention explicitly because PHP is written in C, and provides a lot of identically named wrappers for C functions (strlen(), printf(), etc)

What is the difference between count and sizeof?

These functions are aliases, like mentioned -> http://php.net/manual/en/function.sizeof.php

Length array in array

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

About array, count, sizeof OR?

Use this:

$remove_empty = array_filter($your_array);  
print_r($remove_empty);

How to determine if an array has any elements or not?

You can use the count() or sizeof() PHP functions:

if (sizeof($result) > 0) {
echo "array size is greater than zero";
}
else {
echo "array size is zero";
}

Or you can use:

if (count($result) > 0) {
echo "array size is greater than zero";
}
else {
echo "array size is zero";
}

Checking for empty arrays: count vs empty

I generally use empty. Im not sure why people would use count really - If the array is large then count takes longer/has more overhead. If you simply need to know whether or not the array is empty then use empty.

Incorrect PHP sizeof or count

There is a world of difference between:

$array1 = array(
'layer1' => array(
'item' => array(
'0' => array(
'item-id' => 123123,
'item-name' => 'Bad Dog',
'category' => 'pets',
'link' => 'http://www.baddog.com/',
),
)
)
);

and:

$array2 = array(
'layer1' => array(
'item' => array(
'item-id' => 123123,
'item-name' => 'Bad Dog',
'category' => 'pets',
'link' => 'http://www.baddog.com/',
)
)
);

Basically they are different structures, and PHP is correct to return the following:

count($array1['layer1']['item']);
/// = 1 (count of items in the array at that point)

count($array2['layer1']['item']);
/// = 4 (count of items in the array at that point)

If you wish to get a count for ['layer1']['item'] that makes sense to your app, you will always need ['layer1']['item'] to be an array containing multiple array structures... i.e. like $array1 above. This is the reason why I ask what is generating your array structure because - whatever it is - it is basically intelligently stacking your array data depending on the number of items, which is something you don't want.

Code that can cause an array to stack in this manner normally looks similar to the following:

/// $array = array(); /// this is implied (should be set elsewhere)

$key = 'test';
$newval = 'value';

/// if we are an array, add a new item to the array
if ( is_array($array[$key]) ) {
$array[$key][] = $newval;
}
/// if we have a previous non-array value, convert to an array
/// containing the previous and new value
else if ( isset($array[$key]) ) {
$array[$key] = array($array[$key],$newval);
}
/// if nothing is set, set the $newval
else {
$array[$key] = $newval;
}

Basically if you keep calling the above code, and tracing after each run, you will see the following structure build up:

$array == 'value';

then

$array == array(0 => 'value', 1 => 'value');

then

$array == array(0 => 'value', 1 => 'value', 2 => 'value');

It's the first step in this process that is causing the problem, the $array = 'value'; bit. If you modify the code slightly you can get rid of this:

/// $array = array(); /// this is implied (should be set elsewhere)
$key = 'test';
$newval = 'value';

/// if we are an array, add a new item to the array
if ( is_array($array[$key]) ) {
$array[$key][] = $newval;
}
/// if nothing is set, set the $newval as part of an subarray
else {
$array[$key] = array($newval);
}

As you can see all I've done is delete the itermediate if statement, and made sure when we discover no initial value is set, that we always create an array. The above will create a structure you can always count and know the number of items you pushed on to the array.

$array == array(0 => 'value');

then

$array == array(0 => 'value', 1 => 'value');

then

$array == array(0 => 'value', 1 => 'value', 2 => 'value');

update

Ah, I thought so. So the array is generated from XML. In this case I gather you are using a predefined library to do this so modifying the code is out of the question. So as others have already stated, your best bet is to use one of the many XML parsing libraries available to PHP:

http://www.uk.php.net/simplexml

http://www.uk.php.net/dom

When using these systems you retain more of an object structure which should be easier to count. Both the above also support xpath notation which can allow you to count items without even having to grab hold of any of the data.

update 2

Out of the function you've given, this is the part that is causing your arrays to stack in the manner that is causing the problem:

$children = array();
$first = true;
foreach($xml->children() as $elementName => $child){
$value = simpleXMLToArray($child,$attributesKey, $childrenKey,$valueKey);
if(isset($children[$elementName])){
if(is_array($children[$elementName])){
if($first){
$temp = $children[$elementName];
unset($children[$elementName]);
$children[$elementName][] = $temp;
$first=false;
}
$children[$elementName][] = $value;
}else{
$children[$elementName] = array($children[$elementName],$value);
}
}
else{
$children[$elementName] = $value;
}
}

The modification would be:

$children = array();
foreach($xml->children() as $elementName => $child){
$value = simpleXMLToArray($child,$attributesKey, $childrenKey,$valueKey);
if(isset($children[$elementName])){
if(is_array($children[$elementName])){
$children[$elementName][] = $value;
}
}
else{
$children[$elementName] = array($value);
}
}

That should stop your arrays from stacking... however if you have any other part of your code that was relying on the previous structure, this change may break that code.

Sizeof array in php not working in the for loop

You're creating an empty array and then counting the size of it. Why would you expect anything but a count of zero?

What you really want is the size of the que array:

$length = count($_POST['que']);

But that makes some your code unnecessary since this is already an array.

for($j=1; $j<$length; $j++) {
// $question = $_POST['que']; UNNECESSARY
if($_POST['que'][$j] != "") {
$bla .= $j.'This is good<br/><br/>';
}
}

Count Objects in PHP

From your example, using objects for this seems a very bloated method. Using a simple array would be much easier and faster.

This:

object(stdClass)#46 (3) {
["0"]=>
object(stdClass)#47 (1) {
["productid"]=>
string(2) "15"
}
}

Could just be this:

array(0 => 15);

Or even this:

array(15);

Your example only seems to be storing a product id, so you don't strictly need a key of "productid"

Is there any specific reason you need to use objects?

Is PHP's count() function O(1) or O(n) for arrays?

Well, we can look at the source:

/ext/standard/array.c

PHP_FUNCTION(count) calls php_count_recursive(), which in turn calls zend_hash_num_elements() for non-recursive array, which is implemented this way:

ZEND_API int zend_hash_num_elements(const HashTable *ht)
{
IS_CONSISTENT(ht);

return ht->nNumOfElements;
}

So you can see, it's O(1) for $mode = COUNT_NORMAL.



Related Topics



Leave a reply



Submit