note 125483 added to ref.array
| From: | info at hotkey404 dot com | Date: | Mon, 02 Nov 2020 08:32:46 +0000 |
| Subject: | note 125483 added to ref.array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-215694@lists.php.net to get a copy of this message | ||
Updated code of 'indioeuropeo' with option to input string-based keys.
INPUT:
array(4) {
["a"]=>
string(5) "Apple"
["b"]=>
string(6) "Banana"
["c"]=>
string(3) "Cat"
["d"]=>
string(3) "Dog"
}
OUTPUT:
array(4) {
["a"]=>
string(5) "Apple"
["d"]=>
string(3) "Dog"
["b"]=>
string(6) "Banana"
["c"]=>
string(3) "Cat"
}
FUNCTION:
<?php
function array_move_elem($array, $from, $to){
// return if non-numeric couldn't be found or from=to
if(!is_numeric($from)){
if(array_search($from, array_keys($array))!==FALSE){
$from = array_search($from, array_keys($array));
}else{
return $array;
}
}
$array_numeric_keys = array();
foreach($array as $k => $v){
$array_numeric_keys[] = $k;
}
if ($from == $to) { return $array; }
$c = count($array_numeric_keys);
if (($c > $from) and ($c > $to)) {
if ($from < $to) {
$f = $array_numeric_keys[$from];
for ($i = $from; $i < $to; $i++) {
$array_numeric_keys[$i] = $array_numeric_keys[$i+1];
}
$array_numeric_keys[$to] = $f;
} else {
$f = $array_numeric_keys[$from];
for ($i = $from; $i > $to; $i--) {
$array_numeric_keys[$i] = $array_numeric_keys[$i-1];
}
$array_numeric_keys[$to] = $f;
}
}
$array_new = array();
foreach($array_numeric_keys as $v){
$array_new[$v] = $array[$v];
}
return $array_new;
}
?>
----
Server IP: 45.112.84.4
Probable Submitter: 185.85.3.174 (proxied: 77.237.25.222)
----
Manual Page -- http://php.net/manual/en/ref.array.php
Edit -- https://master.php.net/note/edit/125483
Del: integrated -- https://master.php.net/note/delete/125483/integrated
Del: useless -- https://master.php.net/note/delete/125483/useless
Del: bad code -- https://master.php.net/note/delete/125483/bad+code
Del: spam -- https://master.php.net/note/delete/125483/spam
Del: non-english -- https://master.php.net/note/delete/125483/non-english
Del: in docs -- https://master.php.net/note/delete/125483/in+docs
Del: other reasons-- https://master.php.net/note/delete/125483
Reject -- https://master.php.net/note/reject/125483
Search -- https://master.php.net/manage/user-notes.php