How to use a PHP two-dimensional array

Source: Internet
Author: User

PHP is still quite common, So I studied the two-dimensional PHP array and shared it with you here, if you want to use PHP to retrieve a piece of value from the array, you can use the PHP built-in function array_slice (). However, this function only supports one-dimensional arrays. For details, refer to the PHP manual, the array_slice function does not support two-dimensional arrays ,. First, let's take a 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 the array specified by the offset and length parameters.

Instance: extracts a segment of value from a one-dimensional array.

 
 
  1. <?php $arrayarray = array('b','i','u','u','u'); $result = array_slice($array,0,4); print_r($result); ?>  

The instance extracts four values from the array $ array and starts from the subscript 0 of the starting array. The result is as follows: it is very easy to retrieve a value from the one-dimensional array, and the built-in function array_slice is directly used. The array_slice function is also required for the PHP two-dimensional array value. The two-dimensional array data of the instance is as follows:

 
 
  1. $arrayarray = 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' );  

For the preceding two-dimensional array, if you need to retrieve a segment, you need to know the position of the start and end of the segment in the array. Considering the particularity of the application, it only starts from the first array, in this two-dimensional array, retrieve the number of required arrays. The method is as follows:

 
 
  1. function array_silice_func(array $array, $limit)   
  2. { $k = $count = 0; $temp = array ();  
  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 print_r (array_silice_func ($ array, 5) to obtain the five values of a two-dimensional array. The result is as follows: array ([1] => Array ([0] => b1 [1] => i1 [2] => u1 [3] => u1 [4] => u1)) in this way, the number of required arrays is retrieved. Due to the limitations of the use of PHP two-dimensional arrays, the number of arrays retrieved from a subscript in a two-dimensional array is not required, but this is also worth exploring. The above section describes how to use PHP to retrieve a specified value from a two-dimensional array. I hope it will be helpful to you.


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.