Monthly Archives: May 2012

array_chunk ( ) – Split an array into specified size chunks

The array_chunk() PHP array function will chunk an array at a specified size. It takes up to three parameters. The first parameter we feed it is the target array. The second parameter is the size you wish your chunks to be. And the third parameter is optional. Parameter three uses values of either “true” or “false”, when used you can choose to preserve existing keys in the array. If you do not use the third parameter this function defaults to reindexing the array just as if you specify “false” for the third parameter. Specify a value of “true” as the third parameter if you wish to preserve the keys in the array, and use “false”(or omit param 3) to reindex the array.

Example

 <?php $array = array("1","2","3","4","5","6","7","8","9"); ?>
<?php $values = array_chunk($array, 4); // inbuilt function (PHP 4 >= 4.2.0, PHP 5) ?>
<?php foreach($values as $val): ?>
<div>
<ul>
<?php foreach($val as $v): ?>
<li><?php echo $v; ?></li>
<?php endforeach; ?>
</ul>
</div>
<?php endforeach; ?>

Output

   . 1
   . 2
   . 3
   . 4

   . 5
   . 6
   . 7
   . 8

   . 9

array_push() –php array function

Explanation

The “array_push” function is used insert one or more elements onto the end of array.

Syntax:

    array_push(array,value1,value2...)

In the above syntax “array” specifies the array to which the values in “value1” is to be added, “value2” is optional.

Example

    <?php
    $b=array("c"=>"Cherry","b"=>"Strawberry");
    print_r(array_push($b, Orange, Guava));
    ?>

Result:

    Array ( [c]=> Cherry,[b]=> Strawberry, [0]=> Orange, [1]=> Guava);

In the above example even though the array already has string keys, the added elements will have numerical keys.

count() and sizeof() – php array function to count the number of items in an array

count — Count all elements in an array, or something in an object.

According to the website, sizeof is an alias of count, so they should be running the same code. Perhaps sizeof has a little bit more lag because it needs to resolve it to count? It should be very minimal though.

Example

<?php
$myArray = array("hen","tiger","bear");
$count = count($myArray);
echo $count;
?>

Output

3

range() — php array function

The range() array function in PHP will create a new array using a specified range. You can use letter or number ranges.The range() function creates an array containing a range of elements.This function returns an array of elements from low to high.

Syntax:

array range ( int low, int high [, int step])

Example

  
$questions = range(1, 10, 2);
 // gives 1, 3, 5, 7, 9

 $questions = range(1, 10, 3)
 // gives 1, 4, 7, 10

 $questions = range(10, 100, 10);

list() php function

The list() function is used to assign values to a list of variables in one operation.

Syntax:

list(var1,var2...)

Example

<?php    $record = array(1 ,'amit', 'math');     
 list($roll, $name, $subject) = $record;     
 echo "roll is $roll<br>name is $name<br>subject is $subject"; 
?>

Output

roll is 1
name is amit
subject is math