Get the id of maximum amount values in a associative array [duplicate] - php

This question already has answers here:
Get object with the highest property value in PHP array
(3 answers)
Find highest value in multidimensional array [duplicate]
(9 answers)
How can I sort arrays and data in PHP?
(14 answers)
Closed 2 years ago.
I have an array in this format:
Array
(
[0] => Array
(
[id] => 117
[name] => Apple
[amount] => 300
)
[1] => Array
(
[id] => 188
[name] => Orange
[count] => 20
)
[2] => Array
(
[id] => 189
[name] => Grapes
[amount] => 7000
)
)
I'm trying to get the id of max amount from the associative array.
how can i perform this?
i'm expecting the result
Array
(
[2] => Array
(
[id] => 189
[name] => Grapes
[amount] => 7000
)
)

It's simplest to just initialise a "maxkey" value with 0 and then iterate over the array, replacing the key when you find a value with a higher amount:
$maxkey = 0;
foreach ($data as $key => $value) {
if ($value['amount'] > $data[$maxkey]['amount']) {
$maxkey = $key;
}
}
print_r($maxkey);
print_r($data[$maxkey]);
Output:
2
Array
(
[id] => 189
[name] => Grapes
[amount] => 7000
)

Related

PHP : Sort an array by value based on another array [duplicate]

This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Sorting a php array of arrays by custom order
(8 answers)
Closed last month.
I have two array and I want to arrange one of them based on the other one.
$sortMe = Array
(
[0] => 100
[1] => 1
[2] => 10
[3] => 1000
)
$example = Array
(
[0] => 1
[1] => 1000
[2] => 100
[3] => 10
)
how can I sort the $sortMe array based on $example to:
$sortMe = Array
(
[1] => 1
[3] => 1000
[0] => 100
[2] => 10
)

print complex php object [duplicate]

This question already has answers here:
Implode a column of values from a two dimensional array [duplicate]
(3 answers)
Closed 7 months ago.
I have the following object:
stdClass Object
(
[ID] => 6
[data] => stdClass Object
(
[categories] => Array
(
[23] => Array
(
[id] => 23
[name] => A
)
[22] => Array
(
[id] => 22
[name] => B
)
[19] => Array
(
[id] => 19
[name] => C
)
)
)
I would like to print A,B,C. I managed to print 1 name:
echo $event->data->categories[19]['name']; but I would like to print all names of the array without knowing the id's.
You can use array_column and implode
echo implode(', ', array_column($event->data->categories, 'name'));
This will get all name items and implode them to a string.
You can use foreach to loop through the categories array like this:
foreach($event->data->categories as $category) {
echo $category['name'];
}

Comma separated value from array [duplicate]

This question already has answers here:
How to Flatten a Multidimensional Array?
(31 answers)
PHP - Multidimensional array to CSV
(3 answers)
How to write a multidimensional array to a csv file. PHP
(2 answers)
multidimensional irregular array to CSV in PHP
(1 answer)
Closed 3 years ago.
I need to explode stdentid array in comma separated value.currently the studentid array is in multidimensional array i need all the value should be in comma separated
The result should be for studentid array is
[studentid] => 36399,96500,96503,96509,96512 and so on..
Array
(
[started] => 1
[studentid] => Array
(
[1] => Array
(
[0] => 36399
)
[2] => Array
(
[0] => 96500
[1] => 96503
[2] => 96506
[3] => 96509
[4] => 96512
[5] => 96515
)
[3] => Array
(
[0] => 96501
[1] => 96504
[2] => 96507
[3] => 96510
[4] => 96513
)
[4] => Array
(
[0] => 96502
[1] => 96505
[2] => 96508
[3] => 96511
[4] => 96514
)
)
[name] => Test
[name_or_email] =>
[submitbtn] => 1
)
assuming your variable is $studentsResult (which contains the whole array you have posted)
in that case:
foreach($studentsResult['studentid'] as $studentId => $studentValues){
$tempStudents[$studentId] = join(',',$studentValues);
}
and then $tempStudents is what you're probably looking for\
If, however all those IDs needs to be under that ONE student, then you haven't said where is the ID, but you can follow the answer on flattening the array in one of the comments or do:
$tempStudents = [];
foreach($studentsResult['studentid'] as $someId => $studentValues){
$tempStudents = array_merge($tempStudents,$studentValues);
}
echo join(',',$tempStudents);

How can I count the occurrence of a key based on array value [duplicate]

This question already has answers here:
count occurrences of values in an associative array in php
(6 answers)
Closed 5 years ago.
I have this array
[furnitures] => Array
(
[0] => Array
(
[fi_id] => 8
[price] => 15.00
)
[1] => Array
(
[fi_id] => 8
[price] => 15.00
)
[2] => Array
(
[fi_id] => 7
[price] => 15.00
)
[3] => Array
(
[fi_id] => 7
[price] => 15.00
)
[4] => Array
(
[fi_id] => 8
[price] => 15.00
)
[5] => Array
(
[fi_id] => 9
[price] => 15.00
)
)
I want unique fi_id from it and I also want to count the occurence from it. How many times is this being repreated. Does PHP has any built in function for it?
Like unique items are 7,8,9 and 7 is repreated 2 times, 8 is repreated 3 times and 9 is repreated 1 time.
you can try this:
//Here you get unique Ids
$uniqueFiIdList = array_unique(array_map(function ($el)
{
return $el['fi_id'];
}, $furnitures));
//Here you get the total unique occurences
$totalUniqueFiId = count($furnitures) - count($uniqueFiIdList);

php | Multidimensional array sorting [duplicate]

This question already has answers here:
Sorting a multidimensional array in PHP? [duplicate]
(3 answers)
How do I Sort a Multidimensional Array in PHP [duplicate]
(10 answers)
How to Sort a Multi-dimensional Array by Value
(16 answers)
Closed 9 years ago.
I have an array and need to be sorted (based on id):
Array
(
[0] => Array
(
[qty] => 1
[id] => 3
[name] => Name1
[sku] => Model 1
[options] =>
[price] => 100.00
)
[1] => Array
(
[qty] => 2
[id] => 1
[name] => Name2
[sku] => Model 1
[options] => Color: <em>Black (+10$)</em>. Memory: <em>32GB (+99$)</em>.
[price] => 209.00
)
)
Is it possible to sort my array to get output (id based)?
Array
(
[0] => Array
(
[qty] => 2
[id] => 1
[name] => Name2
[sku] => Model 1
[options] => Color: <em>Black (+10$)</em>. Memory: <em>32GB (+99$)</em>.
[price] => 209.00
)
[1] => Array
(
[qty] => 1
[id] => 3
[name] => Name1
[sku] => Model 1
[options] =>
[price] => 100.00
)
)
Thanks!
Try like
$id_arr = array();
foreach ($my_arr as $key => $value)
{
$id_arr[$key] = $value['id'];
}
array_multisort($id_arr, SORT_DESC, $my_arr);
You can also place SORT_ASC for assending order.Better you add ORDER BY id to the query through which you are getting this array of results
function cmp($a, $b) {
return $a["id"] - $b["id"];
}
usort($arr, "cmp");//$arr is the array to sort

Categories