Image

PHP - PHP Arrays - PHP Arrays

PHP Arrays

PHP’s array manipulation API was redesigned in PHP 4.x to simplify common array manipulation tasks. New objects designed specifically for array iteration were introduced in PHP 5.x as part of the Standard PHP.

Library (SPL) to make array manipulation even more extensible and customizable. The result is a sophisticated toolkit that enables you to easily perform complex tasks, including recursively traversing and searching a series of nested arrays, sorting arrays by more than one key, filtering array elements by user-defined criteria, and swapping array keys and values.


Index Array

Array indexing refers to any use of the square brackets ([]) to index array values. There are many options to indexing, which give numpy indexing great power, but with power comes some complexity and the potential for confusion. This section is just an overview of the various options and issues related to indexing. Aside from single element indexing, the details on most of these options are to be found in related sections.

Single element indexing for a 1-D array is what one expects. It work exactly like that for other standard Python sequences. It is 0-based, and accepts negative indices for indexing from the end of the array.

>>>
>>> x = np.arange(10)
>>>x[2]
2
>>>x[-2]
O/P
8

Associative Arrays

The associative arrays are very similar to numeric arrays in term of functionality but they are different in terms of their index. Associative array will have their index as string so that you can establish a strong association between key and values.

To store the salaries of employees in an array, a numerically indexed array would not be the best choice. Instead, we could use the employees names as the keys in our associative array, and the value would be their respective salary.

<html>
<body>

<?php
/* First method to associate create array. */
         $salaries =array("mohammad"=>2000,"qadir"=>1000,"zara"=>500);

echo"Salary of mohammad is ". $salaries['mohammad']."<br />";
echo"Salary of qadir is ".  $salaries['qadir']."<br />";
echo"Salary of zara is ".  $salaries['zara']."<br />";

/* Second method to create array. */
         $salaries['mohammad']="high";
         $salaries['qadir']="medium";
         $salaries['zara']="low";

echo"Salary of mohammad is ". $salaries['mohammad']."<br />";
echo"Salary of qadir is ".  $salaries['qadir']."<br />";
echo"Salary of zara is ".  $salaries['zara']."<br />";
?>

</body>
</html>
This will produce the following result
Salary of mohammad is 2000
Salary of qadir is 1000
Salary of zara is 500
Salary of mohammad is high
Salary of qadir is medium
Salary of zara is l>ow