PHP Powerful array functions

Source: Internet
Author: User
Tags array to string explode

<?php
/**
* Created by Phpstorm.
* user:63448
* DATE:2018/5/6
* time:21:56
*/
echo "echo "Definition of an array
$arr 1 = array ();
$arr 2 = [];

Array Assignment
$arr = Array (' A ', ' B ');
$arr = Array (' key1 ' = ' a ', ' key2 ' = ' B ');
Assigning a value to an array using the list function
List ($arr [], $arr [], $arr []) = [1, 2, 3];

/**
* Implode ()-Array to string
*/
function function: Convert the array to a string in a certain format.
$arr = Array (' Hello ', ' world! ', ' I ', ' love ', ' shanghai! ');
echo Implode ("", $arr);//arrays are joined with spaces and converted into strings
echo "/**
* Explode ()-String to array
*/

function function: Cut the string into an array in a certain format.
$STR = "Hello World". I Love shanghai! ";
Print_r (Explode ("", $str));//The string is cut in a space and converted to an array
echo "/**
* Array_multisort ()-Array sorting
*/
function function: Multiple arrays can be sorted at the same time, the associated key name remains unchanged, the numeric key name will be re-indexed.
Custom data
$data [] = Array (' volume ' = +, ' edition ' = 2);
$data [] = Array (' volume ' = +, ' edition ' = 1);
$data [] = Array (' volume ' = =, ' edition ' = 6);
$data [] = Array (' volume ' = = 98, ' edition ' = 2);
$data [] = Array (' volume ' = +, ' edition ' = 6);
$data [] = Array (' volume ' = +, ' edition ' = 7);
Get a list of columns
foreach ($data as $key = = $row) {
$volume [$key] = $row [' volume '];
$edition [$key] = $row [' Edition '];
}
The data is sorted in descending order by volume, and then in ascending order according to edition.
The $data as the last parameter, sorted by the Universal key
Array_multisort ($volume, Sort_desc, $edition, SORT_ASC, $data);
Print_r ($data);

echo "/**
* Array_column ()-Get array to specify a column
*/
function function: Gets the specified column of data according to the specified key.


An array that represents the possible recordset returned by the database
$a = Array (
Array (' id ' = = 5698, ' first_name ' = ' Bill ', ' last_name ' = ' Gates ',),
Array (' id ' = = 4767, ' first_name ' = ' Steve ', ' last_name ' = ' Jobs ',),
Array (' id ' = = 3809, ' first_name ' = ' Mark ', ' last_name ' = ' Zuckerberg ',)
);
Gets a column of last_name for the target array and copies it to the result array
$last _names = array_column ($a, ' last_name ');
Print_r ($last _names);
echo "/**
* Array_diff ()-Array subtraction Error set
*/
function function: Compare two arrays to find the difference set of two arrays.
$a 1=array ("a" = "red", "b" = "green", "c" = "Blue", "D" and "yellow");
$a 2=array ("e" = "red", "f" = "black", "G" and "purple");
$a 3=array ("a" and "Red", "B" and "Black", "h" = "yellow");
Save the difference set of the array to the result array
$result =array_diff ($a 1, $a 2, $a 3);
Print_r ($result);
echo "/**
* Array_flip ()-array key and value interchange position
*/
function function: The position of the key and the value in the array is swapped,
$a 1=array ("a" = "red", "b" = "green", "c" = "Blue", "D" and "yellow");
$result =array_flip ($a 1);
Print_r ($result);
echo "/**
* Array_intersect ()-intersection of two arrays
*/
function function: Compare the intersection of two data to figure out the same part of two arrays.

$a 1=array ("a" = "red", "b" = "green", "c" = "Blue", "D" and "yellow");
$a 2=array ("e" = "red", "f" = "green", "G" and "Blue");

$result =array_intersect ($a 1, $a 2);
Print_r ($result);
echo "/**
* Array_key_exists ()-Determine if the array key name exists
*/
function function: Determine whether the specified key name or index exists in the array, only one-dimensional array is applicable.
$a =array ("Volvo" = "XC90", "BMW" and "X5");
if (array_key_exists ("BMW", $a)) {
echo "Key exists! ";
} else {
echo "key does not exist! ";
}
echo "/**
* Array_merge ()-Merging arrays
*/
function function: Merges multiple data without merging elements of the same key value.
$a 1=array ("Red", "green");
$a 2=array ("Blue", "yellow");
Print_r (Array_merge ($a 1, $a 2));
echo "/**
* ARRAY_PAD ()-complement all group elements by setting
*/
function function: Set the function length, more than the minimum complement to ensure that the array length is consistent with the set, you can set the value of the supplementary element.
$a =array ("Red", "green");
Print_r (Array_pad ($a, ten, "Blue"));
echo "/**
* Array_pop ()-The last element of the array is out of the stack (delete)
*/
function function: Remove the last function of the array.
$a =array ("Red", "green", "blue");
Print_r (Array_pop ($a));//Print the deleted one
echo "/**
* ARRAY_PRODUCT ()-multiplication of elements within an array
*/
function function: Calculates the result of multiplying all the elements in the array, and the empty array returns 1.
$result = Array_product ([+]);
Print_r ($result);
echo "/**
* Array_sum ()-element addition in array
*/
function function: Calculates the result of adding all the elements in the array, and the empty array returns 0.
$result = Array_product ([+]);
Print_r ($result);
echo "/**
* Array_push ()-Array overlay elements
*/

function function: stack (stack) element to array, can be more than one.

$a =array ("Red", "green");
Array_push ($a, "blue", "yellow");
Print_r ($a);
echo "/**
* Array_search ()-Array Search key value
*/
function function: Searches the array for the specified value, and the search succeeds returns the key value of the first element.
$a =array ("a" and "Red", "b" = "green", "c" = "blue");
echo Array_search ("Red", $a);
echo "/**
* Array_shift ()-The first element of the array is out of the stack (delete)
*/
function function: The first element in the array is deleted and the first element is ejected.
$a =array ("a" and "Red", "b" = "green", "c" = "blue");
echo array_shift ($a). " ---";
Print_r ($a);
echo "echo "
?>

PHP Powerful array functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.