WarDrome Blog

PHP Guide: handling arrays

PHP Guide: handling arrays

Arrays, as mentioned in previous posts, are nothing but containers containing various pairs of keys => values. PHP provides us with some native functions to manage, sort, remove items and so on.

count($array [$mode = COUNT_NORMAL])

The count will allow us to count the elements in an array. The count starts from 1(not at 0). Let’s analyze this example:

$array = array("books", "pens", "pencils");
echo count($array) // return the result "3"

An array, as you can well imagine, can also be nested, and PHP provides us with the second parameter to count recursively.

$array = array(
   0 => "books",
   1 => "pens"
   2 => array(
     0 => "pencils"
     1 => "paper",
     3 => "markers"
   )
);

echo count($array, COUNT_RECURSIVE) // will return 6, will also count as nested elements
                                     // The array with key "2".
                                     // Note that this type of array is also called 'multidimensional'

array_reverse($array [bool $keeypkeys])

The function array_reverse we reverse the order of the array, the second parameter will be used in case we need to keep the same keys of origin. We see a small example to better understand:

$array = array(
   0 => "books",
   1 => "pens"
   2 => "pencils"
);
var_dump(array_reverse($array));

The example mentioned above will return the result:

Array(
   0 => "pencils"
   1 => "pens"
   2 => "books"
)

However, if you specify the second parameter:

$array = array(
   0 => "books",
   1 => "pens"
   2 => "pencils"
);
var_dump(array_reverse($array, true));

The result will be:

Array(
  2 => "pencils"
  1 => "pens"
  0 => "books"
)

sort(array $array,[int $sort_flags])

Sort allows us to order a given array according to a predetermined flag(specified in the parameter $sort_flags). If the second parameter is not supplied, alphabetic sorting mode is incremental.

$array = array(
   0 => "books",
   1 => "pens"
   2 => "pencils"
);
sort($array);
var_dump($array);

That will return:

Array(
   0 => "books",
   1 => "pencils"
   2 => "pens"
)

rsort($array ,[$sort_flags int])

Similar to the sort() function except that the array will be sorted in descending order.

asort($array ,[$sort_flags int])

This function is also similar to the sort() but will keep the keys of the array provided. Here’s an example:

$array = array(
   0 => "books",
   1 => "pens"
   2 => "pencils"
);
asort($array);
var_dump($array);

The above example will print:

Array(
  0 => "books",
  2 => "pencils"
  1 => "pens"
)

arsort($array)

Rsort Same as(in descending order), but maintains the same keys of the array.

in_array(mixed $needle, array $haystack, [bool $strict = FALSE])

in_array is a very useful feature when you want to search for a value($needle) in an array($haystack). Let’s analyze this example:

$array = array(
   0 => "books",
   1 => "pencils"
   2 => "pens"
);
if(in_array("books",$array)) {
   echo "The array contains the value of books!"
}

The result of course will be “The array contains the value: books”, but if we specify the third parameter(STRICT_MODE) we should remember that the search will be case sensitive.

$array = array(
   0 => "books",
   1 => "pencils"
   2 => "pens"
);
if(in_array("Books",$array, true)) {
   echo "The array contains the value: Books!"
else {
   echo "The array contains the value NO: Books!"
}

If you run the above example the result will always be “The array contains the value NO: Books!” and that’s because you specified a search case sensitive, so this is case sensitive.

array_key_exists(mixed $key,$array)

This feature, as you can imagine, we will search for a specific key provided in the first parameter $key, $array within the second parameter.

$array = array(
   0 => "books",
   1 => "pencils"
   2 => "pens"
);
if(array_key_exists(2,$array))
  echo "The key number 2 exists!"

array_search(mixed $needle,$array [STRICT_MODE bool = FALSE])

The function allows us to search for a particular value within an array, and we will return the key belonging to his relative.

$array = array(
  0 => "books",
  1 => "pencils"
  2 => "pens"
);

$key = array_search("pens",$array);

var_dump($key);

The example returns the result “2”, which will be the key to the value of belonging “pens”. By specifying the 3 rd parameter(Strict mode) the search will be case sensitive.

array_merge(array $array1,$array2,$array3 …)

Array_merge allows us to combine two or more arrays together. Note that if one of the arrays provided as “substitutes” contain a key that already exists in ‘$array1, it will be overwritten with the new.

$office = array(
  0 => "books",
  1 => "pencils"
  2 => "pens"
);

$fruits = array(
  3 => "banana",
  4 => "strawberry",
  5 => "cherry"
);

$result = array_merge($office,$fruits);

var_dump($result);

The example will return as a result:

Array(
  0 => "books",
  1 => "pencils"
  2 => "pens"
  3 => "banana",
  4 => "strawberry",
  5 => "cherry"
)

But instead we analyze the following example:

$office = array(
  0 => "books",
  1 => "pencils"
  2 => "pens"
);

$fruits = array(
  0 => "banana",
  1 => "strawberry",
  2 => "cherry"
);
$result = array_merge($office,$fruits);
var_dump($result);

The result will be:

Array(
  0 => "banana",
  1 => "strawberry",
  2 => "cherry"
)

This is because the keys 0, 1 and 2 were overridden by the 2 nd array provided!

array_pop($array)

Array_pop is used when you want to remove an item at the end of an array, preserving the value in a variable. Example:

$office = array(
  0 => "books",
  1 => "pencils"
  2 => "pens"
);
$pen = array_pop($office); // This variable will contain the string "pens"
var_dump($department);

The result will be:

Array(
  0 => "books",
  1 => "pencils"
)

array_push($array array, mixed$param1, [mixed$param2 …])

Array_push allows us to add new elements to the array provided in the first parameter.

$array = array(
  0 => "books",
  1 => "pencils"
  2 => "pens"
);
array_push($array, "Computer");
var_dump($array);

The result will be:

Array(
  0 => "books",
  1 => "pencils"
  2 => "pens"
  3 => "computer"
)

array_shift($array)

array_shift is similar to array_pop, but allows us to extract the first element of the array.

array_unshift(array $array,$var1, $var2…)

array_unshift is similar to array_push but it allows us to insert elements at the beginning of the array. Here’s an example:

$office = array(
  0 => "books",
  1 => "pencils"
  2 => "pens"
);
array_unshift($office "computer", "marker");
var_dump($department);

The result will be:

Array(
   0 => "computer",
   1 => "marker"
   2 => "books",
   3 => "pencils"
   4 => "pens"
)

Note that after using this function array keys will be reset and reallocated to allow new elements to be positioned correctly.

implode(string $glue,array $array)

implodes is a very used and serves to transform each element of an array into a string, separated by the first parameter supplied($glue). Example:

$office = array(
  0 => "books",
  1 => "pencils"
  2 => "pens"
);
$result = implode(",",$office); // will produce the result "books, pencils, pens," the comma is the separator
                                 // Supplied in the first parameter of the function

explode(string$delimiter, string$string)

is the inverse function of explode implode and allows us to combine a string separated by a specified delimiter, and turn it into an array.

$office = "books, pencils, pens";
$array = explode(",",$office);
var_dump($array);

Will produce:

Array(
  0 => "books",
  1 => "pencils"
  2 => "pens"
)

Even today we have finished our little lesson, in the next ones we will discuss about date strings. See you next tutorial!

Lascia un commento