Home  >  Article  >  Backend Development  >  PHP数组元素的位置

PHP数组元素的位置

WBOY
WBOYOriginal
2016-06-06 20:15:071385browse

<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 然后将数组 加起来 (+)

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