How to use PHP two-dimensional array _php tutorial

Source: Internet
Author: User
PHP is still more commonly used, so I studied the PHP two-dimensional array, here to share with you, I hope that you can use PHP from the array to remove a value from the PHP built-in function array_slice (), but this function only supports one-dimensional array, For the specific use of the PHP manual, the Array_slice function does not support a two-dimensional array. First look at the Array_slice function:

 
  
  
  1. Array array_slice (array array, int offset [, int length [, bool Preserve_keys]]) array_slice ()

Returns a sequence in an array of arrays specified by the offset and length parameters.

Example: Extracting a value from a one-dimensional array

 
  
  
  1. php $arrayarray = Array (' B ', ' I ', ' u ', ' u ', ' U '); $ resultarray_slice?>

The instance takes four values from the array $array, starting at the beginning array subscript 0, and the result is as follows: extracting a value from a one-dimensional array is very simple and directly makes the built-in function Array_slice. PHP two-dimensional array of values also need to use the Array_slice function, instance two-dimensional array data as follows:

 
 
  1. $ Array Array = Array ();
  2. $array [1] = array (' 1 ' =>' B1 ', ' 2 ' =>' i1 ', ' 3 ' =>' U1 ', ' 4 ' =>' U1 ', ' 5 ' =>' U1 ');
    $array [2] = array (' 1 ' =>' B2 ', ' 2 ' =>' i2 ', ' 3 ' =>' U2 ', ' 4 ' =>' U2 ', ' 5 ' =>' U2 ');
    $array [3] = array (' 1 ' =>' B3 ', ' 2 ' =>' i3 ', ' 3 ' =>' U3 ', ' 4 ' =>' U3 ', ' 5 ' =>' U3 ');
    $array [4] = array (' 1 ' =>' b4 ', ' 2 ' =>' I4 ', ' 3 ' =>' U4 ', ' 4 ' =>' U4 ', ' 5 ' =>' U4 ');
    $array [5] = array (' 1 ' =>' b5 ', ' 2 ' =>' i5 ', ' 3 ' =>' U5 ', ' 4 ' =>' U5 ', ' 5 ' =>' U5 ');
    $array [6] = array (' 1 ' =>' b6 ', ' 2 ' =>' I6 ', ' 3 ' =>' U6 ', ' 4 ' =>' U6 ', ' 5 ' =>' U6 ');
    $array [7] = array (' 1 ' =>' B7 ', ' 2 ' =>' i7 ', ' 3 ' =>' U7 ', ' 4 ' =>' U7 ', ' 5 ' =>' U7 ');

As in the two-dimensional array, if you need to take out one of the paragraphs, you need to know where to start and end the position of the array, taking into account the particularity of the application, only starting from the first array, in this two-dimensional array to remove the number of required array. Here's how:

 
 
  1. function Array_silice_func (array $array, $limit)
  2. {$k = $count = 0; $temp = array< /c9> ();
  3. foreach ($array as $key => $value)
  4. {$countcount = count ($value);
  5. if ($count + $k >= $limit)
  6. {$t = array_slice ($value, 0, $limit-$k);
  7. $temp [$key] = $t; Break }
  8. $temp [$key] = $value; $k + = $count; } return $temp; }

Use the following: Print_r (Array_silice_func ($array, 5)) takes out the 5 values of the two-dimensional array, resulting in the following: Array ([1] = = Array ([0] = B1 [1] = I1 [2] =& Gt u1 [3] = u1 [4] = u1) and so on, remove the number of required arrays. Due to the limitations of the use of PHP's two-dimensional arrays, there is no need to implement a single subscript from a two-dimensional array, and the number of groups to be removed, but this is also worth exploring. The above is about how to use PHP from a two-dimensional array to remove a specified value, we hope to help.


http://www.bkjia.com/PHPjc/446526.html www.bkjia.com true http://www.bkjia.com/PHPjc/446526.html techarticle PHP is still more commonly used, so I studied the PHP two-dimensional array, here to take out and share with you, I hope that you can use PHP from the array to remove a value in PHP ...

  • 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.