Читать книгу PHP Programming for Beginners. Key Programming Concepts. How to use PHP with MySQL and Oracle databases (MySqli, PDO) - Sergey D Skudaev - Страница 11

PHP Array Functions

Оглавление

Arrays are used in any programming language. You can imagine an array as a long box with many identical compartments. Visualize it like this: |___|___|___|___|___|.

Whatever you place in a compartment is that particular compartment’s value. Let’s place the characters a, b, c, d and e in the following array of compartments: |_a_|_b_|_c_|_d_|_e_|

Now, to access any of these values, you’ll need to know which compartment the value is stored in. For example, ‘b’ is stored in the second compartment.

In most computer languages, array index counting starts from 0, not 1, meaning that the index of the first element of the array is 0, the index of the second element of the array is 1 and so on. In the following array of names, you see the indexes and values:

//declare an array of names


$names=array ();


$names [0] =“John”;

$names [1] =“George”;

$names [2] =“James”;

$names [3] =“Anna”;

$names [4] =“Robert”;

$names [5] =“John”;

$names [6] =“James”;

$names [7] =“George”;

$names [8] =“Maria”;

$names [9] =“Peter”;

$names [10] =“James”;


To display “Anna” you have to access the element (compartment) with the index 3. If you use the print command,


print ($names [3]);


The built-in function sizeof (array) returns the number of elements in an array. In this case,


$asize=sizeof ($names);


Or you can use the count function


$asize=count ($names);


To display all array values, we can use the for loop.

The for loop looks like this:


for ($i=0; $i <$asize; $i++)


print ($names [$i].”<br>”);


In the for loop, the $i variable is incremented from 0 to the value one less than the $asize value. If the array has 11 elements, the $sizeof () function will return 11. The $i variable will be incremented to 10 and then stops. You might assume that if $i stops at 10, and we have 11 elements, the last array element would not be displayed.

However, that assumption would be wrong because the first array element index is 0, the eleventh element will have index 10, and so our code will display the element index 10, which is the 11th element of the array.

There are many useful built-in functions for arrays. The code below demonstrates some of these:

The in_array () function returns true if the value exists in an array.

So, in the above array of names, in_array (“Anna’) will return True, but in_array (“Lidia’) will return False.

The sort function sorts an array in ascending order.

The rsort function sorts an array in reverse order.

The array_pop function removes and returns the last element of an array.

The array_push function adds an element to the end of array.

The array_unique function removes duplicate values from an array.

The print_r function prints an array.

The array_rand function selects a random array element.

The implode function converts an array into a string.


<?php

print (”<br> The sort function sorts array <br>”);


sort ($names);


//Get size of array

$asize=sizeof ($names);


for ($i=0; $i <$asize; $i++) {

//Check if it is female name put Mrs. prefix

//else put Mr. prefix


if (($names [$i] ==“Anna”) || ($names [$i]

==“Maria”))

{

print (“Hello Mrs.”. $names [$i].”<br>”);

}

else

{

print’ Hello Mr.”. $names [$i].”<br>”);

}


} //for


print (”<br>”);

?>


Output:

The sort function sorts an array

Hello Mrs. Anna

Hello Mr. George

Hello Mr. George

Hello Mr. James

Hello Mr. James

Hello Mr. James

Hello Mr. John

Hello Mr. John

Hello Mrs. Maria

Hello Mr. Peter

Hello Mr. Robert


<?php

echo “The array_unique function removes duplicate array values <br>”;


$array=array ();


$array=array_unique ($names);


foreach ($array as $key => $value) {


echo $key. "-”. $value. "<br>”;

}

print (”<br>”);

?>


Output:

The array_unique function removes duplicate values.

0-Anna

1-George

3-James

6-John

8-Maria

9-Peter

10-Robert


<?php


rsort ($array);


print (“The rsort function sorts an array in reverse order <br>”);


foreach ($array as $key => $value) {

echo $key. "-”. $value. "<br>”;

}

?>

Output:

The rsort function sorts an array in reverse order

0-Robert

1-Peter

2-Maria

3-John

4-James

5-George

6-Anna


<?php


print (”<br> The array_pop () functions returns the last element. <br>”);

$lastelement=array_pop ($array);


print (”<br> The last element=”. $lastelement.”<br>”);

?>


Output:

The array_pop () function returns the last element

The last element=Anna


print (”<br> Array after calling the array_pop ().

The last element removed. <br> <br>”);


foreach ($array as $key => $value) {


echo $key. "-”. $value. "<br>”;

}


Output:

Array after calling array_pop (): The last element removed

0-Robert

1-Peter

2-Maria

3-John

4-James

5-George


The array_push function adds elements to the end of an array.

The print_r prints array function prints array key – value pairs.


<?php


array_push ($array, “Chris”, “Colin”);

print_r ($array);


?>

Array after calling array_push ($array, “Chris”, “Colin”)

and print_r functions: Chris and Colin are added to the end of the array.


Output:

Array ([0] => Robert [1] => Peter [2] => Maria [3] => John [4] => James [5] => George [6] => Chris [7] => Colin)


The array_rand ($array) function returns random array index.


$random=array_rand ($array);


print (”<br> print array element by random

index <br>”);


print (”<br> Random element=”. $array [$random].”<br>”);


Output:

print array element by random index

Random element=Colin


<?php

$string=implode ($array);

print (”<br> Array is imploded in a string: <br>”);

print ($string);

?>


Array is imploded in a string:

RobertPeterMariaJohnJamesGeorgeChrisColin

PHP Programming for Beginners. Key Programming Concepts. How to use PHP with MySQL and Oracle databases (MySqli, PDO)

Подняться наверх