I have one problem. I must get 5 images which rates are the biggest. I have table which key = image_id and value = average rating.
Below is print_r of this array
Array ( ['5'] => 5.00 ['4'] => 3.05 ['12'] => 3.00 ['11'] => 4.00 ['21'] => 2.11 ['53'] => 4.44 )
For example
['5'] => 5.00
means that img which id = '5' have rating 5.00
Expected output 2 Arrays ($id and $rating)
Array ( [0] => '5' [1] => '53' [2] => '11' [3] => '4' [4] => '12' )
Array ( [0] => '5.00' [1] => '4.44' [2] => '4.00' [3] => '3.05' [4] => '3.00' )
Can you help me in this?
Use arsort(); and array_slice();
You can also avoid making 2 separate arrays with functions like array_keys(); and array_values();
// Original array
$array = array(
5 => 5.00,
4 => 3.05,
12 => 3.00,
11 => 4.00,
21 => 2.11,
53 => 4.44
);
// Sort array & maintain keys
arsort($array);
// Now get the first 5 elements, keeping the keys
$array = array_slice($array, 0, 5, true);
// IDs
print_r(array_keys($array));
// Ratings
print_r(array_values($array));
Have you tried to use arsort or uasort?
Related
I've got a multidimensional array that I want to sort alphabetically by the values of specific key. Structure of the array is -
Array
(
[sr] => Array
(
[2] => 1
[3] => 2
[4] => 3
[5] => 4
[6] => 5
[7] => 6
[8] => 7
[9] => 8
[10] => 9
)
[deptt] => Array
(
[2] => KKT-TICKETING
[3] => KKT-TICKETING
[4] => KKT-TICKETING
[5] => KKT-HOTELS
[6] => KKT-TICKETING
[7] => KKT-HOTELS
[8] => GTT-TICKETING
[9] => GTT-HOTELS
[10] => GTT-TICKETING
)
I wanted to sort the data on basis of key 'deptt' alphabetically by the values. My desired output should be like all 'GTT-HOTELS' data should shown first then 'KKT-HOTELS' -
[deptt] => Array
(
[2] => GTT-HOTELS
[3] => GTT-TICKETING
[4] => GTT-TICKETING
[5] => KKT-HOTELS
[6] => KKT-HOTELS
[7] => KKT-TICKETING
[8] => KKT-TICKETING
[9] => KKT-TICKETING
[10] => KKT-TICKETING
)
with corresponding values of key 'sr'.
Any ideas how to do this? Hope I could make you understand the scenario.
Given this data:
$data = [
'sr' =>
[
2 => 1,
3 => 2,
4 => 3,
5 => 4,
6 => 5,
7 => 6,
8 => 7,
9 => 8,
10 => 9,
],
'deptt' =>
[
2 => 'KKT-TICKETING',
3 => 'KKT-TICKETING',
4 => 'KKT-TICKETING',
5 => 'KKT-HOTELS',
6 => 'KKT-TICKETING',
7 => 'KKT-HOTELS',
8 => 'GTT-TICKETING',
9 => 'GTT-HOTELS',
10 => 'GTT-TICKETING',
]
];
The below code can be used. The magic is in the array_combine function which merges two arrays, using one for the keys and one for the values. However, in both cases, it takes the values of those arrays, so you need to call array_keys on the former to get those as values.
// Grab our arrays as variables
$sr = $data['sr'];
$deptt = $data['deptt'];
// Sort the latter by values alphabetically
sort($deptt);
// Merge them together, keys from the first and values from the second
$final = array_combine(array_keys($sr), $deptt);
This produces the following:
array (
2 => 'GTT-HOTELS',
3 => 'GTT-TICKETING',
4 => 'GTT-TICKETING',
5 => 'KKT-HOTELS',
6 => 'KKT-HOTELS',
7 => 'KKT-TICKETING',
8 => 'KKT-TICKETING',
9 => 'KKT-TICKETING',
10 => 'KKT-TICKETING',
)
Additional care should be taken to make sure that both arrays have the same number of items. array_combine will return false in that case.
This question already has answers here:
How to Remove value from an array using another array with multiple values?
(4 answers)
Closed 4 years ago.
I have two array, and i want to remove duplicate record from array2. i don't want to link_id 35 record in array 2 because link_id 35 record is present in array1 so it's not show in array2.
I tried with array_map and Unique methods but it's not working well because i think both the array doesn't have the same value.
$array1=
[0] => stdClass Object
(
[link_id] => 35
[link_name] => Test Listerine cool mint packets 3 pack
[alias] => aa
[link_desc] =>
[user_id] => 47
[link_hits] => 103
[link_votes] => 1
[link_rating] => 5.000000
[link_featured] => 0
[link_published] => 1
[link_approved] => 1
[link_template] =>
)
[1] => stdClass Object
(
[link_id] => 373
[link_name] => Test Subject Data Collection Fish Fresh Yellow Tail
[alias] => ba
[link_desc] =>
[user_id] => 47
[link_hits] => 198
[link_votes] => 8
[link_rating] => 2.875000
[link_featured] => 0
[link_published] => 1
[link_approved] => 1
[link_template] =>
)
$array2 =
[0] => stdClass Object
(
[link_id] => 35
[link_name] => Test Listerine cool mint packets 3 pack
[link_desc] =>
[lat] => 0.000000
[lng] => 0.000000
[contactperson] =>
[cat_name] => AA - Made in USA
[link_votes] => 1
[link_rating] => 5.000000
[link_featured] => 0
[value] => 30020864
)
[1] => stdClass Object
(
[link_id] => 541
[link_name] => Test Subject Data Collection Fish Fresh Yellow Tail
[link_desc] =>
[lat] => 25.182573
[lng] => -80.093079
[country] => United States
[postcode] => 33431
[contactperson] => Captain Jack Certified Charters
[cat_name] => BA - Product of USA
[link_votes] => 8
[link_rating] => 2.875000
[link_featured] => 0
[value] => NA
)
You can do that with array-filter. First extract all the ids from the first array and then filter the second array based of those ids.
$arr1 = array( (object) ["link_id"=> 35, "key" => "AAA"], (object) ["link_id"=> 373, "key" => "BBB"]);
$arr2 = array( (object) ["link_id"=> 35, "key" => "CCC"], (object) ["link_id"=> 341, "key" => "DDD"]);
$ids = array_column($arr1, "link_id");
$arr2 = array_filter($arr2, function ($e) use ($ids) {
return !in_array($e->link_id, $ids); //keep him in arr2 only if NOT in ids of arr1
});
Updated more fast answer Consider big amount of data (as for #mickmackusa comment) use this:
$ids = [];
foreach($arr1 as $e)
$ids[$e->link_id] = true;
$arr2 = array_filter($arr2, function ($e) use ($ids) {
return !isset($ids[$e->link_id]);
});
First solution is in O(n^2) and the second is in O(n)
This should do in php7.
Untested code:
var_export(array_diff_key(array_column($array2, null, 'link_id'), array_column($array1, null, 'link_id'));
Assign new 1st level keys to both arrays, then filter on those keys.
Checking against keys will be more efficient than making iterated calls of in_array.
I have got several or an amount of arrays which cannot be foreseen.
I want to access them und change their keys. I guess with a loop.
Array
(
[count] => 1
[0] => Max, Mustermann
[1] => Job
[2] => Companyname
[3] => IT
[4] => CEO
[5] => N610-611
[6] => +49 (30) 111111
[7] => +49 (30) 111111
[8] => max#company.de
)
Array
(
[count] => 1
[0] => Alicia Keys
[1] => Job
[2] => Companyname
[3] => IT
[4] => CEO
[5] => N610-N611
[6] => +49 11111
[7] => +49 11111
[8] => alikey#company.de
)
I'd like to have an output like this:
Array
(
[count] => 1
[Name] => Max, Mustermann
[Jobname] => Job
[Company] => Companyname
[Division] => IT
[CEO] => CEO
[Room] => N610-611
[Tel] => +49 (30) 111111
[Fax] => +49 (30) 111111
[E-Mail] => max#company.de
)
Array
(
[count] => 1
[Name] => Alicia Keys
[Job] => Job
[Company] => Companyname
[Division] => IT
[CEO] => CEO
[Room] => N610-N611
[Tel] => +49 11111
[Fax] => +49 11111
[E-Mail] => alikey#company.de
)
I'm not sure whether to use a foreach oder a for loop, or if a loop is even necessary. I know that changing keys is not that simple and read of a second array which holds the keys you want to change to. But I'm not sure how to do that
You can use array_combine() along with foreach() with predefined keys arrays
//predefined keys array
$index_array = array('count','Name','Jobname','Company','Division','CEO','Room','Tel','Fax','E-Mail');
foreach($array as &$value){
$value = array_combine($index_array ,$value);
}
Output:-https://3v4l.org/VnmWJ
Note:- If your array is single-dimensional then:
$array = array_combine($index_array ,$array);
Output:-https://3v4l.org/R8oY0
Explanation:-
foreach() because it take care about indexes as well as more readable. (save you from undefined index error happen multiple times using for() loop)
&$valueis passing by reference, so that any change in child array will reflect in the original/initial array automatically.
If you have a 1D array and you want to change keys of this array. It's enough to define an array with new keys and change keys of array in a loop. I make a example in following snippets code:
<?php $arr = [
'count' => 1,
'0' => 'Max Mustermann',
'1' => 'Job',
'2' => 'Companyname',
'3' => 'IT',
'4' => 'CEO',
'5' => 'N610-611',
'6' => '+49 (30) 111111',
'7' => '+49 (30) 111111',
'8' => 'max#company.de',
];
$arr2 = ['count', 'Name', 'Jobname', 'Company', 'Division', 'CEO', 'Room', 'Tel', 'Fax', 'E-Mail'];
$index = 0;
foreach($arr as $oldkey => $value) {
$arr[$arr2[$index]] = $arr[$oldkey];
unset($arr[$oldkey]);
$index++;
}
print_r($arr);
you can see output in this link
This array keys is a database primary keys and i need to find biggest value from array and after that update query which need this primary keys. so i need to it will be remain same keys.
My array
Array ( [1] => 7 [2] => 2 [3] => 2 [4] => 10 [5] => 15 [6] => 11 [7] => 40 )
i need this output
Array ( [7] => 40 [5] => 15 [6] => 11 [4] => 10 [1] => 7 )
The PHP arsort function will sort your array in descending order and maintain the indices.
$array = array ( 1 => 7, 2 => 2, 3 => 2, 4 => 10, 5 => 15, 6 => 11, 7 => 40 );
arsort($array);
print_r($array);
Demo: eval.in/836445https://3v4l.org/KQB5Y
To limit it to the top five use array_slice:
print_r(array_slice($array, 0, 5, true));
https://3v4l.org/DSkRt
Given the following arrays how can I elegantly validate that option, price and cost arrays have matching key values?
Array
(
[option] => Array
(
[1] => C
[2] => M
[3] => G
)
[price] => Array
(
[1] => 100
[2] => 200
[3] => 300
)
[cost] => Array
(
[1] => 0
[2] => 0
[3] => 0
)
)
I thought of running a foreach(array as key => values) on each array and sending those values to another array, and then using if(!in_array), but theres got to be a better way to do it.
It sounds like you want the same keys as there is no correlation with the values in the array. If so, you can run a diff on the keys of each sub-array:
if(call_user_func_array('array_diff_key', $array)) {
// not the same keys
} else {
// same keys
}
call_user_func_array() takes the array as an array of arguments and passes each to array_diff_key()
If the result is not empty then there are differences
If the result is empty then there are no differences
I recommend using an array in this way:
Array
(
[option] => Array
(
[C] => Array
(
[price] => 100
[cost] => 0
)
[M] => Array
(
[price] => 200
[cost] => 0
)
[G] => Array
(
[price] => 300
[cost] => 0
)
)
)
PHP Code:
$product = array("option" => array("C" => array("price" => 100, "cost" => 0), "M" => array("price" => 200, "cost" => 0), "G" => array("price" => 300, "cost" => 0)));