I want to sort an array of varchar data in ascending order through PHP code.
I have tried doing it, the result I am getting is :
ABC1
ABC10
ABC11
ABC11A
ABC11B
ABC2
ABC2A
ABC20
ABC3
But i want :
ABC1
ABC2
ABC2A
ABC3
ABC10
ABC11
ABC11A
ABC11B
ABC20
Is there any way to achieve this?
$myarray= array("ABC1","ABC10","ABC11","ABC11A","ABC11B","ABC2","ABC2A","ABC20","ABC3");
natsort($myarray);
var_dump($myarray);
result
array(9) {
[0]=>
string(4) "ABC1"
[5]=>
string(4) "ABC2"
[6]=>
string(5) "ABC2A"
[8]=>
string(4) "ABC3"
[1]=>
string(5) "ABC10"
[2]=>
string(5) "ABC11"
[3]=>
string(6) "ABC11A"
[4]=>
string(6) "ABC11B"
[7]=>
string(5) "ABC20"
}
UPDATE due to discussion in comments
$keys = array_keys($myarray);
natsort($keys);
$newarray = array();
foreach ($keys as $k) $newarray[] = $myarray[$k];
Related
Hi I Have An Indexed Array Like This I Have Been Trying To Figure Out How can i change it to multi dimensional after every 4 elements:
array(8) {
[0]=>
string(3) "yes"
[1]=>
string(11) "John DOE"
[2]=>
string(3) "116"
[3]=>
string(15) "John DOE.jpeg"
[4]=>
string(24) "No"
[5]=>
string(11) "John Snow"
[6]=>
string(3) "116"
[7]=>
string(15) "JohnSnow.jpeg"
}
And I Want to have Something Like This a multidimensional array If Possible
array([0]=>{
[0]=>'Yes',
[1]=>'John Doe',
[2]=>'116,
[3]=>'JohnDoe.jpeg'
},[1]=>{
[0]=>'No',
[1]=>'John Snow',
[2]=>'116,
[3]=>'JohnSnow.jpeg'
}
you could also use this if you prefer to use core coding:-
<?php
$arr=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17];
$i=0;
$arra1=[];
$j=0;
foreach ($arr as $value) {
$arr1[$j][$i]=$value;
$i++;
if($i==4){
$i=0;
$j++;
}
}
print_r($arr1);
So currently I'm having a problem where I have an array setup like this
array(8) {
[0]=> array(2) {
[0]=> float(2.1166666666667)
[1]=> string(7) "9434493"
}
[1]=> array(2) {
[0]=> float(2.07)
[1]=> string(7) "8591971"
}
[2]=> array(2) {
[0]=> float(2.0566666666667)
[1]=> string(8) "17015102"
}
[3]=> array(2) {
[0]=> float(2.0366666666667)
[1]=> string(7) "9637191"
}
[4]=> array(2) {
[0]=> float(2.015)
[1]=> string(8) "11405473"
}
[5]=> array(2) {
[0]=> float(1.9833333333333)
[1]=> string(8) "28233403"
}
[6]=> array(2) {
[0]=> float(2.0366666666667)
[1]=> string(8) "14248330"
}
[7]=> array(2) {
[0]=> float(2.0933333333333)
[1]=> string(8) "14987165"
}
}
After I use the function arsort() it looks like this:
array(8) {
[0]=> array(2) {
[0]=> float(2.1166666666667)
[1]=> string(7) "9434493"
}
[7]=> array(2) {
[0]=> float(2.0933333333333)
[1]=> string(8) "14987165"
}
[1]=> array(2) {
[0]=> float(2.07)
[1]=> string(7) "8591971"
}
[2]=> array(2) {
[0]=> float(2.0566666666667)
[1]=> string(8) "17015102"
}
[6]=> array(2) {
[0]=> float(2.0366666666667)
[1]=> string(8) "14248330"
}
[3]=> array(2) {
[0]=> float(2.0366666666667)
[1]=> string(7) "9637191"
}
[4]=> array(2) {
[0]=> float(2.015)
[1]=> string(8) "11405473"
}
[5]=> array(2) {
[0]=> float(1.9833333333333)
[1]=> string(8) "28233403"
}
}
So all cool that my array is sorted by the value of [0] index. But.....
When I try to loop through like this...
$x = 0;
while ($x < count($sorted_array)) {
$sorted_array[$x][0];
$x++;
}
It kept printing out the original array order. I then realized when I used the function arsort() it kept the original order of the indexes so that's why it was printing in the original array order.
Is there a function to fix this so I can loop it with an index? Any help will be much appreciated.
When you use arsort() you preserve the keys.
Because you are iterating using $x, you are effectively ignoring your sort call.
Either use rsort() with your loop.
Or use a foreach() loop after your arsort() call.
Or best, just call array_column() instead of looping.
Here are some demonstrations: (Demo Link)
$array=$copy=[
[2.1166666666667,9434493],
[2.07,8591971],
[2.0566666666667,17015102],
[2.0366666666667,9637191],
[2.015,11405473],
[1.9833333333333,28233403],
[2.0366666666667,14248330],
[2.0933333333333,14987165]
];
arsort($array);
var_export(array_column($array,0)); // <-- you lose the keys you preserved
echo "\n---\n";
foreach($array as $index=>$row){ // <-- you keep the keys you preserved
echo "$index : {$row[0]}\n";
}
echo "\n---\n";
rsort($copy); // you don't preserve the keys
for($x=0, $count=sizeof($copy); $x<$count; ++$x){ // you should cache the count instead of calling count() on every iteration
echo "$x : {$copy[$x][0]}\n";
}
Output:
array (
0 => 2.1166666666667,
1 => 2.0933333333333,
2 => 2.07,
3 => 2.0566666666667,
4 => 2.0366666666667,
5 => 2.0366666666667,
6 => 2.015,
7 => 1.9833333333333,
)
---
0 : 2.1166666666667
7 : 2.0933333333333
1 : 2.07
2 : 2.0566666666667
6 : 2.0366666666667
3 : 2.0366666666667
4 : 2.015
5 : 1.9833333333333
---
0 : 2.1166666666667
1 : 2.0933333333333
2 : 2.07
3 : 2.0566666666667
4 : 2.0366666666667
5 : 2.0366666666667
6 : 2.015
7 : 1.9833333333333
rsort() is great fit here.
Sort an array in reverse order
This function does not maintain index association
A quick look at PHP documentation suggests rsort() would work.
http://php.net/manual/en/array.sorting.php
I have an existing array (as seen below)...
array(15) {
[0]=>
string(17) "orderid:100000154"
[1]=>
string(61) "shipping_method:channelunitycustomrate_channelunitycustomrate"
[2]=>
string(18) "qty_ordered:1.0000"
[3]=>
string(26) "shipping_firstname:John"
[4]=>
string(24) "shipping_lastname:Doe"
[5]=>
string(17) "shipping_company:"
[6]=>
string(36) "shipping_street1:123 Fake Street"
[7]=>
string(17) "shipping_street2:"
[8]=>
string(20) "shipping_city:LAUREL"
[9]=>
string(28) "shipping_postcode:20723-1042"
[10]=>
string(24) "shipping_region:Maryland"
[11]=>
string(19) "shipping_country:US"
[12]=>
string(21) "vendor_sku:3397001814"
[13]=>
string(16) "vendor_linecode:"
[14]=>
string(1) "
"
}
I have a desired key setup in this array -- the key for the first value would be orderid, so I'd like orderid => 1000000154
How would I go about this? I believe I have to explode the array again, but I'm not sure about the way to write it and none of my attempts have gotten me any closer.
Thanks!
Just loop through and set the keys and values using explode(). Use the first item in the exploded array as the key and the second as the value, then unset the existing item (the numeric-indexed array element) to clean up.
$input = array(
"orderid:100000154",
"shipping_method:channelunitycustomrate_channelunitycustomrate",
"qty_ordered:1.0000",
"shipping_firstname:John",
"shipping_lastname:Doe",
"shipping_company:",
"shipping_street1:123 Fake Street",
"shipping_street2:",
"shipping_city:LAUREL",
"shipping_postcode:20723-1042",
"shipping_region:Maryland",
"shipping_country:US",
"vendor_sku:3397001814",
"vendor_linecode:",
"
"
);
foreach($input as $key => $val) {
if(strstr($val, ":")) {
$exploded = explode(":", $val);
$input[$exploded[0]] = $exploded[1];
}
unset($input[$key]);
}
echo "<pre>";
var_dump($input);
echo "</pre>";
Outputs:
array(14) {
["orderid"]=>
string(9) "100000154"
["shipping_method"]=>
string(45) "channelunitycustomrate_channelunitycustomrate"
["qty_ordered"]=>
string(6) "1.0000"
["shipping_firstname"]=>
string(4) "John"
["shipping_lastname"]=>
string(3) "Doe"
["shipping_company"]=>
string(0) ""
["shipping_street1"]=>
string(15) "123 Fake Street"
["shipping_street2"]=>
string(0) ""
["shipping_city"]=>
string(6) "LAUREL"
["shipping_postcode"]=>
string(10) "20723-1042"
["shipping_region"]=>
string(8) "Maryland"
["shipping_country"]=>
string(2) "US"
["vendor_sku"]=>
string(10) "3397001814"
["vendor_linecode"]=>
string(0) ""
}
$result = array();
foreach($yourArray as $row) {
list($key, $value) = explode(":", $row);
$result[$key] = $value;
}
I get all keywords separated by commas from database and create arrays from them then I merge all arrays. From the resulted array I keep only unique items array_unique. The problem is that I can't sort resulted array by value.
Here is my code:
$select_all_keywords = mysqli_query($db_connect, "SELECT `keywords` FROM `bookmarks`") or die(mysqli_error());
$keywords_array = array();
while($keywords = mysqli_fetch_assoc($select_all_keywords))
{
$explode_keywords = explode(", ", $keywords['keywords']);
$keywords_array = array_merge($keywords_array, $explode_keywords);
}
$unique_keywords = array_unique($keywords_array);
sort($unique_keywords);
$unique_keywords = array_values($unique_keywords);
print_array($unique_keywords);
The printed array:
array(23) {
[0]=>
string(10) "Awolnation"
[1]=>
string(7) "Belgium"
[2]=>
string(7) "Gravity"
[3]=>
string(21) "Nervo (Musical Group)"
[4]=>
string(5) "R3..."
[5]=>
string(22) "R3hab (Musical Artist)"
[6]=>
string(5) "Remix"
[7]=>
string(4) "Sail"
[8]=>
string(30) "Tomorrowland (Recurring Event)"
[9]=>
string(21) "Tomorrowland Festival"
[10]=>
string(9) "Unlimited"
[11]=>
string(6) "dasdas"
[12]=>
string(10) "freshbooks"
[13]=>
string(11) "gdsfgdsfgds"
[14]=>
string(6) "mockup"
[15]=>
string(3) "php"
[16]=>
string(11) "programming"
[17]=>
string(10) "revolution"
[18]=>
string(17) "tomorrowland 2013"
[19]=>
string(11) "ummet ozcan"
[20]=>
string(10) "web design"
[21]=>
string(7) "wikihow"
[22]=>
string(3) "xml"
}
I tryed almost all sorting array functions. None of them helped me sort the array by value ASC.
I believe you might be looking for asort(), it will sort the array by value. But still keep the keys the same.
Look at the documentation here.
http://php.net/manual/en/function.asort.php
I have a $times array, which contains:
array(8) {
[0]=>
string(5) "10:00"
[1]=>
string(5) "13:00"
[2]=>
string(5) "10:00"
[3]=>
string(5) "11:00"
[4]=>
string(5) "12:00"
[5]=>
string(5) "13:00"
[6]=>
string(5) "14:00"
[7]=>
string(5) "15:00"
}
How can I a) sort it so it starts with lowest value b) only have one entry of each time? (no duplicates, currently theres two 10:00s and 13:00s etc)
Why not just use PHP's inbuilt functions:
$input = array_unique($input);
sort($input);
print_r($input);
Well, you could always use the php-shipped standard functions for arrays:
Array Unique
and sort()
$array = array_unique(sort($a));