This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Closed 8 years ago.
below is my array
$myarray = Array(
[1] => Array (['mytime']=>1),
[7] => Array(['mytime']=>2),
[2] => Array(['mytime']=>3),
[3] => Array(['mytime']=>4)
);
I want to sort output of this array based on keys...
$myarray = Array(
[1] => Array (['mytime']=>1),
[2] => Array(['mytime']=>3),
[3] => Array(['mytime']=>4),
[7] => Array(['mytime']=>2)
);
I have already tried ksort($myarray) it displays 1
anyways to fix this??
ksort() does this:
ksort($myarray);
Note: sorting functions do not return a new sorted array; they simply sort the array passed, and return true or false. Thus, ksort($myarray) will return 1 when successful, and $myarray will be sorted.
It's very clear if you read the docs: http://php.net/manual/en/function.ksort.php
Related
This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Closed 1 year ago.
Consider this array:
Array( [Wingspan] => 5
[Scythe] => 1
[Spirit Island] => 2
[Everdell] => 1)
How can I sort this array with the highest value(5) as first, then 2 then lowest (1) as the last? I used this:
print_r(rsort($_POST['item']), SORT_NUMERIC);
But I get this:
Array
(
[0] => 2
[1] => 1
[2] => 1
[3] => 1
)
It changes the key, but this is wrong. I expect to get this:
Array( [Wingspan] => 5
[Spirit Island] => 2
[Scythe] => 1
[Everdell] => 1)
I searched and found this: Sort an Array by numerical value but that did not help.
Use the method
arsort($_POST['item'])
instead of rsort. rsort only works for simple arrays, not for associative ones. But anyways usually if the order of te items matters to you, probably an associative array is not the best choice. You could use a simple array with objects inside containing the values and the keys at once
This question already has answers here:
Is there a function to extract a 'column' from an array in PHP?
(15 answers)
Closed 5 years ago.
I've the following array:
Array
(
[0] => Array
(
[ID] => 1
[more_user_data] => More
)
[1] => Array
(
[ID] => 2
[more_user_data] => More
)
)
Now I want to have a comma separated list of the IDs to use them in an own array. To get something like this:
array(1,2)
How could I only extract the IDs from the second array?
Use array_column() function like:
$arr = array_column($array, 'ID');
Working Example
This question already has answers here:
PHP Sorting Array ASC
(3 answers)
Closed 6 years ago.
Hello everyone I have a small problem with the php sort I have basically a variable example
$ciao ="4,v#2,f#1,x#22,a"; // Can have other elements
$prova = explode("#",$ciao);
rsort($prova);
echo $prova[0];
but that's out 4,v Instead I would like so 1,x
Use sort() simply.
<?php
$ciao ="4,v#2,f#1,x#22,a"; // Can have other elements
$prova = explode("#",$ciao);
sort($prova);
echo $prova[0]; // Prints 1,x
?>
See it working live
have a look at http://php.net/manual/en/function.sort.php, here you can use second parameter i.e. sort_flags
$ciao ="4,v#2,f#1,x#22,a"; // Can have other elements
$prova = explode("#",$ciao);
sort($prova, SORT_STRING); //SORT_STRING - compare items as strings
print_r($prova);
sort($prova, SORT_NUMERIC); //SORT_NUMERIC - compare items numerically
print_r($prova);
output
Array
(
[0] => 1,x
[1] => 2,f
[2] => 22,a
[3] => 4,v
)
Array
(
[0] => 1,x
[1] => 2,f
[2] => 4,v
[3] => 22,a
)
This question already has answers here:
How to reindex an array?
(6 answers)
Closed 7 years ago.
I apologize in advance for this question - I'm sure it's been answered before, (hell, I've done it before), I just can't find anything in the search and my brain is drawing a blank...
I have an array in PHP where print_r outputs:
Array
(
[0] => VALUE
[1] => VALUE
[2] => VALUE
[3] => VALUE
[4] => VALUE
)
I use unset($_SESSION['item'][$lineID]); to unset a particular line from the array, and I'm left with:
Array
(
[0] => VALUE
[1] => VALUE
[3] => VALUE
[4] => VALUE
)
Element [2] has been removed, so the resulting array is no longer numerically in order
(e.g. - [0][1][3][4] instead of [0][1][2][3] ).
What PHP command do I run on the resulting array to "take out blanks" and reformat it so that it is numerically in order from 1 to n
To reset array keys after unset()
$array = array_values($array);
This question already has answers here:
How do I use array_unique on an array of arrays?
(7 answers)
Closed 7 years ago.
I am storing some value in multidimensional array, while storing that iam trying to neglate duplicate array in my values for that i used array_unique function in php. Its working in local, but not working in live can any one help me. I have attached my code below
$ex = $_POST;
$_SESSION["cartdetail"][] = $ex;
$_SESSION["cartdetail"] = array_unique($_SESSION["cartdetail"]);
echo "<pre>";
print_r($_SESSION["cartdetail"]);
echo "</pre>";
outPut
Array
(
[0] => Array
(
[cid] => 7
[cname] => studies
[ctype] => Class Room
[cdate] => 12-1
[ctime] => 09:30 am-06:30 pm
[cdays] => 5
[cprice] => 1
[viewDetails] => start
)
)
When i am trying to store different value in array, its not storing. Its storing only first value in array.Thank for your help
You need to serialize the internal arrays and then unserialize back:
$_SESSION["cartdetail"] = array_map("unserialize", array_unique(array_map("serialize", $_SESSION["cartdetail"])));