Home  >  Article  >  php教程  >  Arrays and array traversal

Arrays and array traversal

WBOY
WBOYOriginal
2016-08-22 10:14:221324browse

I have been learning PHP on and off for a while, but I still can’t understand it well. Now I have sorted out arrays and traversing arrays.

Start from the basics:

 PHP arrays are divided into index arrays, and relational arrays,

 The index array means that the subscripts start from 0, 1, 2, 3, 4..., but the relational array does not, and is understood to be with a key (key is the key)

Start writing an array:

$userNane = array(<span style="color: #800000;">'</span><span style="color: #800000;">陈云</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">罗鑫</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">袁修成</span><span style="color: #800000;">'</span>);

Of course you can also write it like this:

    $userNane[<span style="color: #800080;">0</span>] = <span style="color: #800000;">'</span><span style="color: #800000;">陈云</span><span style="color: #800000;">'</span><span style="color: #000000;">;

    $userNane[</span><span style="color: #800080;">1</span>] = <span style="color: #800000;">'</span><span style="color: #800000;">罗鑫</span><span style="color: #800000;">'</span><span style="color: #000000;">;

    $userNane[</span><span style="color: #800080;">2</span>] = <span style="color: #800000;">'</span><span style="color: #800000;">袁修成</span><span style="color: #800000;">'</span>;

Now let’s iterate through this array

<span style="color: #0000ff;">for</span>($i = <span style="color: #800080;">0</span>;$if70f3dfc26a2a15f262162c414d3a21b<span style="color: #800000;">"</span><span>; </span>
<span>};</span>

At this time, if the subscript of the array does not start from 0 (non-indexed array), then it is useless to just rely on for loop and count(). At this time, our good friend foreach() comes.

<span style="color: #0000ff;">foreach</span> ($userNane <span style="color: #0000ff;">as</span> $key =><span> $value) {<br>   echo $key.</span><span style="color: #800000;">'</span><span style="color: #800000;">------</span><span style="color: #800000;">'</span>.$value.<span style="color: #800000;">"</span><span style="color: #800000;">df250b2156c434f3390392d09b1c9563</span><span style="color: #800000;">"</span><span>; <br>}</span>

$key represents the key value (= is similar to an index subscript) $value represents the value of the key value (subscript),
$key and $value can change their names at will, it is just a convention to use these two variables Express

In addition to foreach(), we have a good friend who can also traverse the array, that is each()

ccc70dc546f18409f5b15c6511319a6f
<?<span style="color: #000000;">php
    $userNane[</span><span style="color: #800000;">'</span><span style="color: #800000;">陈云</span><span style="color: #800000;">'</span>] = <span style="color: #800080;">19</span><span style="color: #000000;">;

    $userNane[</span><span style="color: #800000;">'</span><span style="color: #800000;">罗鑫</span><span style="color: #800000;">'</span>] = <span style="color: #800080;">21</span><span style="color: #000000;">;

    $userNane[</span><span style="color: #800000;">'</span><span style="color: #800000;">袁</span><span style="color: #800000;">'</span>] = <span style="color: #800080;">11</span><span style="color: #000000;">;

    $a</span>=each($userNane);     <span style="color: #008000;">//</span><span style="color: #008000;">指针下移</span>
    echo $a[<span style="color: #800000;">"</span><span style="color: #800000;">key</span><span style="color: #800000;">"</span>].<span style="color: #800000;">'</span><span style="color: #800000;">----</span><span style="color: #800000;">'</span>.$a[<span style="color: #800000;">"</span><span style="color: #800000;">value</span><span style="color: #800000;">"</span>].<span style="color: #800000;">'</span><span style="color: #800000;">df250b2156c434f3390392d09b1c9563</span><span style="color: #800000;">'</span><span style="color: #000000;">;

    $a</span>=<span style="color: #000000;">each($userNane);
    echo $a[</span><span style="color: #800000;">"</span><span style="color: #800000;">key</span><span style="color: #800000;">"</span>].<span style="color: #800000;">'</span><span style="color: #800000;">----</span><span style="color: #800000;">'</span>.$a[<span style="color: #800000;">"</span><span style="color: #800000;">value</span><span style="color: #800000;">"</span>].<span style="color: #800000;">'</span><span style="color: #800000;">df250b2156c434f3390392d09b1c9563</span><span style="color: #800000;">'</span><span style="color: #000000;">;

    $a</span>=<span style="color: #000000;">each($userNane);
    echo $a[</span><span style="color: #800000;">"</span><span style="color: #800000;">key</span><span style="color: #800000;">"</span>].<span style="color: #800000;">'</span><span style="color: #800000;">----</span><span style="color: #800000;">'</span>.$a[<span style="color: #800000;">"</span><span style="color: #800000;">value</span><span style="color: #800000;">"</span>].<span style="color: #800000;">'</span><span style="color: #800000;">df250b2156c434f3390392d09b1c9563</span><span style="color: #800000;">'</span><span style="color: #000000;">;


    </span><span style="color: #008000;">/*</span><span style="color: #008000;">while (!!$a=each($userNane)) {         
    //根据while循环判断,条件内为真则继续执行循环体,为假则退出循环,两个!!将数组转化为布尔值,存在则代表 1
        echo $a["key"].'----'.$a["value"].'df250b2156c434f3390392d09b1c9563';
    }</span><span style="color: #008000;">*/</span>

    <span style="color: #008000;">//</span><span style="color: #008000;">each()函数返回的是一个数组

    </span><span style="color: #008000;">//</span><span style="color: #008000;">each后所在数组的“每一个”键值会被重新包装成一个新的数组,里面有四条数据,key and value,0 and 1

    </span><span style="color: #008000;">//</span><span style="color: #008000;">each完一次后指针下移一位,默认开始是第一位,也就是指向数组的第二条键值对,第二条键值对被重新包装秤数组</span>

?>

As for the introduction of each(), you will understand by reading the comments. After each() completes the array, each element (key-value pair) in the array will be repackaged into a new array with four pieces of data in it.

But such traversal is too troublesome and quite low...

At this time, list(), a good friend of each(), appears. First, let’s introduce list():

<span style="color: #008000;">$userNane = array(1,2,3);</span>

    <span style="color: #008000;">list($a,$b,$c) = $userNane;

    echo $a; </span> <span style="color: #008000;">//</span><span style="color: #008000;">1

</span>

Why does it print out 1?

This is like this, the list() function assigns the values in the array to some variables, and correspondingly (according to the array order) assigns the elements in the $userNane array to $a, $b, $ respectively. c

Now that I understand list(), with its good friend each(), I can easily traverse the array

$userNane[<span style="color: #800000;">'</span><span style="color: #800000;">陈云</span><span style="color: #800000;">'</span>] = <span style="color: #800080;">19</span><span style="color: #000000;">;

$userNane[</span><span style="color: #800000;">'</span><span style="color: #800000;">罗鑫</span><span style="color: #800000;">'</span>] = <span style="color: #800080;">21</span><span style="color: #000000;">;

$userNane[</span><span style="color: #800000;">'</span><span style="color: #800000;">袁</span><span style="color: #800000;">'</span>] = <span style="color: #800080;">11</span><span style="color: #000000;">;

list($name,$age) </span>=<span style="color: #000000;"> each($userNane);

    echo $name;  </span><span style="color: #008000;">//</span><span style="color: #008000;">陈云</span>
    echo $age;  <span style="color: #008000;">//</span><span style="color: #008000;">19<br><br><br></span>
list($name,$age) = each($userNane);


  echo $name; //Luo Xin

 echo $age; //21

<span style="color: #008000;"> </span>

Because the elements

in the array after each are repackaged into a new array, each new array has four pieces of data,

At this time, each data has two corresponding key and value. At this time, key and value cannot be used, but 0 and 1 come in handy,

Use it to assign

0 (key) in the new element array in $userNane to $name, and 1 (value) to $age

Because the array pointer will move down one bit after each() is completed, if you continue list--each, the information of my friend Yuan Xiucheng will be printed out,

At this time, if I want to reprint the first one, I need to use the reset() function. Its function is to reset the

array pointer to the first position

list($name,$age) =<span style="color: #000000;"> each($userNane);
    echo $name;  </span><span style="color: #008000;">//</span><span style="color: #008000;">陈云</span>
    echo $age;  <span style="color: #008000;">//</span><span style="color: #008000;">19</span><span style="color: #000000;">

    list($name,$age) </span>=<span style="color: #000000;"> each($userNane);
    echo $name;  </span><span style="color: #008000;">//</span><span style="color: #008000;">罗鑫</span>
    echo $age;  <span style="color: #008000;">//</span><span style="color: #008000;">21</span>
<span style="color: #000000;">

    reset($userNane);<br>
    list($name,$age) </span>=<span style="color: #000000;"> each($userNane);
    echo $name;  </span><span style="color: #008000;">//</span><span style="color: #008000;">陈云</span>
    echo $age;  <span style="color: #008000;">//</span><span style="color: #008000;">19</span>

Here we introduce two useful array functions:

ccc70dc546f18409f5b15c6511319a6f
9e1686351822eae85fae6c4a2c330ca1<span style="color: #800080;">12</span>,<span style="color: #800000;">"</span><span style="color: #800000;">罗鑫</span><span style="color: #800000;">"</span>=><span style="color: #800080;">18</span><span style="color: #000000;">);

    var_dump(array_flip($number));

    </span><span style="color: #008000;">//</span><span style="color: #008000;">array_flip()交换数组中的键和值</span>

    
?>
See the comments for its functions~

It’s too late, I’m a little tired, no, no, no, my dear two-dimensional array hasn’t been traversed yet, how can I sleep? No! ! !

Look at the code:

<meta charset="UTF-8">
<?php
	//遍历二位数组
	$food = array(
			array('苹果','香蕉','梨子'),
			array('鸡肉','猪肉','牛肉'),
			array('脉动','快线','果粒橙'),
		);

	for ($i=0; $i <count($food); $i++) { 
		for ($j=0; $j <count($food[$i]);$j++) { 
			echo $food[$i][$j].'|';   //数组里的 <span style="color: #ff6600;">第i个数组</span> 中的 <span style="color: #ff6600;">第j个元素</span>,这样理解第二维的数组
		}

		echo "<br />";
	}

	
?>
 

What is a two-dimensional array? Here you can understand it as an array within an array. The above code means that there are three arrays within an array.

Now the question arises, what if there is a two-dimensional array with key-value pairs? How to make traversal.

See code:

    <span style="color: #008000;">//</span><span style="color: #008000;">遍历二位数组带key的</span>
    $food =<span style="color: #000000;"> array(
            array(</span><span style="color: #800000;">'</span><span style="color: #800000;">产品</span><span style="color: #800000;">'</span>=><span style="color: #800000;">'</span><span style="color: #800000;">苹果</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">价格</span><span style="color: #800000;">'</span>=><span style="color: #800000;">'</span><span style="color: #800000;">12</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">口味</span><span style="color: #800000;">'</span>=><span style="color: #800000;">'</span><span style="color: #800000;">香</span><span style="color: #800000;">'</span><span style="color: #000000;">),
            array(</span><span style="color: #800000;">'</span><span style="color: #800000;">产品</span><span style="color: #800000;">'</span>=><span style="color: #800000;">'</span><span style="color: #800000;">鸡肉</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">价格</span><span style="color: #800000;">'</span>=><span style="color: #800000;">'</span><span style="color: #800000;">13</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">口味</span><span style="color: #800000;">'</span>=><span style="color: #800000;">'</span><span style="color: #800000;">甜</span><span style="color: #800000;">'</span><span style="color: #000000;">),
            array(</span><span style="color: #800000;">'</span><span style="color: #800000;">产品</span><span style="color: #800000;">'</span>=><span style="color: #800000;">'</span><span style="color: #800000;">脉动</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">价格</span><span style="color: #800000;">'</span>=><span style="color: #800000;">'</span><span style="color: #800000;">14</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">口味</span><span style="color: #800000;">'</span>=><span style="color: #800000;">'</span><span style="color: #800000;">酸</span><span style="color: #800000;">'</span><span style="color: #000000;">)
        );

    </span><span style="color: #008000;">//</span><span style="color: #008000;">利用 foreach() 来遍历</span>

    <span style="color: #0000ff;">for</span> ($i=<span style="color: #800080;">0</span>; $i 82a3cbd10bb7d2d642758f2bfa4db491<span style="color: #000000;"> $value) {
            </span><span style="color: #008000;">//</span><span style="color: #008000;">echo $food[$i][$key].'|';   </span><span style="color: #008000;">//</span><span style="color: #008000;">只获取到数组里键 对应的值</span>
            echo $key.<span style="color: #800000;">'</span><span style="color: #800000;">---</span><span style="color: #800000;">'</span>.$value;   <span style="color: #008000;">//</span><span style="color: #008000;">打印出了二维数组的键and值</span>
<span style="color: #000000;">        }

        echo </span><span style="color: #800000;">"</span><span style="color: #800000;">df250b2156c434f3390392d09b1c9563</span><span style="color: #800000;">"</span><span style="color: #000000;">;
    }</span>

It’s clear from the comments. In addition to foreach() to traverse, our couple partner list---each can also be implemented

See code:

    <span style="color: #008000;">//</span><span style="color: #008000;">遍历二位数组带key的</span>
    $food =<span style="color: #000000;"> array(
            array(</span><span style="color: #800000;">'</span><span style="color: #800000;">产品</span><span style="color: #800000;">'</span>=><span style="color: #800000;">'</span><span style="color: #800000;">苹果</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">价格</span><span style="color: #800000;">'</span>=><span style="color: #800000;">'</span><span style="color: #800000;">12</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">口味</span><span style="color: #800000;">'</span>=><span style="color: #800000;">'</span><span style="color: #800000;">香</span><span style="color: #800000;">'</span><span style="color: #000000;">),
            array(</span><span style="color: #800000;">'</span><span style="color: #800000;">产品</span><span style="color: #800000;">'</span>=><span style="color: #800000;">'</span><span style="color: #800000;">鸡肉</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">价格</span><span style="color: #800000;">'</span>=><span style="color: #800000;">'</span><span style="color: #800000;">13</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">口味</span><span style="color: #800000;">'</span>=><span style="color: #800000;">'</span><span style="color: #800000;">甜</span><span style="color: #800000;">'</span><span style="color: #000000;">),
            array(</span><span style="color: #800000;">'</span><span style="color: #800000;">产品</span><span style="color: #800000;">'</span>=><span style="color: #800000;">'</span><span style="color: #800000;">脉动</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">价格</span><span style="color: #800000;">'</span>=><span style="color: #800000;">'</span><span style="color: #800000;">14</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">口味</span><span style="color: #800000;">'</span>=><span style="color: #800000;">'</span><span style="color: #800000;">酸</span><span style="color: #800000;">'</span><span style="color: #000000;">)
        );


    </span><span style="color: #008000;">//</span><span style="color: #008000;">利用 list--->each() 来遍历</span>

    <span style="color: #0000ff;">for</span> ($i=<span style="color: #800080;">0</span>; $i <count($food); $i++<span style="color: #000000;">) { 
        </span><span style="color: #0000ff;">while</span> ( list($key,$value) = each($food[$i])) {    <span style="color: #008000;">//</span><span style="color: #008000;">each后的数组拆分赋给list()里的变量($key,$value)</span>
            echo $key.<span style="color: #800000;">'</span><span style="color: #800000;">---</span><span style="color: #800000;">'</span>.$value.<span style="color: #800000;">'</span><span style="color: #800000;">|</span><span style="color: #800000;">'</span>;   <span style="color: #008000;">//</span><span style="color: #008000;">打印出了二维数组的键值</span>
<span style="color: #000000;">        }

        echo </span><span style="color: #800000;">"</span><span style="color: #800000;">df250b2156c434f3390392d09b1c9563</span><span style="color: #800000;">"</span><span style="color: #000000;">;
    }</span>

At this point, the traversal is over~

If there are any errors, pointers are welcome~

Hey, go to bed and go to bed...

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