Skip to main content

PHP Arrays

An array stores multiple values in one single variable:


Example

<?php
$cars = array("Volvo""BMW""Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>



What is an Array?

An array is a special variable, which can hold more than one value at a time.
If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this:
$cars1 = "Volvo";
$cars2 = "BMW";
$cars3 = "Toyota";
However, what if you want to loop through the cars and find a specific one? And what if you had not 3 cars, but 300?
The solution is to create an array!
An array can hold many values under a single name, and you can access the values by referring to an index number.

Create an Array in PHP

In PHP, the array() function is used to create an array:
array();



Get The Length of an Array - The count() Function

The count() function is used to return the length (the number of elements) of an array:

Example

<?php
$cars = array("Volvo""BMW""Toyota");
echo count($cars);
?>



PHP Indexed Arrays...........



PHP Indexed Arrays

There are two ways to create indexed arrays:
The index can be assigned automatically (index always starts at 0), like this:
$cars = array("Volvo", "BMW", "Toyota");
or the index can be assigned manually:
$cars[0] = "Volvo";
$cars[1] = "BMW";
$cars[2] = "Toyota";
The following example creates an indexed array named $cars, assigns three elements to it, and then prints a text containing the array values:


Example

<?php
$cars = array("Volvo""BMW""Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>


Loop Through an Indexed Array

To loop through and print all the values of an indexed array, you could use a for loop, like this:


Example

<?php
$cars = array("Volvo""BMW""Toyota");
$arrlength = count($cars);

for($x = 0; $x < $arrlength; $x++) {
    echo $cars[$x];
    echo "<br>";
}
?>




PHP Associative Arrays.......




PHP Associative Arrays

Associative arrays are arrays that use named keys that you assign to them.
There are two ways to create an associative array: 
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
or:
$age['Peter'] = "35";
$age['Ben'] = "37";
$age['Joe'] = "43";
The named keys can then be used in a script:

Example

<?php
$age = array("Peter"=>"35""Ben"=>"37""Joe"=>"43");
echo "Peter is " . $age['Peter'] . " years old.";
?>



Loop Through an Associative Array

To loop through and print all the values of an associative array, you could use a foreach loop, like this:


Example

<?php
$age = array("Peter"=>"35""Ben"=>"37""Joe"=>"43");

foreach($age as $x => $x_value) {
    echo "Key=" . $x . ", Value=" . $x_value;
    echo "<br>";
}
?>




PHP Multidimensional Arrays.....



PHP - Two-dimensional Arrays

A two-dimensional array is an array of arrays (a three-dimensional array is an array of arrays of arrays).
First, take a look at the following table:
NameStockSold
Volvo2218
BMW1513
Saab52
Land Rover1715
We can store the data from the table above in a two-dimensional array, like this:
$cars = array
  (
  array("Volvo",22,18),
  array("BMW",15,13),
  array("Saab",5,2),
  array("Land Rover",17,15)
  );
Now the two-dimensional $cars array contains four arrays, and it has two indices: row and column.
To get access to the elements of the $cars array we must point to the two indices (row and column):

Example

<?php
echo $cars[0][0].": In stock: ".$cars[0][1].", sold: ".$cars[0][2].".<br>";
echo $cars[1][0].": In stock: ".$cars[1][1].", sold: ".$cars[1][2].".<br>";
echo $cars[2][0].": In stock: ".$cars[2][1].", sold: ".$cars[2][2].".<br>";
echo $cars[3][0].": In stock: ".$cars[3][1].", sold: ".$cars[3][2].".<br>";
?>


We can also put a for loop inside another for loop to get the elements of the $cars array (we still have to point to the two indices):



Example

<?php
for ($row = 0; $row < 4; $row++) {
  echo "<p><b>Row number $row</b></p>";
  echo "<ul>";
  for ($col = 0; $col < 3; $col++) {
    echo "<li>".$cars[$row][$col]."</li>";
  }
  echo "</ul>";
}
?>





PHP Sorting Arrays..........


PHP - Sort Functions For Arrays

In this chapter, we will go through the following PHP array sort functions:
  • sort() - sort arrays in ascending order
  • rsort() - sort arrays in descending order
  • asort() - sort associative arrays in ascending order, according to the value
  • ksort() - sort associative arrays in ascending order, according to the key
  • arsort() - sort associative arrays in descending order, according to the value
  • krsort() - sort associative arrays in descending order, according to the key

Sort Array in Ascending Order - sort()

The following example sorts the elements of the $cars array in ascending alphabetical order:

Example

<?php
$cars = array("Volvo""BMW""Toyota");
sort($cars);
?>


The following example sorts the elements of the $numbers array in ascending numerical order:


Example

<?php
$numbers = array(4622211);
sort($numbers);
?>




Sort Array in Descending Order - rsort()

The following example sorts the elements of the $cars array in descending alphabetical order:

Example

<?php
$cars = array("Volvo""BMW""Toyota");
rsort($cars);
?>
The following example sorts the elements of the $numbers array in descending numerical order:

Example

<?php
$numbers = array(4622211);
rsort($numbers);
?>



Sort Array (Ascending Order), According to Value - asort()

The following example sorts an associative array in ascending order, according to the value:

Example

<?php
$age = array("Peter"=>"35""Ben"=>"37""Joe"=>"43");
asort($age);
?>


Sort Array (Ascending Order), According to Key - ksort()

The following example sorts an associative array in ascending order, according to the key:


Example

<?php
$age = array("Peter"=>"35""Ben"=>"37""Joe"=>"43");
ksort($age);
?>





Sort Array (Descending Order), According to Value - arsort()

The following example sorts an associative array in descending order, according to the value:



Example

<?php
$age = array("Peter"=>"35""Ben"=>"37""Joe"=>"43");
arsort($age);
?>



Sort Array (Descending Order), According to Key - krsort()

The following example sorts an associative array in descending order, according to the key:



Example

<?php
$age = array("Peter"=>"35""Ben"=>"37""Joe"=>"43");
krsort($age);
?>





Comments

Popular

Privacy Policy of Dream Cash

Privacy Policy INTISAR DEVELOPER built the Dream Cash app as a Free app. This SERVICE is provided by INTISAR DEVELOPER at no cost and is intended for use as is. This page is used to inform visitors regarding my policies with the collection, use, and disclosure of Personal Information if anyone decided to use my Service. If you choose to use my Service, then you agree to the collection and use of information in relation to this policy. The Personal Information that I collect is used for providing and improving the Service. I will not use or share your information with anyone except as described in this Privacy Policy. The terms used in this Privacy Policy have the same meanings as in our Terms and Conditions, which is accessible at Dream Cash unless otherwise defined in this Privacy Policy. Information Collection and Use For a better experience, while using our Service, I may require you to provide us with certain personally identifiable information. The information that I ...

PHP Constants

PHP Constants A constant is an identifier (name) for a simple value. The value cannot be changed during the script. A valid constant name starts with a letter or underscore (no $ sign before the constant name). Note:  Unlike variables, constants are automatically global across the entire script. Create a PHP Constant To create a constant, use the  define()  function. Syntax define( name ,  value ,  case-insensitive ) Parameters: name : Specifies the name of the constant value : Specifies the value of the constant case-insensitive : Specifies whether the constant name should be case-insensitive. Default is false Example Create a constant with a  case-sensitive  name: <?php define( "GREETING" ,  "Welcome to W3Schools.com!" ); echo  GREETING; ?> Example Create a constant with a  case-insensitive  name: <?php define( "GREETING" ,  "Welcome to W3Schools.com!" , true); echo  greeting; ?> PHP Constant Arrays In PHP7, ...

Earning App

App Details User App details: 1.Sign Up - name, number, password,refer (Phone number verification) 2.Login - Number and Password 4.Multiple Account not supported 5. Dashboard - Point, Number, Today Claim, Invalid Acitvity 6.Quiz cliam reward after 20 quiz. 7. Click on ad after a amount of time.(as you want) 8.Payment History (own) 9.Payment History (All user) 10.Your social links (Telegram, Facebook etc.) 11.Push Notification 12.Automatically Back 13.Automatically back by tracking  VPN Servise . Motet things will be added if you want. Admin App details 1.See all user 2.Edit Users data(Name, Number, point, inavild activity count etc.) 3.Block or Unblock 4.Payment Requests 5.Send Push Notifications 6.Change Admin password. Our app supported only this company ads - 1.Admob Order Now Our contact Number : +8801533471179 Imo/Telegram/What'sApp +8801533471179 Our email - fedatoapps@gmail.com USER APP PIC<{[<>]}>   ...