Home  >  Article  >  Backend Development  >  How to use PHP two-dimensional array_PHP tutorial

How to use PHP two-dimensional array_PHP tutorial

WBOY
WBOYOriginal
2016-07-15 13:27:21951browse

PHP is still relatively commonly used, so I studied PHP two-dimensional arrays and shared them with you here. I hope it will be useful to everyone. To use PHP to remove a value from an array, you can use the PHP built-in function array_slice( ), but this function only supports one-dimensional arrays. Please refer to the PHP manual for specific usage. The array_slice function does not support two-dimensional arrays. First, let’s take a look at the array_slice function:

<ol class="dp-xml"><li class="alt"><span><span>array array_slice ( array array, int offset [, int length [, bool preserve_keys]] ) array_slice()  </span></span></li></ol>

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

Example: take out a value from a one-dimensional array

<ol class="dp-xml"><li class="alt">
<span><strong><font color="#006699"><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN></FONT></STRONG><SPAN> $</SPAN><SPAN class=attribute><FONT color=#ff0000>array</FONT></SPAN><SPAN class=attribute-value><FONT color=#0000ff>array</FONT></SPAN><SPAN> = array('b','i','u','u','u'); $</SPAN><SPAN class=attribute><FONT color=#ff0000>result</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>array_slice</FONT></SPAN><SPAN>($array,0,4); print_r($result); </SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></span></font></strong></span><span>  </span>
</li></ol>

Example take out four values ​​from the array $array, starting from the starting array subscript 0, and the result As follows: Retrieving a segment of value from a one-dimensional array is very simple, just use the built-in function array_slice. The value of PHP two-dimensional array also needs to use the array_slice function. The example two-dimensional array data is as follows:

<ol class="dp-xml">
<li class="alt"><span><span>$</span><span class="attribute"><font color="#ff0000">array</font></span><span class="attribute-value"><font color="#0000ff">array</font></span><span> = array ();   </span></span></li>
<li class="">
<span>$array [1] = array ('1' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'b1', '2' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'i1', '3' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'u1', '4' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'u1', '5' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'u1' );<br>$array [2] = array ('1' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'b2', '2' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'i2', '3' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'u2', '4' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'u2', '5' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'u2' ); <br>$array [3] = array ('1' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'b3', '2' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'i3', '3' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'u3', '4' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'u3', '5' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'u3' );<br>$array [4] = array ('1' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'b4', '2' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'i4', '3' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'u4', '4' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'u4', '5' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'u4' ); <br>$array [5] = array ('1' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'b5', '2' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'i5', '3' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'u5', '4' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'u5', '5' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'u5' ); <br>$array [6] = array ('1' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'b6', '2' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'i6', '3' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'u6', '4' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'u6', '5' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'u6' ); <br>$array [7] = array ('1' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'b7', '2' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'i7', '3' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'u7', '4' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'u7', '5' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'u7' );  </span>
</li>
</ol>

As the above two-dimensional array, if you need to remove a certain segment, you need to know where the start and end of the segment are in the array. In consideration of the particularity of the application, we only start from the first array and extract the required number of arrays from this two-dimensional array. The method is as follows:

<ol class="dp-xml">
<li class="alt"><span><span>function array_silice_func(array $array, $limit)   </span></span></li>
<li class="">
<span>{ $</span><span class="attribute"><font color="#ff0000">k</font></span><span> = $</span><span class="attribute"><font color="#ff0000">count</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">0</font></span><span>; $</span><span class="attribute"><font color="#ff0000">temp</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">array</font></span><span> ();  </span>
</li>
<li class="alt">
<span> foreach ( $array as $</span><span class="attribute"><font color="#ff0000">key</font></span><span> =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> $value )   </span>
</li>
<li class="">
<span>{ $</span><span class="attribute"><font color="#ff0000">count</font></span><span class="attribute-value"><font color="#0000ff">count</font></span><span> = count ( $value );   </span>
</li>
<li class="alt">
<span>if ($count + $k </span><span class="tag"><strong><font color="#006699">></font></strong></span><span>= $limit)   </span>
</li>
<li class="">
<span>{ $</span><span class="attribute"><font color="#ff0000">t</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">array_slice</font></span><span> ( $value, 0, $limit - $k );   </span>
</li>
<li class="alt"><span>$temp [$key] = $t; break; }   </span></li>
<li class=""><span>$temp [$key] = $value; $k += $count; } return $temp; }  </span></li>
</ol>

Use as follows: print_r (array_silice_func ($array, 5)) to take out the 5 values ​​​​of the two-dimensional array, the result is as follows: Array ( [1] => Array ( [0] => b1 [1] => i1 [2] => u1 [3] => u1 [4] => u1 ) ) and so on, Get the required number of arrays. Due to the limitations of using PHP two-dimensional arrays, there is no requirement to retrieve the number of arrays starting from a certain subscript of the two-dimensional array, but this is also an area worth exploring. The above is about how to use PHP to extract a specified value from a two-dimensional array. I hope it will be helpful to everyone.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446526.htmlTechArticlePHP is still relatively commonly used, so I studied PHP two-dimensional arrays and shared them with you here. , I hope it is useful to everyone. To use PHP to remove a value from an array, you can use PHP...
Statement:
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