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.
Related
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 that I need to get a value from within the same array that is unassigned to a variable:
return ['a' => 1, 'b'=> 'a', 'c' => 2];
So in this case I need 'b' to return the same value as 'a'. Which would be 1
Thanks for the help.
edit
I intend on running a function on b's value so the value of b is slightly different than a
return ['a' => 1, 'b'=> myFunction('a'), 'c' => 2];
You can try this way.
foreach ($array as $key => $agent) {
$array[$key]['agent_address_1'] = $agent['agent_company_1'] . ', ' .$agent['agent_address_1'];
unset($array[$key]['agent_company_1']);
}
What you want is not clear.
But i am assuming that you are trying to get the 'b' element of an array to be assigned a value similar to the value of 'a' element of that same array
If that is what you need, this will do it.
<?php
$a = array('a' => 1, 'b' => null, 'c' => 2);
$a['b'] = myFunction($a, 'a');
function myFunction($a, $b)
{
return $a[$b];
}
var_dump($a);
You can then return the array, or do what you want with it.
Maybe something like
<?php
function resolve(array $arr) {
foreach($arr as &$v) {
if ( isset($arr[$v])) {
$v = $arr[$v];
}
}
return $arr;
}
function foo() {
return resolve( ['a' => '5', 'b'=>'a', 'c' => '1'] );
}
var_export( foo() );
will do, prints
array (
'a' => '5',
'b' => '5',
'c' => '1',
)
But keep in mind that resolve( ['b'=>'a', 'a' => 'c', 'c' => '1'] ); will return
array (
'b' => 'c',
'a' => '1',
'c' => '1',
)
(you could resolve that with while( isset($arr[$v])) { instead of if ( isset($arr[$v]) ) { ...but there are most likely more elegant/performant ways to do that)
I am following this thread How can I sort arrays and data in PHP?
I want to sort a two dimensinal array based on another single dimensional array
array i want to sort
$main = array(array('name' => 'terry', 'age' => '25', 'sex' => 'm', 'status' => 'active'),
array('name' => 'raul', 'age' => '26', 'sex' => 'm', 'status' => 'active'),
array('name' => 'mata', 'age' => '27', 'sex' => 'm', 'status' => 'active'),
array('name' => 'ronaldo', 'age' => '28', 'sex' => 'm', 'status' => 'active'),
array('name' => 'drogba', 'age' => '29', 'sex' => 'm', 'status' => 'active'),
array('name' => 'messi', 'age' => '30', 'sex' => 'm', 'status' => 'active'));
order array
$order = array('30','25');
function cmp(array $a, array $b) {
global $order;
return array_search($a['age'], $order) - array_search($b['age'], $order);
}
usort($main, 'cmp');
This is the function i am trying out. but it is not sorting the array.
desired output: I want to get the arrays with age value 30 & 25 as the first two arrays in my input two dimensional array
I am able to do it with this code
function cmp(array $a, array $b) {
global $order;
foreach ($order as $ord) {
if (in_array($ord, $a)) {
return true;
}
}
}
But i want to avoid the foreach loop
Try this:
$inArray = array(30, 25);
uasort($main, function($a, $b) use ($inArray){
$aAge = $a['age'];
$bAge = $b['age'];
$aWeight = 0;
$bWeight = 0;
if (in_array($aAge, $inArray))
$aWeight++;
if (in_array($bAge, $inArray))
$bWeight++;
if ($aWeight != $bWeight) {
return $aWeight > $bWeight ? -1 : 1;
} else if ($aWeight > 0) {
// need to sort by order which specified in array
$aIndex = array_search($aAge, $inArray);
$bIndex = array_search($bAge, $inArray);
return ($aIndex == $bIndex ? 0 : ($aIndex > $bIndex ? 1 : -1));
} else {
// just compare age values
return ($aAge == $bAge ? 0 : ($aAge > $bAge ? 1 : -1));
}
});
I have a maybe stupid question?
I have three arrays. And I want to get different values from the first and third array. I created the following code but the returned values are wrong.
function ec($str){
echo $str.'<br>';
}
$arr1 = array( array(
'letter' => 'A',
'number' => '1'
),
array(
'letter' => 'B',
'number' => '2'
),
array(
'letter' => 'C',
'number' => '3'
)
);
$arr2 = array( array(
'letter' => 'A',
'number' => '1'
),
array(
'letter' => 'B',
'number' => '2'
)
);
$arr3 = array( array(
'letter' => 'D',
'number' => '4'
),
array(
'letter' => 'E',
'number' => '5'
)
);
$mergeArr = array_merge($arr1,$arr3);
foreach ($mergeArr as $kMerge => $vMerge){
foreach ($arr2 as $val2){
if($val2['letter'] != $mergeArr[$kMerge]['letter']){
ec($mergeArr[$kMerge]['letter']);
}
}
}
The result of this code is:
A
B
C
C
D
D
E
E
The result I want:
C
D
E
Thanks in advance.
Based on the result you are looking for, this should do it:
$mergeArr = array_merge($arr1,$arr3);
$res = array_diff_assoc($mergeArr, $arr2);
var_dump($res);
See the snippet on codepad.
Try this instead of your foreach's:
$diff = array_diff($mergeArr, $arr2);
foreach( $diff as $d_k => $d_v ) {
ec($d_v['letter']);
}
If I understand what you are trying to do correctly, this function should do the job:
function find_unique_entries () {
$found = $repeated = array();
$args = func_get_args();
$key = array_shift($args);
foreach ($args as $arg) {
if (!is_array($arg)) return FALSE; // all arguments muct be arrays
foreach ($arg as $inner) {
if (!isset($inner[$key])) continue;
if (!in_array($inner[$key], $found)) {
$found[] = $inner[$key];
} else {
$repeated[] = $inner[$key];
}
}
}
return array_diff($found, $repeated);
}
Pass the key you are searching to the first arguments, then as many arrays as you like in the subsequent arguments. Returns an array of results or FALSE on error.
So your usage line would be:
$result = find_unique_entries('letter', $arr1, $arr2, $arr3);
See it working
How can I group same values in a multidimention array?
I want this
array(
array('a' => 1, 'b' => 'hello'),
array('a' => 1, 'b' => 'world'),
array('a' => 2, 'b' => 'you')
)
to become
array(
array(
array('a' => 1, 'b' => 'hello'),
array('a' => 1, 'b' => 'world')
),
array('a' => 2, 'b' => 'you')
)
function array_gather(array $orig, $equality) {
$result = array();
foreach ($orig as $elem) {
foreach ($result as &$relem) {
if ($equality($elem, reset($relem))) {
$relem[] = $elem;
continue 2;
}
}
$result[] = array($elem);
}
return $result;
}
then
array_gather($arr,
function ($a, $b) { return $a['a'] == $b['a']; }
);
This could be implemented in a more efficient matter if all your groups could be reduced to a string value (in this case they can, but if your inner arrays were something like array('a' => ArbitraryObject) they could not be).