I just can't figure it out, there is a code like this:
$a1 = ['k1' => '1', 'k2' => '2','k3' => '3', 'k4' => '4', 'k5' => '5'];
$a2 = ['k1' => '-1', 'k2' => '-2','k3' => '-3', 'k4' => '-4', 'k5' => '-5'];
$ix = 1; // number to move to
$k = 'k3'; // the key we want to move
$i = 1; // iteration number
$oldk1 = $oldk2 = '';
foreach($a1 as $key => $value) {
if($k === $key && $ix != $i) {
$oldk1 = $value;
$oldk2 = $a2[$k];
unset($a1[$k], $a2[$k]);
}
if($i === $ix) {
$a1[$k] = $oldk1;
$a2[$k] = $oldk2;
}
++$i;
}
But it does not work, why I cannot understand what I was trying to do, I cannot solve this problem.
It consists in the following:
2 arrays with the same keys, but with different values, they must always go through the keys in the same order.
How to make it so that when I want, for example, that k3 stands in 1 place or on any other, I enter a number from 1 to the maximum in the array and so that in 2 arrays it is rearranged to the specified number and so that it is already like this:
$a1 = ['k3' => '3', 'k1' => '1', 'k2' => '2', 'k4' => '4', 'k5' => '5'];
$a2 = ['k3' => '-3', 'k1' => '-1', 'k2' => '-2', 'k4' => '-4', 'k5' => '-5'];
Moving an array element by key to a new position. Here the index of the first element in the array starts at 1 as described in the question.
$a1 = ['k1' => '1', 'k2' => '2','k3' => '3', 'k4' => '4', 'k5' => '5'];
$a2 = ['k1' => '-1', 'k2' => '-2','k3' => '-3', 'k4' => '-4', 'k5' => '-5'];
$k = 'k3'; // Key to move
$i = 1; // New position index
// Retrieve the element with key $k
$element = array_splice($a1, array_flip(array_keys($a1))[$k], 1);
// Insert an element at a new position
$a1 = array_merge(
array_slice($a1, 0, $i - 1),
$element,
array_slice($a1, $i - 1)
);
// Rearrange $a2 keys and values by $a1 array
$a2 = array_merge($a1, $a2);
print_r($a1);
print_r($a2);
I have array like this :
$a = array('value' =>
array(
'lesson_id' => array('1','6'),
'knowledge_value' => array('2','7'),
'knowledge_description' => array('3','8'),
'skill_value' => array('4','9'),
'skill_description' => array('5','10')
)
);
I want to change it to be like this :
$a = array('value' =>
array(
array(
'lesson_id' => '1',
'knowledge_value' => '2',
'knowledge_description' => '3',
'skill_value' => '4',
'skill_description' => '5'
),
array(
'lesson_id' => '6',
'knowledge_value' => '7',
'knowledge_description' => '8',
'skill_value' => '9',
'skill_description' => '10'
),
)
);
How can I do it?
Demo Link
Here is the snippet for you to work, please see inline doc for explanation
$temp = [];
$keys = array_keys($a['value']); // fetched all keys
for ($i = 0; $i < count($a['value']['lesson_id']); $i++) { // compared with first count of lession_id
$temp['value'][] = array_combine($keys, array_column($a['value'], $i)); // combined key and values
}
array_keys — Return all the keys or a subset of the keys of an array
array_combine — Creates an array by using one array for keys and another for its values
array_column — Return the values from a single column in the input array
Is it possible to put if statement inside an array like this
array(1 => 1, 2 => 2 if(!empty(3)){echo ", 3 => 3";});
instead of going
if(!empty(3)){
array (1 => 1, 2 => 2, 3 => 3);
}
else {
array (1 =>1, 2 => 2);
}
You could add the value after you created the array
$array = [1 => 1, 2 => 2];
if (...) {
$array[3] = 3;
}
if you don't need the key, you could also just write
$array[] = 3;
$array = [ 1 => 1, 2 => 2 ];
if ( !empty(3) ) {
$array[3] = 3;
}
You can use the conditional operator for value like below
$array = array(
'1' => '1',
'2' => '2',
'3' => $cond ? '3' : ''
);
if you need to add condition in key you can do below way
if ($cond) {
$array[3] = 3;
}
you can also use array union operator or array_merge:
array('1' => '1') + ($cond ? array('3' => '3') : array())
array_merge(array('1' => '1'), $cond ? array('3' => '3') : array())
now you have to decide what is better for you.
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;
}
?>
Is there any easy way to get the hightest numeric value of an associative array?
$array = array(
0 => array(
'key1' => '123',
'key2' => 'values we',
'key3' => 'do not',
'key4' => 'care about'
),
1 => array(
'key1' => '124',
'key2' => 'values we',
'key3' => 'do not',
'key4' => 'care about'
),
2 => array(
'key1' => '125',
'key2' => 'values we',
'key3' => 'do not',
'key4' => 'care about'
)
);
AwesomeFunction($array, 'key1'); // returns 2 ($array key)
Please be kind since this question was written with a phone. Thanks.
PHP 5.5 introduced array_column() which makes this much simpler:
echo max(array_column($array, 'key1'));
Demo
If you know your data will always be in that format, something like this should work.
function getMax( $array )
{
$max = 0;
foreach( $array as $k => $v )
{
$max = max( array( $max, $v['key1'] ) );
}
return $max;
}
#ithcy - extension to that will work with any size array
function getMax($array) {
if (is_array($array)) {
$max = false;
foreach($array as $val) {
if (is_array($val)) $val = getMax($val);
if (($max===false || $val>$max) && is_numeric($val)) $max = $val;
}
} else return is_numeric($array)?$array:false;
return $max;
}
I think
(returns false when there are no numeric values are found)
This one is inspired by ithcy example, but you can set the key to look up.
Also, it returns both the min and max values.
function getArrayLimits( $array, $key ) {
$max = -PHP_INT_MAX;
$min = PHP_INT_MAX;
foreach( $array as $k => $v ) {
$max = max( $max, $v[$key] );
$min = min( $min, $v[$key] );
}
return Array('min'=>$min,'max'=>$max);
}