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);
Related
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 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
I have an array like:-
([0] =>Array([amount] => 1, [address]=> 'a'),
[1] =>Array([amount] => 12, [address]=> 'b'),
[2] =>Array([amount] => -1, [address]=> 'a'),
[3] =>Array([amount] => 3, [address]=> 'a'))
How do I make an Loop so that at last I get amounts of a and only positive ones.
Let's say your array is this,
$array = array(
array('amount' => 3, 'address' => 'a'),
array('amount' => 26, 'address' => 'a'),
array('amount' => 345, 'address' => 'a'),
array('amount' => -3, 'address' => 'a'),
array('amount' => 22, 'address' => 'a'),
);
You can write a small for loop to achieve this,
$results = array();
foreach ($array as $k => $v){
if($v['amount'] > 0 && $v['address'] == 'a'){
$results[] = $v;
}
}
print_r($results);
This will give you elements where amount is greater than 0 and address is a. Is this what you looking for?
Could you be more precise with what you want ?
Assuming that your array is in an var called $_var
foreach($_var as $_array){
if($_array['amount'] > 0 && $_array['address']=='a'){ //if the amount is positiv and address = 'a'
$res[] = $_array; //Push the current item in your res array
}
}
$amount_of_a = count($res); //The number of a
print_r($res); //your result
I have this array
$array = Array(
'0' => Array('id' => '5', 'class' => 'A'),
'1' => Array('id' => '53', 'class' => 'B'),
'2' => Array('id' => '2', 'class' => 'C'),
);
I want if for example if class is 'B' to keep only key 1;
This is my code but it is not working correct:
foreach ($array as $key => $values) {
$array[$key]['description'] = 'dadadadad';
if ($values['class'] == 'B') {
$array = array_intersect_key($array, array_flip(Array($key)));
}
}
Please help me.
Use array_filter:
$a = array_filter($a, function($item) {
return $item['class'] === 'B';
});
According to the documentation, "array keys are preserved" when using array_filter.
Help:
Array format like this:
$arr=array(
array('element1'=>'a','element2'=>1),
array('element1'=>'b','element2'=>2),
array('element1'=>'a','element2'=>2),
array('element1'=>'b','element2'=>3),
);
Synthesis is needed,how to change it like:
$arr=array(
array('element1'=>'a','element2'=>array(1,2)),
array('element1'=>'b','element2'=>array(2,3)),
);
$new = array();
foreach ($arr as $key => $value) {
$new[$value['element1']][] = $value['element2'];
}
$new2 = array();
foreach($new as $k=>$v){
$new2[] = array('element1'=>$k,'element2'=>$v);
}
print_r($new2);
Result
array(
array('element1' => 'a', 'element2' => array(0 => 1, 1 => 2)),
array('element1' => 'b', 'element2' => array(0 => 2, 1 => 3)),
)