Array Operations, sort, asort, ksort, array_push, and array_shift

Source: Internet
Author: User
When using array, you can simply assign values to array variables. $ A = array ('ABC', 'Def ', 'ghi'); array variable assignment
  • When using array, you can simply assign values to array variables. $ A = array ('ABC', 'Def ', 'ghi ');
  • The following data is generated during data generation. $ A [0] = 'abc'; $ a [1] = 'Def '; $ a [2] = 'ghi ';
  • If the subscript is omitted, the array is automatically allocated for the last time. $ A [] = 'jkl '; $ a [] = 'mno ';
Break down array variables
  • Array variables can be easily decomposed using the list $ d = array ('ABC', 'Def ', 'ghi'); list ($ a, $ B, $ c) = $ d;
  • The following data is generated during data generation. $ A = $ d [0]; $ B = $ d [1]; $ c = $ d [2];
Decomposed string
  • Set string variables and indicators to view and access character decomposition.
    $ A [0] = '0', $ a [1] Visible = '1 ′
    However, the array is not available at all because it is back and added as a reference.
$a = '0123';  print "

$a[0]

\n"; print "

$a[1]

\n"; print "

$a[2]

\n"; print "

$a[3]

\n";
Get the number of arrays
  • Use count to count the array.
    The maximum number of characters to add and the value of count ($ a) is-1.
$a = array('abc', 'def', 'ghi');    $ct = count($a);
Sort and list references
  • If the following arrays exist.
    $ A = array ('ABC', 'Def ', 'ghi ');
  • In PHP3, code reset, while, and each are used for resetting.
    $ Tmp [0] is a subscript character, and $ tmp [1] is the actual data.
    reset($a);    while ($tmp = each($a)) {    print "

$tmp[0] - $tmp[1]

\n"; }
Array sorting (ascending)
  • Use sort to sort arrays in ascending order.
    $a = array('abc', 'def', 'ghi')    sort($a);     foreach ($a as $tmp) {    print "

$tmp

\n"; }
Array sorting (descending)
  • Use rsort to sort arrays in descending order.
    $a = array('abc', 'def', 'ghi');    rsort($a);     foreach ($a as $tmp) {    print "

$tmp

\n"; }
Sort joined arrays (ascending)
  • Use asort to sort arrays in ascending order.
$a = array('key1' => 'abc', 'key2' => 'def', 'key3' => 'ghi');    asort($a);     foreach ($a as $key => $tmp) {    print "

$key - $tmp

\n"; }
Sort Associated arrays (descending)
  • Use arsort to sort arrays in ascending order.
    $a = array('key1' => 'abc', 'key2' => 'def', 'key3' => 'ghi');    arsort($a);     foreach ($a as $key => $tmp) {    print "

$key - $tmp

\n"; }
Sort joined array key names (ascending)
  • Use ksort to sort the array key names in ascending order.
    $a = array('key1' => 'abc', 'key2' => 'def', 'key3' => 'ghi');    ksort($a);     foreach ($a as $key => $tmp) {    print "

$key - $tmp

\n"; }
Sort joined array key names (descending)
  • Use krsort to sort the array key names in descending order.
$a = array('key1' => 'abc', 'key2' => 'def', 'key3' => 'ghi');    krsort($a);     foreach ($a as $key => $tmp) {    print "

$key - $tmp

\n"; }
Add at the end of the array
  • Use array_push at the end of the array to add elements.
    You can specify multiple data entries.
    This function is available in PHP4.
    $a = array('abc', 'def', 'ghi');     array_push($a, 'data1', 'data2');     foreach ($a as $tmp) {    print "

$tmp

\n"; }
Delete end of array
  • Use array_pop at the end of the array to delete elements.
    You can specify multiple data entries.
    This function is available in PHP4.
    $a = array('abc', 'def', 'ghi');     array_pop($a, 'data1', 'data2');     foreach ($a as $tmp) {    print "

$tmp

\n"; }
Add an array header
  • The array header uses array_unshift to add elements.
    You can specify multiple data entries.
    This function is available in PHP4.
    $a = array('abc', 'def', 'ghi');    array_unshift($a, 'data1', 'data2');    foreach ($a as $tmp) {    print "

$tmp

\n"; }
Delete array header
  • The array header uses array_shift to delete elements.
    You can specify multiple data entries.
    This function is available in PHP4.
    $a = array('abc', 'def', 'ghi');    array_shift($a);    foreach ($a as $tmp) {    print "

$tmp

\n"; }
Array link
  • The array link uses array_merge.
    This function is available in PHP4.
    $a = array('abc', 'def', 'ghi');    $b = array('123', '456', '789');    $a = array_merge($a, $b);    foreach ($a as $tmp) {    print "

$tmp

\n"; }

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.