Detri Amelia Chandra
  • HOME
  • GIS
  • HTML
  • Bootstrap
  • CSS
  • Javascript
  • Ubuntu
  • Useful Tools
    • URL Redirect Tool
    • URL Download Encryptor
No Result
View All Result
Detriamelia.com
  • HOME
  • GIS
  • HTML
  • Bootstrap
  • CSS
  • Javascript
  • Ubuntu
  • Useful Tools
    • URL Redirect Tool
    • URL Download Encryptor
No Result
View All Result
Detriamelia.com
No Result
View All Result

11. PHP Array Example, Complete Editions

17/01/2020
in PHP, Programming
PHP Array Example, Complete Editions

PHP Array Example, Complete Editions

Welcome back to the PHP programming learning page. This time we will discuss arrays, more precisely arrays in PHP. Arrays in PHP are actually almost similar to arrays in javascript. But there are a few fundamental differences. For more details, please follow the tutorial article below.

PHP Array Introduction

An array stores multiple values in one single variable:

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

What is An Array?

An exhibit is an exceptional variable, which can hold more than each incentive in turn.
On the off chance that you have a rundown of things (a rundown of vehicle names, for instance), putting away the autos in single factors could resemble this:

1
2
3
$cars1 = "Volvo";
$cars2 = "BMW";
$cars3 = "Toyota";

Be that as it may, imagine a scenario where you need to circle through the vehicles and locate a particular one. Furthermore, imagine a scenario in which you had not 3 vehicles, however 300.
The arrangement is to make a cluster!
A cluster can hold numerous qualities under a solitary name, and you can get to the qualities by alluding to a record number.

Make an Array in PHP

In PHP, the array() work is utilized to make a cluster array();.
In PHP, there are three types of arrays:

  • Indexed arrays – Arrays with a numeric index
  • Associative arrays – Arrays with named keys
  • Multidimensional arrays – Arrays containing one or more arrays

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:

1
2
3
4
<?php
$cars = array("Volvo", "BMW", "Toyota");
echo count($cars);
?>

PHP Indexed Arrays

There are two ways to create indexed arrays. The index can be assigned automatically (index always starts at 0), like this:

1
$cars = array("Volvo", "BMW", "Toyota");

or the index can be assigned manually:

1
2
3
$cars[0] = "Volvo";
$cars[1] = "BMW";
$cars[2] = "Toyota";

The accompanying model makes a recorded cluster named $cars, allots three components to it, and afterward prints a book containing the exhibit esteems:

1
2
3
4
<?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 every one of the estimations of a listed cluster, you could utilize a for circle, this way:

1
2
3
4
5
6
7
8
<?php
$cars = array("Volvo", "BMW", "Toyota");
$arrlength = count($cars);
for($x = 0; $x < $arrlength; $x++) {
    echo $cars[$x];
    echo "<br>";
}
?>

PHP Associative Arrays

Affiliated clusters are exhibits that utilization named keys that you appoint to them.
There are two different ways to make an affiliated exhibit:

1
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");

or :

1
2
3
$age['Peter'] = "35";
$age['Ben'] = "37";
$age['Joe'] = "43";

The named keys can then be used in a script:

1
2
3
4
<?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:

1
2
3
4
5
6
7
<?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

In the previous pages, we have described arrays that are a single list of key/value pairs.
However, sometimes you want to store values with more than one key. For this, we have multidimensional arrays.

PHP – Multidimensional Arrays

A multidimensional array is an array containing one or more arrays.
PHP supports multidimensional arrays that are two, three, four, five, or more levels deep. However, arrays more than three levels deep are hard to manage for most people.
The dimension of an array indicates the number of indices you need to select an element.

  • For a two-dimensional array you need two indices to select an element
    For a three-dimensional array you need three indices to select an element

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:

Name Stock Sold
Volvo 22 18
BMW 15 13
Saab 5 2
Land Rover 17 15

We can store the data from the table above in a two-dimensional array, like this:

1
2
3
4
5
6
7
$cars = array
  (
  array("Volvo",22,18),
  array("BMW",15,13),
  array("Saab",5,2),
  array("Land Rover",17,15)
  );

Presently the two-dimensional $cars exhibit contains four clusters, and it has two lists: line and section.
To gain admittance to the components of the $cars cluster we should highlight the two lists (line and section):

1
2
3
4
5
6
<?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):

1
2
3
4
5
6
7
8
9
10
<?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

The components in a cluster can be arranged in sequential order or numerical request, plummeting or rising.

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:

1
2
3
4
<?php
$cars = array("Volvo", "BMW", "Toyota");
sort($cars);
?>

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

1
2
3
4
<?php
$numbers = array(4, 6, 2, 22, 11);
sort($numbers);
?>

Sort Array in Descending Order – rsort()

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

1
2
3
4
<?php
$cars = array("Volvo", "BMW", "Toyota");
rsort($cars);
?>

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

1
2
3
4
<?php
$numbers = array(4, 6, 2, 22, 11);
rsort($numbers);
?>

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

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

1
2
3
4
<?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:

1
2
3
4
<?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:

1
2
3
4
<?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:

1
2
3
4
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
krsort($age);
?>

Hope this article can be useful for all of us. Thank you for reading…

Tags: PHPProgramming
ShareTweetShare

Related Posts

Learn Python Basics for Beginners: Easy Ways to Understand the Python Programming Language
Python

Learn Python Basics for Beginners: Easy Ways to Understand the Python Programming Language

Python is a high-level programming language that can be used to create a wide range of applications. Python was created...

09/08/2023
Database Table Relations Using Join Operations and the Classical Model
Belajar PHP

Database Table Relations Using Join Operations and the Classical Model

MySQL is a highly popular relational database in today's programming world. With features that allow for table-to-table relationships, MySQL databases...

09/08/2023
PHP Forms - Validation With Required Fields
PHP

14. PHP Forms – Validation With Required Fields

Hi everyone, this time we will discuss about PHP Validation using the Required Field. Not many people know, it turns...

15/02/2020
PHP Form Handling
PHP

13. New Performing PHP Form Handling

Detriamelia.Com - Running form functions in PHP is not difficult. By using the $ _POST and $ _GET methods it...

13/02/2020
Next Post
Learn PHP Global Variables - Superglobals With Example

12. Learn PHP Global Variables - Superglobals With Example

PHP Form Handling

13. New Performing PHP Form Handling

Leave a Reply Cancel reply

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

Recommended

PHP Syntax, Variables and PHP Echo/Print

5. PHP Echo and Print Statements

29/11/2019
Application of Jquery Mask to Numbers in HTML and PHP Forms

Application of Jquery Mask For Numbers in HTML and PHP Forms

23/04/2019
Understanding PHP Condition If Else, Elseif Statements

7. Understanding PHP Condition If Else, Elseif Statements

15/01/2020
Adding Modern and Beautiful Link Effects with CSS3

Adding Modern and Beautiful Link Effects with CSS3

31/07/2019

Instagram Feed

    The Instagram Access Token is expired, Go to the Customizer > JNews : Social, Like & View > Instagram Feed Setting, to refresh it.
  • HOME
  • GIS
  • HTML
  • Bootstrap
  • CSS
  • Javascript
  • Ubuntu
  • Useful Tools
Detriamelia.Com

© 2022 Detri Amelia Chandra - IT Tips and Programming Tutorials.

No Result
View All Result
  • HOME
  • GIS
  • HTML
  • Bootstrap
  • CSS
  • Javascript
  • Ubuntu
  • Useful Tools
    • URL Redirect Tool
    • URL Download Encryptor