Possible Duplicate:
Get the maximum value from an element in a multidimensional array?
find max() of specific multidimensional array value in php
Iam trying to find out the largest array from multi dimensional array.
Array
(
[0] => Array
(
[comment] => ayya
[commented_on] => 17/03/12
[ckey] => 210029c5d80d8259d1599c9a
[username] => pappa
[up] => 2
[down] => 0
[vote] => 2
)
[1] => Array
(
[comment] => sdfsd
[commented_on] => 17/03/12
[ckey] => 08f6a34f96bdeef2903ddaf4
[username] => jesse
[up] => 2
[down] => 0
[vote] => 2
)
[2] => Array
(
[comment] => 159
[commented_on] => 17/03/12
[ckey] => 4da385124793336339268782
[username] => jesse
[up] => 2
[down] => 0
[vote] => 2
)
[3] => Array
(
[comment] => s
[commented_on] => 17/03/12
[ckey] => 299c77c52ee087e468e23e82
[username] => jesse
[up] => 2
[down] => 0
[vote] => 2
)
[4] => Array
(
[comment] => jh
[commented_on] => 17/03/12
[ckey] => 523c18820d8b8db827a240ad
[username] => jesse
[up] => 2
[down] => 0
[vote] => 2
)
[5] => Array
(
[comment] => jh
[commented_on] => 17/03/12
[ckey] => 9f824c11b0ecafcc38c09f4c
[username] => jesse
[up] => 1
[down] => 1
[vote] => 0
)
[6] => Array
(
[comment] => jh
[commented_on] => 17/03/12
[ckey] => c97e7ad4d205220c4b8b0332
[username] => jesse
[up] => 1
[down] => 0
[vote] => 1
)
)
I would like to get the array having highest votes. Highest means the array having highest vote
I have used the following code, but it is not working.
$large=array();
foreach($final2 as $f1){
foreach($final2 as $f2){
if($f1['vote']>$f2['vote'])
$large=$f1;
}
}
6 Answers 6
AFAIK array size is counted from the number of elements it has.
So may be this will help
$largeArraySize = 0;
foreach($arraylist as $array) {
if(count($array) > $largeArraySize) {
$largeArray = $array;
}
}
// Hence $largeArray has the largest
print_r($largeArray);
Unless a large array come this code will take the first occurrence as the largest.
-
you need to change $largeArraySize to equal
count($array);
each time a new larger array is found.user3413723– user34137232015年06月28日 20:03:18 +00:00Commented Jun 28, 2015 at 20:03 -
or a 1 liner :
$max = max( array_map(function( $row ){ return count($row); }, $multi ) )
DarkMukke– DarkMukke2015年08月07日 14:39:15 +00:00Commented Aug 7, 2015 at 14:39
Since the array is only nested one level deep and all the sub-arrays have the same structure, just loop through the outer array and track the maximum value seen so far. Nice and simple.
Without additional information such as whether the array is already sorted on number of votes, the only option you have left is a O(n)
linear search.
$max = 0;
$max_index = 0;
if( count($outer_array) > 0 )
{
// There are elements in the outer array
for($i = 0; $i < count($outer_array); $i++ )
{
if( isset($outer_array[$i]["vote"]) )
{
if( $outer_array[$i]["vote"] > $max )
{
$max_index = $i;
}
}
else
{
// Error condition, malformed array
// Do something here, maybe throw exception?
}
}
}
else
{
// Error condition - outer array is empty, could also throw exception here...
$max = -1; // Assume votes cannot be negative
$max_index = -1;
}
if( $max_index != -1 )
{
// Success
// Do something...
}
This should give you an idea to get the highest number of votes in the inner arrays:
$array = array(
array('votes' => 2),
array('votes' => 3),
array('votes' => 0),
array('votes' => 1)
);
$votes = 0;
$key = 0;
for($i = 0; $i < count($array); $i++){
if($array[$i]['votes'] > $votes){
$key = $i;
$votes = $array[$i]['votes'];
}
}
Change your code to:
$large=array('vote' => -1);
foreach($final2 as $f1){
if ($f1['vote'] > $large['vote']) {
$large=$f1;
}
}
$array = array(.....)
$max_votes = 0;
foreach ($array as $data) {
$vote = $data['vote'];
if ($vote > $max_vote) {
$max_vote = $vote;
}
}
$max = array();
foreach ($array as $key => $data) {
if ($data['vote'] == $max_vote) {
$max[] = $key;
}
}
$max will now hold all of the arrays with the highest number of votes being the value of $max_votes.
foreach
is a copy & paste error since it's not needed.$maxs = $multi[array_keys(array_map(function( $row ){ return count($row['vote']); }, $multi ), max(array_map(function( $row ){ return count($row['vote']); }, $multi )))[0]];