Lesson 10: Inbuilt and user defined functions:array_shift() – PHP

From lesson 9, we did learn how to use explode() function. The return type of this function is an array and we did print the array elements in lesson .

array_shift() removes the first element in the and reassigns the indices.

Syntax:

array_shift(array)

array Required parameter. Specifies an array to be shifted

This function returns the value of the removed element from an array, or NULL if the array is empty

See below

<?php

$mystring = “*188*1*2*”;

$result = explode(“*”,$mystring);

echo $result;

for ($i = 0; $i < count($result); ++$i) {
echo $result[$i] . " ";
}

echo array_shift( $result);?> 

The reason why we are doing array_shift() will be precisely known when we start on principals of USSD

Count ()

This function is used to count elements in a data structure. In this our case, this function will count and return the number of elements in an array.
  
  

<?php

$mystring = “*188*1*2*”;

$result = explode(“*”,$mystring);

echo $result;

echo count($result);// see what count(array) returns

?> 

I hope you enjoyed this lesson. Leave your comments or additions.

 

3 Comments

Add a Comment

Your email address will not be published. Required fields are marked *