Last active
December 16, 2015 02:03
-
-
Save ytkhs/7650c0286fa439c1f02a to your computer and use it in GitHub Desktop.
PHP:listを使って配列の順番を任意に入れ替える ref: http://qiita.com/qube81/items/aab9803c64a1068c769b
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// これを | |
$x = ['PHP', 'Ruby', 'Perl']; | |
// こうしたい | |
$x = ['PHP', 'Perl', 'Ruby']; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// まともに入れ替える | |
$temp = $x[1]; | |
$x[1] = $x[2]; | |
$x[2] = $temp; | |
var_dump($x); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
array(3) { | |
[0]=> | |
string(3) "PHP" | |
[1]=> | |
string(4) "Perl" | |
[2]=> | |
string(4) "Ruby" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
list($x[0], $x[2], $x[1]) = $x; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
array(3) { | |
[0] => | |
string(3) "PHP" | |
[1] => | |
string(4) "Perl" | |
[2] => | |
string(4) "Perl" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//PHP 7.0.0RC6 | |
list($x[0], $x[2], $x[1]) = $x; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
array(3) { | |
[0] => | |
string(3) "PHP" | |
[1] => | |
string(4) "Ruby" | |
[2] => | |
string(4) "Ruby" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
list($x[0], $x[1], $x[2]) = [$x[0], $x[2], $x[1]]; | |
// または | |
list($x[0], $x[2], $x[1]) = $x + []; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// PHP5, PHP7どちらでも同じ結果 | |
array(3) { | |
[0] => | |
string(3) "PHP" | |
[1] => | |
string(4) "Perl" | |
[2] => | |
string(4) "Ruby" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// PHPではこういう書き方ができない | |
$x[0], $x[1], $x[2] = $x[0], $x[2], $x[1]; | |
# => PHP Parse error: syntax error, unexpected ',' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment