Heim  >  Artikel  >  Backend-Entwicklung  >  PHP数组元素的位置

PHP数组元素的位置

WBOY
WBOYOriginal
2016-06-06 20:15:071386Durchsuche

<code class="php">$array_one = ['one','two','three'];

$array_two = ['one'=>'one,one','two'=>'two,two','three'=>'three,three'];</code>

$array_one中元素的位置是已知的,比如'two'的位置是2。
但是在$array_two中'two,two'的位置还是2吗?

很明显不是的,我如果要调换位置1和3的元素,该如何?

<code>$array_two = ['three'=>'three,three','two'=>'two,two','one'=>'one,one'];</code>

回复内容:

<code class="php">$array_one = ['one','two','three'];

$array_two = ['one'=>'one,one','two'=>'two,two','three'=>'three,three'];</code>

$array_one中元素的位置是已知的,比如'two'的位置是2。
但是在$array_two中'two,two'的位置还是2吗?

很明显不是的,我如果要调换位置1和3的元素,该如何?

<code>$array_two = ['three'=>'three,three','two'=>'two,two','one'=>'one,one'];</code>

位置?
应该是下标,而且two的下标应该是1
array_two的关联数组就用key访问!

如果只是三个元素的话 $array_two = array_reverse($array_two); 就行了

三个元素参考: @炑木 的就行了
也可以考虑 array_slice 然后将数组 加起来 (+)

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn