I have two array like this :
$array1 = array(1,1,2,3,3,4,5); //remember that i have two '1' value in this array
$array2 = array($url1, $url2, $url3, $url4, $url5, $url6);
I wish to find the lowest/highest value in $array1 then link to $url1/$url5 like this :
1 or 5
How I can make this happen using PHP? Any help would be greatly appreciated
Thanks
Use the max() & min() function
max — Find highest value
min — Find lowest value
Example code:
$max = max($array);
$min = min($array);
If the array is already sorted, use $array2[0] and $array2[count($array2)-1].
If it's not already sorted, you can use this to sort the arrays.
array_multisort($array1, SORT_NUMERIC, $array2);
$lowest = $array2[0];
$highest = $array2[count($array2)-1];
As far as I understand you need this 2 functions: max, min
try this
$maxValueKeys = array_keys($array1, max($array1)); // Your min value indexes
$minValueKeys = array_keys($array1, min($array1)); // Your max value indexes
But it isn't an associative array
Related
Up front, I would like to clarify that I am not looking for a workaround to find max--I already have the solution for accomplishing that goal. What I am curious about is why max(array_push()) doesn't work.
I have an array with many values in it, and want to find the max of specific values within that array. Existing array:
$array1 = array(2,6,1,'blue','desk chair');
The basic idea is to create a new array consisting of those specified values, and then find the max of that new array. I attempted to make this effort operate all on one line:
$max = max(array_push($array2, $array1[0], $array1[1], $array1[2]));
//this doesn't work at all, since $array2 doesn't already exist, as per the PHP manual
I changed to code to first create $array2, but didn't assign values at that time, just to see if the code works.
$array2 = array();
$max = max(array_push($array2, $array1[0], $array1[1], $array1[2]));
//$array2 is populated, but $max is not assigned
The obvious solution is to assign values to $array2 at creation and then use max() by itself. I'm just curious as to why max(array_push()) doesn't find max, since compounded functions normally operate from inside to outside.
Thank you.
max needs an array to work with but array_push returns an integer and actually uses the provided array by reference, you have to do :
$array2 = array();
array_push($array2, $array1[0], $array1[1], $array1[2])
$max = max($array2);
Alternatively do:
$array2 = array($array1[0], $array1[1], $array1[2]);
$max = max($array2);
A 3rd option (though not the cleanest one):
$array2 = array();
$max = max($array2 = array_merge($array2, [$array1[0], $array1[1], $array1[2]]));
For reference, see:
http://php.net/manual/en/function.max.php
http://php.net/manual/en/function.array-push.php
You could also use array_slice():
$max = max(array_slice(
$array1,
0,
3
));
if
the values you are interested in are in a sequence
you know offset and length of the sequence
For reference, see:
http://php.net/manual/de/function.array-slice.php
http://php.net/manual/de/function.max.php
For an example, see:
https://3v4l.org/nMK1Z
I need to get all maximum values from array using php.
For this array:
$arr = array('a'=>10,'b'=>20,'c'=>5,'d'=>20);
I used below code,
$key = array_search(max($arr), $arr);
but I get only b, I need to get both b and d -- all keys with the highest value.
To find all keys use array_keys with a second parameter:
$arr = array('a'=>10,'b'=>20,'c'=>5,'d'=>20);
$key = array_keys($arr, max($arr));
By the way it is said on array_search man page)
Is there a way to sort an array of associative arrays by the length of one of the elements? What I am trying to do is order the arrays by the largest to smallest description length.
$some_array = [];
$some_array[0] = ['name'=>'a name','description'=>'a description'];
$some_array[1] = ['name'=>'a name1','description'=>'a description 1'];
$some_array[2] = ['name'=>'a name2','description'=>'a description two for the third array element'];
$some_array[3] = ['name'=>'a name3','description'=>'a description three'];
With the above example $some_array[2] should come first followed by 3 then 1 then 0.
PHP >= 5.5.0 needed for array_column:
array_multisort(array_map('strlen', array_column($some_array, 'description')),
SORT_DESC,
$some_array);
Use usortto sort arrays based on custom parameters.
usort($array, function($a, $b) {
return strlen($b['description']) - strlen($a['description']);
});
var_dump($array)
i want to get static length to get any random value from array.
PHP CODE:
in this below code how to get 5 random value from array?
$arr_history = array(23, 44,24,1,345,24,345,34,4,35,325,34,45,6457,57,12);
$lenght=5;
for ( $i = 1; $i < $lenght; $i++ )
echo array_rand($arr_history);
You can use array_rand() to pick 5 random keys and then use those to intersect with the array keys; this keeps the original array intact.
$values = array_intersect_key($arr_history, array_flip(array_rand($arr_history, 5)));
Demo
Alternatively, you can first shuffle the array in-place and then take the first or last 5 entries out:
shuffle($arr_history);
$values = array_slice($arr_history, -5);
This has the advantage that you can take multiple sets out consecutively without overlaps.
Try this :
$rand_value = array_rand($arr_history);
echo $rand_value;
REF: http://php.net/manual/en/function.array-rand.php
OR use :
shuffle($arr_history)
This will shuffle the order of the array : http://php.net/manual/en/function.shuffle.php
I have an associative array from a loop where I store a user_id (key) and a score (value)
$scores[$user_id] = $score;
I was wondering, after this loop has finished, how can I sort the values (get the ten highest scores) and get their keys.
asort($scores);
$tenHighest = array_slice($scores, -10, null, true);
$tenHighestKeys = array_keys($tenHighest);
Use asort() (to preserve the keys) and array_slice() to get the top 10.
asort($scores, SORT_NUMERIC);
$top10 = array_slice($sort, -10);
Use array_keys() to get the keys, like Deceze did.
asort($scores);
http://php.net/manual/en/function.asort.php