My sql table consists of ID,Book name,Genre and ratings.Currently my array from my sql table looks like this (output)
Array (
[book] => Array (
[0] => Array
( [0] => 1
[1] => book one
[2] => Fantasy/Horror
[4] => 8.9 )
[1] => Array (
[0] => 2
[1] => book two
[2] => Comedy
[4] => 8.3 ) ) )
i want to sort this array to DESC by ID
Also in the future by title.
The array variable is $book so i wrote ksort($book) also tried arsort
for some reason it doesnt work? Is it because of the array? Thanks!
IF you are able to:
Use SQL sorting instead, because of the speed and flexibility (Use db-engine where it's appropiate) Only use array-functionality if you really need to! :-)
Do something like:
SELECT ID,Book name,Genre, ratings ORDER BY ID DESC
OR
If you have still have to use array sorting, use usort() in PHP:
<?php
//Book array
$bookArray['book'][0] = array(2, 'book one', 'Fantasy/Horror', 8.9);
$bookArray['book'][1] = array(1, 'book two ', 'Comedy', 8.3);
function sortByIndex($a, $b) {
return $a[0] - $b[0];
}
usort($bookArray['book'], 'sortByIndex');
echo 'RESULT=' . print_r($bookArray,true);
?>
Results in this output:
RESULT=Array ( [book] => Array ( [0] => Array ( [0] => 1 [1] => book two [2] => Comedy [3] => 8.3 ) [1] => Array ( [0] => 2 [1] => book one [2] => Fantasy/Horror [3] => 8.9 ) ) )
Book two comes first here, because in my example I set the first index to be 1 with value 'book two'.
The easiest way is to add this to your SQL statement at the end:
ORDER BY id DESC
Also if you really want to know how to do it in PHP you would do it this way:
function cmp($a, $b)
{
if ($a[0] == $b[0]) {
return 0;
}
return ($a[0] > $b[0]) ? -1 : 1;
}
usort($array['book'], "cmp");
Just you need to change your Select Statement. It should be something like this -
select id,bookname,genre,ratings from yourtable ORDER BY id DESC,bookname;
Related
I've looked on Stack Overflow with no solution that seems to work. Know that I'm a newbie!
Suppose I have a file (data.csv) with the following contents:
year,total
1990,57.58
2011,73.28
1880,54.67
1996,53.41
1950,53.22
1979,52.76
1876,52.62
1883,52.35
1882,52.12
2018,52.23
...then import the data with PHP:
$csv = array_map('str_getcsv', file('data.csv'));
How would I sort both the year and total by the total column in ascending order (such that 1882/52.12 are under $csv[0] in the array and 2011/73.28 are under $csv[10]?
The following and a print($csv); does not seem to be getting the items in the right order:
function compare($a, $b) {
return ($b[0][1] - $a[0][1]);
}
usort($csv, "compare");
Do I need to use typecasting? Thank you!
This will do the trick:
You have to modify the function like this:
function compare($a, $b)
{
// here, comparing "total" column of each row:
return $a[1] >= $b[1] ? 1 : -1;
}
And the new ordering will be:
Array
(
[0] => Array
(
[0] => 1882
[1] => 52.12
)
[1] => Array
(
[0] => 2018
[1] => 52.23
)
[2] => Array
(
[0] => 1883
[1] => 52.35
)
[3] => Array
(
[0] => 1876
[1] => 52.62
)
[4] => Array
(
[0] => 1979
[1] => 52.76
)
[5] => Array
(
[0] => 1950
[1] => 53.22
)
[6] => Array
(
[0] => 1996
[1] => 53.41
)
[7] => Array
(
[0] => 1880
[1] => 54.67
)
[8] => Array
(
[0] => 1990
[1] => 57.58
)
[9] => Array
(
[0] => 2011
[1] => 73.28
)
)
Three problems with your comparison function.
The items that will be compared in your comparison function are going to be arrays corresponding to rows from your CSV file. For example, $a and $b will be things like [1990,57.58] and [1950,53.22].
When you refer to those items in your comparison function, you're looking at index [0][1], but that doesn't exist; the arrays don't have that second dimension. $a[0][1] and $b[0][1] will both be null, so no sorting happens. (You might think you'd get some kind of warning or notice for trying to refer to an int or float with an array index, but you won't, just one of the weird things about PHP.)
You want to sort in ascending order, but putting $b first will sort in descending order.
The comparison function should return an int greater than, less than, or equal to zero depending on the result of the comparison, but yours will return a float. There are various way to make it return an int. The other answer shows how to do it with a ternary expression. If you're using PHP 7, you can use the combined comparison operator, like this:
function compare($a, $b) {
return $a[1] <=> $b[1];
}
Also, you don't need to define a named comparison function, you can use an anonymous function for the second argument if you like.
usort($csv, function($a, $b) {
return $a[1] <=> $b[1];
});
I have following two multidimensional arrays:
First array:
$array_1_data = Array (
[0] => Array ( [id] => 1 [name] => IT [slug] => it )
[1] => Array ( [id] => 2 [name] => Accounting [slug] => accounting )
)
Second array:
$array_2_data = Array (
[0] => Array ( [cid] => 3 [jid] => 24061 )
[1] => Array ( [cid] => 1 [jid] => 24062 )
)
Expected result:
$some_array = Array (
[0] => Array ( [id] => 1 [name] => IT [slug] => it )
)
I won't mind having [cid] in the result.
I want to intersect these two arrays by [id] of the first array and [cid] of the second array, like inner join in MySQL. I have basic foreach and if else logic for this purpose but speed is a priority now so I'm looking for non-looped solution. For better understanding here is the basic looped solution:
foreach ($array_1_data as $array_1_row ) {
foreach ($array_2_data as $array_2_row ) {
if ($array_2_row['cid'] == $array_1_row['id']) {
//intersection iteration
}
}
}
I tried array_uintersection as follows:
array_uintersect($array_1_data, $array_2_data, function($a1, $a2){
$diff1 = strcasecmp($a1['id'], $a2['cid']);
if ($diff1 != 0) return $diff1;
return 0;
});
But it gives me undefined index 'id'. I checked this question: Comparing two arrays with different key names. First answer for this question gives a looped solution which I want to avoid. Second answer suggests changing SQL structure but I have no control over that. So,
Is there really a non-looped fast solution to this kind of situation?
The solution using array_uintersect_uassoc function:
$result = array_uintersect_uassoc($array_1_data, $array_2_data, function($a, $b){
return strcasecmp($a['id'], $b['cid']);
}, function($a, $b){
return (int) [$a, $b] == ['id', 'cid'];
});
print_r($result);
The output:
Array
(
[0] => Array
(
[id] => 1
[name] => IT
[slug] => it
)
)
According yo your condition: to intersect these two arrays by [id] of the first array and [cid] of the second array, we should consider a key comparison function for those keys only ['id', 'cid'].
Having the needed keys on each comparison step it only remain to compare their values(with value compare function)
http://php.net/manual/en/function.array-uintersect-uassoc.php
DEMO link
I want to search an array for duplicate values in each subarray and if there is one, only keep the highest value connected to this item.
That's my question, but I love to add an example to clarify things!
This is my array:
Array
(
[0] => Array
(
[groupid] => 1
[points] => 5
)
[1] => Array
(
[groupid] => 1
[points] => 1
)
[2] => Array
(
[groupid] => 1
[points] => 4
)
[3] => Array
(
[groupid] => 2
[points] => 1
)
)
As you can see, the first three items have the same groupid. I want to match those items and see which of them has the highest points. The other one (with the lowest points) should be kicked out of the array.
So in this case, item 0, 1 and 2 are from the same group, but 2 has the highest points. 0 and 1 should be dropped out of the array.
My desired result would be something like this:
Array
(
[0] => Array
(
[groupid] => 1
[points] => 5
)
[1] => Array
(
[groupid] => 2
[points] => 1
)
)
I've been trying for a few hours now but no luck yet. Maybe I am overlooking something very simple and thinking to hard to figure this out... Any help would be appreciated!
EDIT
It's a Drupal site (but that shouldn't make any difference) and this is query:
foreach ($promotions as $value) {
$promo = db_query("SELECT * FROM {_promo_articles} WHERE art = :art AND promoid = :promoid", array(
':art' => $value['product'][0]->sku,
':promoid' => $value['promo_id'][0])) ->fetchAll();
}
As I see from your comment, the information is coming from the database so do something like this:
(pseudocode)
SELECT group_id, MAX(points)
FROM db_table
GROUP BY group_id;
I would do something like :
$final_array = array();
foreach($array AS $element)
{
if(!isset($final_array[$element["groupid"]]) ||
$final_array[$element["groupid"]] < $element["points"])
$final_array[$element["groupid"]] = $element["points"];
}
Then, $final_array would look like...
Array
(
[1] => 5
[2] => 1
)
(group 1 has 5 points, group 2 has 1 point)
You could use the usort function to order your array in order by points. This function will order it from greatest to least:
usort($myArray, function($a, $b) {
return $b['points'] - $a['points'];
});
As stated in comments the best way to do this is from your database if that is an option.
If not you can use array_reduce and a closure (PHP 5.3+)
$array = array_reduce($array,function(&$result, $val){
if (!array_key_exists($val["groupid"], $result){
$result[$val["groupid"]] = $val;
} else {
if ($result[$val["groupid"]]["points"] < $val["points"])
$result[$val["groupid"]]["points"] = $val["points"];
}
}, array());
Then if you want to reset the keys
$array = array_values($array);
Suppose I have a php array like this:
$shop = array( array("name"=>"Tom", "level"=> 1.25 ),
array("name"=>"Mike","level"=> 0.75 ),
array("name"=>"John","level"=> 1.15 )
);
I want to filter this array similar to filtering a mysql table with where conditions. Supposedly I want every array where level is higher than 1. I could iterate through and check with if statements. Are there any php solutions to this?
array_filter is what you are looking for:
$results= array_filter($shop, function($item) { return $item['level'] > 1; });
print_r($results);
Output:
Array
(
[0] => Array
(
[name] => Tom
[level] => 1.25
)
[2] => Array
(
[name] => John
[level] => 1.15
)
)
I have an array that I'm simply trying to order by a score key that contains the value that I'm trying to sort by. The desired results would change the order of the array below to display the index in the following order: [0], [2], [1].
I'm looping through the results which are pulled from a database. I'm creating a key named score and then pushing the $row2 array in to a separate array because if I use mysql_data_seek it gets rid of the score key.
Also it's creating an unwanted key score at the bottom.
How can I best clean this up and make sure the results are ordered high to low as desired?
$my_array = array();
while ($row2 = mysql_fetch_assoc($result2))
{
//score determined here
//$row2['score'] = ...more math, variable is numeric, not a string
array_push($array_page,$row2);
}
Current undesired results...
Array
(
[0] => Array
(
[score] => 7
)
[1] => Array
(
[score] => 2
)
[2] => Array
(
[score] => 4
)
[score] =>
)
Desired results...
Array
(
[0] => Array
(
[score] => 7
)
[2] => Array
(
[score] => 4
)
[1] => Array
(
[score] => 2
)
)
function scoreSort($a, $b){
if($a['score'] == $b['score']) return 0;
return $a['score'] > $b['score'] ? -1 : 1;
}
usort(&$myArray, 'scoreSort');
on php>5.3 you can use inline-functoins:
usort(&$myArray, function($a,$b){ /* ... */ });