Sort array by value and store in variable - php

$array = array(5,4,6,8,5,3,4,6,1);
I want to sort $array like asort does, but the problem is that asort is a function and its product can't be stored in a variable.
How can I do something like this?:
$array = array(5,4,6,8,5,3,4,6,1);
$sorted_array = asort($array);
Edit: I also want $array to keep its original order.

Do this for maintaining $array in its original order
$array = array(5,4,6,8,5,3,4,6,1);
$sorted_array = $array;
asort($sorted_array);
Output
http://codepad.viper-7.com/8E78Fo

$orignal_array = array(5,4,6,8,5,3,4,6,1);
$copied_array = $orignal_array;
asort($copied_array);
$sorted_array = $copied_array;
not the most efficient way to do it though :(

Sort it first and then assign it
asort($array);
$sorted_array = $array

Related

How to change key array php

How to change key array php from
array(
[0]=>Joni
[1]=>Jono
[2]=>Riki
[3]=>Budi
);
Change index to:
array(
[nominal]=>Joni
[nominal]=>Jono
[nominal]=>Riki
[nominal]=>Budi
);
you can make a multi-dimensional array for this purpose
$arr = array('a','b','c','d');
for($i=0;$i<count($arr);$i++){
$newArr['nominal'][$i] = $arr[$i];
}
print_r($newArr);
The expected outcome you want is not possible at all, because same indexes will be over-written in single-dimensional array.
Check this to understand what i said above:- https://eval.in/954556
Now there are 2 closer possiblities of outcomes, which i am going to mention:-
$possibility1 = [];
$possibility2 =[];
foreach($array as $arr){
$possibility1[] = ['nominal'=>$arr];
$possibility2['nominal'][] = $arr;
}
print_r($possibility1);//first closer possibility
print_r($possibility2);//second closer possibility
Output:- https://eval.in/954559

PHP renumber array but keep sequence

I have an array like this:
array(13,4,7,1,16);
I want to recount the array, but I want to keep the sequence, like this:
array(4,2,3,1,5);
How can I do this?
If you are trying to sort the array, keeping the keys in the same order as the values, the PHP asort() function does that.
If you want to keep the original array but get the keys in sort order, then you can use something like:
$arr = array(13,4,7,1,16);
asort($arr);
$keys = array_keys($arr);
Then $keys has the keys from the original array sorted in the order of the original values, e.g. $keys = array(4,2,3,1,5);
if you want to get the index of the array with reference to the sorted values
Try this
$numbers = array(13,4,7,1,16);
$numberscopy = $numbers;
sort($numberscopy);
$final = array();
//echo array_search(13, $numbers);
for($a=0 ; $a<count($numberscopy );$a++){
$final[] = array_search($numberscopy[$a], $numbers) + 1;
}
var_dump($final);

What is the most elegant way to change a key of an array by preserve the order

I have a big associative array with over 1 000 items and I want to rename one key but the order must be preserved.
I don't wont to iterate over the complete array and copy it into a new.
have a look at the array_splice function: http://php.net/manual/en/function.array-splice.php
this will do the job
$arr[$newkey] = $arr[$oldkey];
unset($arr[$oldkey]);
$array_keys = array_keys($array);
$array_keys[array_search('old', $array_keys)] = 'new';
$array = array_combine($array_keys, $array);
$array[$newkeyname] = $array[$oldkeyname];
unset($array[$oldkeyname]);

Add tems to array with keys in PHP

How to add array items to an existing array with key => value ? actually i want to create an array of mysql rowset i.e.
$n =0;
while($row = mysql_fetch_array($rowset))
{
$array[$n] = array('name' => $row['name'], 'city' = $row['city']);
$n += 1;
}
Thanks.
Just try with:
$existingArray['newKey'] = 'new value';
Or use array_merge function:
$newArray = array_merge($existingArray, $additionalData);
http://php.net/manual/en/function.array-merge.php
That what you're looking for?
-edit-
Just to note, if conflicting results are found, the last most array entry will be used. If you array merge three arrays with id fields, only the final arrays id will be stored in the result.
You may want to look into this:
http://php.net/array_push
Should be simple enough.
For One:
$array['key'] = $value;
Merge:
$mergedArray = array_merge($array1, $array2);
(http://php.net/manual/en/function.array-merge.php)

Change the keys of an array

I've an array like below :
$array = array('string'=>'hello','somethingeElse'=>'how r u', ....);
I wanna change the keys of array to numeric values (consecutive):
$array = array('1'=>'hello','2'=>'how r u','3'=>....);
any help would be appreciated ;)
You could use the array_values() function, which will basically do what you say.
array_values() returns all the values from the input array and indexes
numerically the array.
Example:
$array = array_values($array);
If you want to avoid PHP loops, you may try something like :
$newArray = array_combine(range(1, count($array)), array_values($array));

Categories