I want to sort a php array whose key value combination is dynamic thus making it difficult to define a function and apply usort()
Here is the array
Array (
[0] => Array ( [PAYE] => 43 )
[1] => Array ( [VAT] => 2 )
[2] => Array ( [NHIF] => 1 )
[3] => Array ( [NSSF] => 2 )
[4] => Array ( [MPESA] => 1 )
[5] => Array ( [EQUITEL] => 1 )
[6] => Array ( [AIRTEL] => 1 )
[7] => Array ( [CER] => 2 )
[8] => Array ( [BDD] => 4 )
[9] => Array ( [BMI] => 1 )
[10] => Array ( [TG] => 7 )
[11] => Array ( [BT] => 3 )
[12] => Array ( [EPL] => 4 )
[13] => Array ( [KPL] => 8 )
)
I want to sort the array using the right most value. The result should be
Array (
[0] => Array ( [PAYE] => 43 )
[13] => Array ( [KPL] => 8 )
[10] => Array ( [TG] => 7 )
[8] => Array ( [BDD] => 4 )
[12] => Array ( [EPL] => 4 )
[11] => Array ( [BT] => 3 )
[7] => Array ( [CER] => 2 )
[3] => Array ( [NSSF] => 2 )
[1] => Array ( [VAT] => 2 )
[3] => Array ( [NSSF] => 2 )
[6] => Array ( [AIRTEL] => 1 )
[9] => Array ( [BMI] => 1 )
[4] => Array ( [MPESA] => 1 )
[2] => Array ( [NHIF] => 1 )
)
How should I go about it?
use uasort function to save keys and array_shift to take values to compare
uasort($array, function($i1, $i2) {
return array_shift($i2) - array_shift($i1); });
print_r($array);
uasort and current functions will do the job:
// $arr is your initial array
uasort($arr, function($a, $b){ // will maintain index association
return current($b) - current($a);
});
http://php.net/manual/en/function.current.php
Related
Array
(
[2] => Array
(
[option_id] => 2
[price] => 15
[processor] => Array
(
[3] => Array
(
[processor_id] => 3
[price] => 15
)
[4] => Array
(
[processor_id] => 4
[price] => 15
)
)
)
[3] => Array
(
[option_id] => 3
[price] => 15
[processor] => Array
(
[3] => Array
(
[processor_id] => 3
[price] => 15
)
[4] => Array
(
[processor_id] => 4
[price] => 15
)
)
)
[4] => Array
(
[processor] => Array
(
[3] => Array
(
[price] => // empty value
)
[4] => Array
(
[price] => // empty value
)
)
)
)
I have this array, Now I want to unset the array which array doesn't have any value like in the last array there is no value given so I want to unset the whole array key.
In This array, i have empty value so how can i unset the [4] key. So is it possible without foreach loop.
You can use array_filter for this. As you supplied no code I recommend reading the documentation on php.net.
function filter_function($var) {
// return an expression evaluating to true to keep value
return ($var["option_id"]);
}
$filtered = array_filter($unfiltered, "filter_function");
I have two arrays and I want to combine them together
1) first look like this:
[11] => Array
(
[id] => 11
[name] => test
)
[12] => Array
(
[id] => 12
[name] => test1
)
2) second array look like this:
[0] => Array
(
[offer_id] => 11
[countries] => Array
(
[SA] => Array
(
[id] => 682
)
)
)
[1] => Array
(
[offer_id] => 12
[countries] => Array
(
[KW] => Array
(
[id] => 414
)
)
)
I want this result. How is it possible can any one provide solution for same?
[11] => Array
(
[id] => 11
[name] => test
[countries] => Array
(
[SA] => Array
(
[id] => 682
)
)
)
[12] => Array
(
[id] => 12
[name] => test
[countries] => Array
(
[KW] => Array
(
[id] => 414
)
)
)
Thank you for the help!
Try this:
foreach ($array1 as &$arr1) {
$offer_id = $arr1['id']; // Search for this offer_id in array 2
$match = array_filter($array2, function($v) use ($offer_id){
return $v['offer_id'] == $offer_id; // Return matching offer id
});
$arr1['countries'] = current($match)['countries']; // Assign matched country to array
}
This is the array as I currently have,please convert it to the below array with single dimensional array:-
Array
(
[0] => Array
(
[is_custom] => yes
)
[1] => Array
(
[custom_amount] => 45
)
[2] => Array
(
[custom_amount_text] => Enter Amount
)
[3] => Array
(
[amount_btn] => Dropdown
)
[4] => Array
(
[multiple_amounts] => W3sidGl0bGUiOiJMYWJlbCIsImFtb3VudCI6IjQ1In0seyJ0aXRsZSI6IkxhYmVsIiwiYW1vdW50IjoiNDU1In1d
)
[5] => Array
(
[recurring_plans] => W3sibmFtZSI6IkxhYmVsIiwicmVjdXJyaW5nX2Ftb3VudCI6Ijc4In0seyJuYW1lIjoidSIsInJlY3VycmluZ19hbW91bnQiOiI3ODgifV0=
)
[6] => Array
(
[recurring_interval] => WyJNb250aGx5IiwiUXVhcnRlcmx5IiwiSGFsZi1ZZWFybHkiXQ==
)
[7] => Array
(
[admin_mail_subject] =>
)
[8] => Array
(
[admin_mail_body] =>
)
[9] => Array
(
[user_mail_subject] =>
)
[10] => Array
(
[user_mail_body] =>
)
[11] => Array
(
[is_recurrance] => yes
)
[12] => Array
(
[is_onetime] => yes
)
)
Now I want to convert this into something like this
Array([0]=>
[is_custom] => yes
[custom_amount] => 45
[custom_amount_text] => Enter Amount
[amount_btn] => Dropdown
[multiple_amounts] => W3sidGl0bGUiOiJMYWJlbCIsImFtb3VudCI6IjQ1In0seyJ0aXRsZSI6IkxhYmVsIiwiYW1vdW50IjoiNDU1In1d
[recurring_plans] => W3sibmFtZSI6IkxhYmVsIiwicmVjdXJyaW5nX2Ftb3VudCI6Ijc4In0seyJuYW1lIjoidSIsInJlY3VycmluZ19hbW91bnQiOiI3ODgifV0=
)
Array 0 key will have all the keys can It be possible anyhow.I tried but failed.
Please help me with a possible solution
Use RecursiveIteratorIterator for fast and simple.
$output_array = iterator_to_array(new RecursiveIteratorIterator(new RecursiveArrayIterator($input_array)), 0); //$input_array-Replace your arrray
echo "<pre>";
print_r($output_array);
echo "</pre>";
Result:
Array
(
[0] => yes
[1] => 45
[2] => Enter Amount
[3] => Dropdown
[4] => W3sidGl0bGUiOiJMYWJlbCIsImFtb3VudCI6IjQ1In0seyJ0aXRsZSI6IkxhYmVsIiwiYW1vdW50IjoiNDU1In1d
[5] => W3sibmFtZSI6IkxhYmVsIiwicmVjdXJyaW5nX2Ftb3VudCI6Ijc4In0seyJuYW1lIjoidSIsInJlY3VycmluZ19hbW91bnQiOiI3ODgifV0=
[6] => WyJNb250aGx5IiwiUXVhcnRlcmx5IiwiSGFsZi1ZZWFybHkiXQ==
[7] =>
[8] =>
[9] =>
[10] =>
[11] => yes
[12] => yes
)
Run Yourself:
http://sandbox.onlinephpfunctions.com/code/86a50fd58455d64c4de074b888d5d2ea5ee1dd13
try this one
$newArray=array_map(function($v){
return current($v);
},$oldArray);
I have two array $array1 and $array2 which I get dynamically and look like
$array1 = Array
(
[0] => Array
(
[hour] => 10
[activity] => Array
(
[0] => Array
(
[activity_id] => 1
[cnt] => 2
)
[1] => Array
(
[activity_id] => 2
[cnt] => 1
)
)
)
[1] => Array
(
[hour] => 11
[activity] => Array
(
)
)
[2] => Array
(
[hour] => 12
[percentage] => 0
[activity] => Array
(
[0] => Array
(
[activity_id] => 2
[cnt] => 5
)
[1] => Array
(
[activity_id] => 3
[cnt] => 2
)
)
)
);
$array2 = Array
(
[0] => Array
(
[id] => 1
[name] => Phone Calls
[readable] => 1
[status] => active
)
[1] => Array
(
[id] => 2
[name] => Meeting With Customer
[readable] => 1
[status] => active
)
[2] => Array
(
[id] => 3
[name] => Others Works
[readable] => 1
[status] => active
)
);
which i need to compare.
if $array2['id'] is not in $array1["activity"](i.e"activity_id") add array ['activity_id'=>$array2['id'],'cnt'=>0] to $array1['activity'].
My result must be like
$result = Array
(
[0] => Array
(
[hour] => 10
[activity] => Array
(
[0] => Array
(
[activity_id] => 1
[cnt] => 2
)
[1] => Array
(
[activity_id] => 2
[cnt] => 1
)
[2] => Array
(
[activity_id] => 3
[cnt] => 0
)
)
)
[1] => Array
(
[hour] => 11
[activity] => Array
(
[0] => Array
(
[activity_id] => 1
[cnt] => 0
)
[1] => Array
(
[activity_id] => 2
[cnt] => 0
)
[2] => Array
(
[activity_id] => 3
[cnt] => 0
)
)
)
[2] => Array
(
[hour] => 12
[percentage] => 0
[activity] => Array
(
[0] => Array
(
[activity_id] => 1
[cnt] => 0
)
[1] => Array
(
[activity_id] => 2
[cnt] => 5
)
[2] => Array
(
[activity_id] => 3
[cnt] => 2
)
)
)
);
What i have tried is
$finalArray = array();
foreach($array1 as $arr1) {
foreach($array2 as $arr2) {
if(!in_array($arr2['id'], $arr1['activity'])) {
$array = ['activity_id'=>$arr2['id'], 'cnt'=>0];
}
array_push($arr1['activity'], $array);
unset($array);
}
array_push($finalArray, $result);
}
print_r($finalArray);
in_array() function is not working as I excepted or I am trying to do it in the wrong way. Can someone helps me with this?
Sorry,finally i get what i did wrong.May be someone get helped.
everything is ok just change the line
if(!in_array($arr2['id'], $arr1['activity'])) {
into
if(!in_array( $readActivity['id'], array_column($result['activity'],'activity_id'))){
I have the following array, which is a load of arrays nested in a bigger array:
Array
(
[0] => Array
(
[vote_for] => 15
)
[1] => Array
(
[vote_for] => 15
)
[2] => Array
(
[vote_for] => 15
)
[3] => Array
(
[vote_for] => 5
)
[4] => Array
(
[vote_for] => 5
)
[5] => Array
(
[vote_for] => 2
)
[6] => Array
(
[vote_for] => 2
)
[7] => Array
(
[vote_for] => 2
)
[8] => Array
(
[vote_for] => 2
)
[9] => Array
(
[vote_for] => 2
)
[10] => Array
(
[vote_for] => 2
)
[11] => Array
(
[vote_for] => 2
)
[12] => Array
(
[vote_for] => 2
)
[13] => Array
(
[vote_for] => 2
)
[14] => Array
(
[vote_for] => 2
)
)
I want to do the equivalent of array_count_values on this array, such that I get 15 => 3, 5 => 2 and 2 => 10. How do I un-nest the arrays to do this?
You may want to try reforming your array to count:
$count_array = array();
foreach ($arr as $v) {
$count_array[] = $v['vote_for'];
}
// Now get the counts
$the_count = array_count_values($count_array);
I think array map will also work for this
$count_array = array_map(function($item) { return $item['vote_for']; }, $array);
$the_count = array_count_values($count_array);