I'm pretty new to PHP and i'm stuck in the following scenario. I have an array with some values and I want to get the max value in the array set.
For ex:
$array = array(
0 => array(
'1' => '123',
'2' => '120',
'3' => '30',
'4' => '150'
),
1 => array(
'1' => '123',
'2' => '122',
'3' => '30',
'4' => '158'
),
2 => array(
'1' => '123',
'2' => '129',
'3' => '300',
'4' => '150'
)
);
The value i'm expecting is 300.
I have tried the following code and i don't know how to get the maximum value from all the sub arrays.
$max = 0;
foreach( $array as $k => $v ){
//this is where i need help
}
Any kind of help would be highly appreciated.
You can flatten your array first using array_merge(...$array), then just use the max() function:
$new_array = array_merge(...$array);
echo max($new_array);
Demo
I took #Hirumina's solution and just set $max = $y if $y was > $max
$max = 0;
foreach( $array as $k => $v ) {
foreach($v as $x => $y) {
if($y > $max){
$max = $y;
}
}
}
echo $max;
$new_array = array_map(function($value){
return max($value);
}, $array);
echo max($new_array);
Here array_map function will get max value from individual $array and store in $new_array.
Then max($new_array) will give you max value.
I have an result array like this:-
$array = array(
'key1' => array('...'),
'key2' => array('...'),
'key3' => array('...'),
'key4' => array('...'),
.
.
.
.
'keyn' => array('...')
);
I need to get 5 elements from above array. first time key1 to key5, key6 to key10
and so on...... On the basis of condition I need to perform operation on this.
I need output :-
$array = array(
'key1' => array('...'),
'key2' => array('...'),
'key3' => array('...'),
'key4' => array('...'),
'key5' => array('...')
);
I can get value if my key value directly $array['key1']....$array['key5'] but if key is unknown value than
I am facing problem.
Thanks in advance.
To iterate over such an array you can use array_slice() in this manner:
$n = count($array);
$len = 5;
for ($i = 0; $i + $len <= $n; $i += $len) {
$chunk = array_slice($array, $i, $len, true);
// do work on $chunk
}
you can just splice or slice the first 5 items off of the array.
<?php
$array = array(
'key1' => array('...'),
'key2' => array('...'),
'key3' => array('...'),
'key4' => array('...'),
'key5' => array('...'),
'key6' => array('...'),
'key7' => array('...')
);
$arraySlice1 = array_slice($array, 0, 5);
$arraySlice2 = array_splice($array, 0, 5);
Slice will leave $array intact and $array will still hold 7 items, splice will remove the returned results from $array, so $array will only hold key6 and key7
Edit: A better way is array_slice. See this question. Example:
$fifthElement = array_slice($array,4,1); //will get the 5th element
You can use foreach in a way that gives you the keys and the associated values. So, to get the fifth key-value-pair, you could write:
$i = 1;
foreach($array as $key => $value)
{
if($i == 5)
{
echo "Fifth Key=".$key." value=".$value."\n";
}
$i++;
}
array_slice surely works for you
$first_five_elements = array_slice($array, 0, 5);
Here is codepad
I have an array that looks something like this:
$array = array(
array('Field1' => 'red', 'Field2' => 'green', 'Field3' => 'blue'),
array('Field1' => 'pink', 'Field2' => 'pinkish', 'Field3' => 'barbiecolor'),
array('Field1' => 'red', 'Field2' => 'blue', ' Field3' => 'orange')
);
And I want to check this by the given values:
$searchBy = array('Field1' => 'red', 'Field2' => 'blue');
What I want to achieve, is to return the parent array that has all associative key & value pairs matched. I've tried in_array() but it doesn't work..
array_filter may be better for this:
$matches = array_filter($array,function($a) use ($searchBy) {
foreach($searchBy as $k=>$v) {
if( $a[$k] != $v) return false;
}
return true;
});
You can try:
$find = array_filter($array, function ($a) use($searchBy) {
return array_intersect_assoc($searchBy, $a) == $searchBy;
});
See Live Demo
Old School Version
$find = find($searchBy, $array);
print_r($find);
// Function used
function find($needle, $haystack) {
$r = array();
foreach ( $haystack as $k => $a ) {
array_intersect_assoc($needle, $a) == $needle and $r[$k] = $a;
}
return $r;
}
Old School Demo
I have an array like this:
array (0 =>
array (
'id' => '20110209172713',
'Date' => '2011-02-09',
'Weight' => '200',
),
1 =>
array (
'id' => '20110209172747',
'Date' => '2011-02-09',
'Weight' => '180',
),
2 =>
array (
'id' => '20110209172827',
'Date' => '2011-02-09',
'Weight' => '175',
),
3 =>
array (
'id' => '20110211204433',
'Date' => '2011-02-11',
'Weight' => '195',
),
)
I need to extract minimal and maximal Weight values.
In this example
$min_value = 175
$max_value = 200
Any help on how to do this ?
Thank you !
Option 1. First you map the array to get those numbers (and not the full details):
$numbers = array_column($array, 'weight')
Then you get the min and max:
$min = min($numbers);
$max = max($numbers);
Option 2. (Only if you don't have PHP 5.5 or better) The same as option 1, but to pluck the values, use array_map:
$numbers = array_map(function($details) {
return $details['Weight'];
}, $array);
Option 3.
Option 4. If you only need a min OR max, array_reduce() might be faster:
$min = array_reduce($array, function($min, $details) {
return min($min, $details['weight']);
}, PHP_INT_MAX);
This does more min()s, but they're very fast. The PHP_INT_MAX is to start with a high, and get lower and lower. You could do the same for $max, but you'd start at 0, or -PHP_INT_MAX.
foreach ($array as $k => $v) {
$tArray[$k] = $v['Weight'];
}
$min_value = min($tArray);
$max_value = max($tArray);
For the people using PHP 5.5+ this can be done a lot easier with array_column. Not need for those ugly array_maps anymore.
How to get a max value:
$highest_weight = max(array_column($details, 'Weight'));
How to get the min value
$lowest_weight = min(array_column($details, 'Weight'));
It is interesting to note that both the solutions above use extra storage in form of arrays (first one two of them and second one uses one array) and then you find min and max using "extra storage" array. While that may be acceptable in real programming world (who gives a two bit about "extra" storage?) it would have got you a "C" in programming 101.
The problem of finding min and max can easily be solved with just two extra memory slots
$first = intval($input[0]['Weight']);
$min = $first ;
$max = $first ;
foreach($input as $data) {
$weight = intval($data['Weight']);
if($weight <= $min ) {
$min = $weight ;
}
if($weight > $max ) {
$max = $weight ;
}
}
echo " min = $min and max = $max \n " ;
How about without using predefined functions like min or max ?
//find max
$arr = [4,5,6,1];
$val = $arr[0];
$n = count($arr);
for($i=0;$i<$n;$i++) {
if($val < $arr[$i]) {
$val = $arr[$i];
}
}
echo $val;
//find min
$arr = [4,5,6,1];
$val = $arr[0];
$n = count($arr);
for($i=0;$i<$n;$i++) {
if($val > $arr[$i]) {
$val = $arr[$i];
}
}
echo $val;
$num = array (0 => array ('id' => '20110209172713', 'Date' => '2011-02-09', 'Weight' => '200'),
1 => array ('id' => '20110209172747', 'Date' => '2011-02-09', 'Weight' => '180'),
2 => array ('id' => '20110209172827', 'Date' => '2011-02-09', 'Weight' => '175'),
3 => array ('id' => '20110211204433', 'Date' => '2011-02-11', 'Weight' => '195'));
foreach($num as $key => $val)
{
$weight[] = $val['Weight'];
}
echo max($weight);
echo min($weight);
<?php
$array = array (0 =>
array (
'id' => '20110209172713',
'Date' => '2011-02-09',
'Weight' => '200',
),
1 =>
array (
'id' => '20110209172747',
'Date' => '2011-02-09',
'Weight' => '180',
),
2 =>
array (
'id' => '20110209172827',
'Date' => '2011-02-09',
'Weight' => '175',
),
3 =>
array (
'id' => '20110211204433',
'Date' => '2011-02-11',
'Weight' => '195',
),
);
foreach ($array as $key => $value) {
$result[$key] = $value['Weight'];
}
$min = min($result);
$max = max($result);
echo " The array in Minnumum number :".$min."<br/>";
echo " The array in Maximum number :".$max."<br/>";
?>
$Location_Category_array = array(5,50,7,6,1,7,7,30,50,50,50,40,50,9,9,11,2,2,2,2,2,11,21,21,1,12,1,5);
asort($Location_Category_array);
$count=array_count_values($Location_Category_array);//Counts the values in the array, returns associatve array
print_r($count);
$maxsize = 0;
$maxvalue = 0;
foreach($count as $a=>$y){
echo "<br/>".$a."=".$y;
if($y>=$maxvalue){
$maxvalue = $y;
if($a>$maxsize){
$maxsize = $a;
}
}
}
echo "<br/>max = ".$maxsize;
print fast five maximum and minimum number from array without use of sorting array in php
:-
<?php
$array = explode(',',"78, 60, 62, 68, 71, 68, 73, 85, 66, 64, 76, 63, 81, 76, 73,
68, 72, 73, 75, 65, 74, 63, 67, 65, 64, 68, 73, 75, 79, 73");
$t=0;
$l=count($array);
foreach($array as $v)
{
$t += $v;
}
$avg= $t/$l;
echo "average Temperature is : ".$avg." ";
echo "<br>List of seven highest temperatsures :-";
$m[0]= max($array);
for($i=1; $i <7 ; $i++)
{
$m[$i]=max(array_diff($array,$m));
}
foreach ($m as $key => $value) {
echo " ".$value;
}
echo "<br> List of seven lowest temperatures : ";
$mi[0]= min($array);
for($i=1; $i <7 ; $i++)
{
$mi[$i]=min(array_diff($array,$mi));
}
foreach ($mi as $key => $value) {
echo " ".$value;
}
?>
Probably a simple one for you :
I have 2 arrays
$array1 = array(
'foo' => 5,
'bar' => 10,
'baz' => 6
);
$array2 = array(
'x' => 100,
'y' => 200,
'baz' => 30
);
I wish to get a third array by combining both the above, which should be :
$result_array = array(
'foo' => 5,
'bar' => 10,
'baz' => 36,
'x' => 100,
'y' => 200,
);
Is there any built in 'array - way' to do this, or will I have to write my own function ?
Thanks
$resultArray = $array1;
foreach($array2 as $key => $value) {
if (isset($resultArray[$key])) {
$resultArray[$key] += $value;
} else {
$resultArray[$key] = $value;
}
}
There's no built-in function for this, you'll have to write your own.
you need
$newArray = $array1;
foreach($array2 as $key => $value) {
if(array_key_exists($key, $newArray)){
$newArray[$key] += $value;
}else{
$newArray[$key] = $value;
}
}