A detailed tutorial based on PHP array arrays

  • 2020-06-07 04:07:12
  • OfStack

Define an array
The array array is an ordered set of 1 variables, each of which is called an element.
1. Define an array
You can create a new array using the array() language structure. It accepts a fixed number of comma-separated key = > value parameter pairs.
array( [key = > ] value,... ) // key can be a number or a string // value can be any value
Example 1:

<?php 
$phpjc = array( 
=>'word', 
=>'excel', 
'outlook', 
'access'); 
print_r($phpjc); 
?>

The output results are as follows:

Array ( [0] => word [3] => excel [4] => outlook [5] => access ) 

Example 1 defines an array called phpjc, the value of the first element is: word, the second element is empty, the third element is: excel, and the fourth and fifth elements are generated automatically

You can create an empty array by assigning a variable an array() with no arguments, and then add the value using the square bracket [] syntax. (Note: The array_push() function can also be used to add values to the array!!)
Example 2:

<?php
$phpjc = array(); 
$phpjc[] = "one"; 
$phpjc[] = "two"; 
echo $phpjc[0]."<br>"; 
echo $phpjc[1]; 
?>

The output results are as follows:

one 
two 

2. Read array elements
Use the string index (or key) to access the values stored in the array
Example 3:

<?php
$phpjc = array("first"=>1,"second"=>2,"third"=>3); 
echo $phpjc["second"]; 
$phpjc["third"]=5; // The first 3 The value of each element is determined by" 3 "Change to" 5 "  
echo $phpjc["third"]; 
?>

There are many array related functions in PHP. 11 illustrates is_array(), n_array(), count(), array_push(), array_unshift(), array_merge(), array_pop(), array_shift(), sort().
1. is_array () function
Is it an array
-----------------------------------------------------------
2. in_array () function
If you have a large array of 1 and all you need to do is find a given value that exists, you can use in_array() to return true or false. The following code prints "Not found in this array" -- because you're looking for a non-existent "Alber" in $namesArray.

<?php
  $namesArray = array("Joe", "Jane", "Bob", "Mary", "Paul", "Eddie", "John");
  $lookingFor = "Albert"; 
  if (in_array($lookingFor, $namesArray)) {
    echo "You've found it!";
  } else {
    echo "Not found in this array!";
  }
?>

-----------------------------------------------------------
3. count () function
If you change the value of $lookingFor to "Mary", you get the message "You 've found it!" -- Because "Mary" is part 1 of the $namesArray.
If you want to count array elements, you can use the count() function:

<?php
  $namesArray = array("Joe", "Jane", "Bob", "Mary", "Paul", "Eddie", "John");
  $count = count($namesArray); 
?>

The value of $count will be 7.
-----------------------------------------------------------
4. array_push () function
You can add elements to any array, either at the beginning or the end of an existing array. You can also use the function to create a new array containing two or more array elements. When you merge, each array will be arranged in the desired order. If your array already has an internal sort, you need to resort the new merged array. Let's start by adding an element to the end of an existing array, using the function array_push() :

<?php
   /*  Create the original array  */
  $fruitArray = array("apple", "orange", "banana", "kiwi", "pear");
  /*  Add to the original array  */
  array_push($fruitArray, "grape", "pineapple", "tomato");
  /*  Each element is listed by its key value */
  while (list($key,$value) = each($fruitArray)) {
    echo "$key : $value<br>";
  }
?>

This will show:

apple
orange
banana
kiwi
pear
grape
pineapple
tomato

-----------------------------------------------------------
5. array_unshift () function
The code is very similar when you need to add elements to the beginning of an array. The only difference is the function name: array_unshift() instead of array_push().

<?php
   /*  Create the original array  */
  $fruitArray = array("apple", "orange", "banana", "kiwi", "pear");
  /*  Add to the original array  */
  array_unshift($fruitArray, "grape", "pineapple", "tomato");
  /*  Each element is listed by its key value */
  while (list($key,$value) = each($fruitArray)) {
    echo "$key : $value<br>";
  }
?>

This will show:

pineapple
tomato
apple
orange
banana
kiwi
pear

-----------------------------------------------------------
6. array_merge () function
The array_merge() function merges two or more arrays.

<?php
   /*  Create the original array  */
  $fruitArray = array("apple", "orange", "banana", "kiwi", "pear");
  /*  Create the first 2 An array  */
  $vegArray = array("carrot", "green beans", "asparagus", "artichoke", "corn");
  /*  Combined into 1 An array  */
  $goodfoodArray = array_merge($fruitArray, $vegArray);
  /*  Each element is listed by its key value */
  while (list($key,$value) = each($goodfoodArray)) {
    echo "$key : $value<br>";
  }
?>

This will show:

apple
orange
banana
kiwi
pear
carrot
green beans
asparagus
artichoke
corn

Now that you've added and merged elements to the array, let's practice removing element functions. You can use the function array_pop() to remove 1 element from the end of the 1 array. If you use the function array_shift(), remove 1 element from the beginning of the 1 array. In fact, when you remove an element from an array, that element is still available to you -- when you do pop or shift from an existing array.
-----------------------------------------------------------
7. array_pop () function
Remove 1 value from the end of the array using the array_pop() function:

<?php
  /*  create 1 An array of */
  $fruitArray = array("apple", "orange", "banana", "kiwi", "pear");
  /*  Pop a value at the end  */
  $popped = array_pop($fruitArray);
  /*  Lists the contents of the new array and the values that pop up */
  while (list($key,$value) = each($fruitArray)) {
    echo "$key : $value<br>";
  }
  echo "<br>and finally, in $popped: $popped";
?>

This will show:

apple
orange
banana
kiwi 
and finally, in $popped: pear 

-----------------------------------------------------------
8. array_shift () function
Next, remove a value from the end of the array:

<?php
  /*  create 1 An array of */
  $fruitArray = array("apple", "orange", "banana", "kiwi", "pear");
  /*  Remove a value from the header of an array  */
  $shifted = array_shift($fruitArray);
  /*  Lists the contents of the new array and the values removed */
  while (list($key,$value) = each($fruitArray)) {
    echo "$key : $value<br>";
  }
  echo "<br>and finally, in $shifted: $shifted";
?>

This will show:

orange
banana
kiwi
pear and finally, in $shifted: apple

-----------------------------------------------------------
9. sort () function
There are many functions that can help you sort the elements of an array. But I'm going to show you the basic sorting to help you understand how it works:

<?php
  /*  Create the original array  */
  $fruitArray = array("apple", "orange", "banana", "kiwi", "pear");
  /*  The sorting  */
  sort($fruitArray);
  /*  Reset it to display the array correctly from beginning to end  */
  /*  Each element is listed by its key value */
  while (list($key,$value) = each($fruitArray)) {
    echo "$key : $value<br>";
  }
?>

This will show:

Array ( [0] => word [3] => excel [4] => outlook [5] => access ) 
8

Related articles: