Home > Backend Development > PHP Tutorial > Lecture 3 of 'The Quickest to Understand PHP Programming': PHP Array_PHP Tutorial

Lecture 3 of 'The Quickest to Understand PHP Programming': PHP Array_PHP Tutorial

WBOY
Release: 2016-07-21 15:34:11
Original
842 people have browsed it

Example 7: Basic operations on array values ​​

Copy code The code is as follows:

$arr=array ('a'=>"You",'b'=>"I","He");
$arr[]="Other";
echo $arr['b']. "
";
$arr['c']="";//Give a null value, but still occupy the position
echo count($arr)."
" ;//How many values ​​does the array have.
unset($arr['b']);//This function can unregister strings, entire arrays and other value types and reference types.
print_r($arr);//This function prints the entire internal structure of numerical and reference types.
echo "
";
foreach($arr as $key=>$value)
echo $key.":".$value."
";// Loop through the entire array of values.
?>

Example 8: Conversion between arrays and strings
Copy code The code is as follows:

$arr=array('a'=>"you",'b'=>"I","him");
echo $arr =implode('-',$arr);//Array to string, connector -
echo "
";
print_r(explode('-',$arr,2)); //Convert string to array. If the last parameter is not used, it means that all '-'s are divided into arrays
?>

Example 9: Array sorting
Copy code The code is as follows:

$arr=array('b'=>"You",'a'=>"I"," He");
ksort($arr);//The array is sorted by the pinyin (UTF-8 encoding) of the key value, and the key value will not be lost. Note that this sort does not return a new array but directly passes the original array as a reference.
print_r($arr);
echo "
";
asort($arr);//The array is sorted by the pinyin of the value (UTF-8 encoding), and the key value will not be lost. If you don't want key values, you can use the function sort(); if you want to reverse the order, there is also the function rsort(). Note that this sort does not return a new array but directly passes the original array as a reference.
print_r($arr);
echo "
";
$arr=array(10000,100,1000);
natsort($arr);//Naturally sort by numbers value, and natcasesort() is not case sensitive
print_r($arr);
echo "
";
print_r(array_reverse($arr));//Array reverse order
echo "
";
?>

Example 10: Array, random extraction of numbers, number and encoding conversion
Copy code The code is as follows:

$arr=array('b'=>"You",'a'=>"I", "him");
$key=array_rand($arr,2);//The array randomly selects 2 key values ​​and returns an index array containing the two key values
echo $arr[$key[0] ].$arr[$key[1]];
echo "
";
echo mt_rand(60,100);//Return a random integer within the range.
echo "
";
echo chr(mt_rand(ord('a'),ord('z')));//Number and encoding conversion.
echo "
";
?>

That’s it for the array functions. I’ll just pick a few representative ones to get you started. In fact, there are Some uncommon array functions. In addition, we can use for or foreach loops to process arrays and generate our own my_function. There are stupid ways.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322479.htmlTechArticleExample 7: Basic operations on array values. Copy the code. The code is as follows: ?php $arr=array('a'=" you",'b'="I","him"); $arr[]="other"; echo $arr['b']."br"; $arr['c']="";/ /Give a null value, but still account for...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template