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

PHP数组元素的位置

WBOY
Release: 2016-06-06 20:15:07
Original
1437 people have browsed it

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

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

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

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

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

回复内容:

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

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

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

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

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

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

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

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

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template