I having an issue merging arrays. I have 3 arrays that show particular data. I need a way to search through all three arrays and if the record matches the date I need to store it in a new custom array.
I have 3 arrays that need to merge them using the date
The arrays are as follows.
// ARRAY 1
$transactions = array(
array(
"date"=>"2021-03-01",
"trans_count"=>100,
"trans_amount"=>5300
),
array(
"date"=>"2021-03-02",
"trans_count"=>95,
"trans_amount"=>5035
),
array(
"date"=>"2021-03-03",
"trans_count"=>105,
"trans_amount"=>5565
)
);
// ARRAY 2
$overdrafts = array(
array(
"date"=>"2021-03-01",
"od_amount"=>500
),
array(
"date"=>"2021-03-02",
"od_amount"=>1000
),
);
// ARRAY 3
$payouts= array(
array(
"date"=>"2021-03-02",
"amount"=>2300
)
);
I tried to write a function but I got stuck. The end goal is to collect all records from all 3 arrays and coming up with one array.
the expected result should look like this.
$merged_arr = array(
array(
"date"=>"2021-03-01",
"type"=>"transactions",
"trans_count"=>100,
"trans_amount"=>5300
),
array(
"date"=>"2021-03-01",
"type"=>"overdrafts",
"od_amount"=>500,
),
array(
"date"=>"2021-03-02",
"type"=>"transactions",
"trans_count"=>95,
"trans_amount"=>5035
),
array(
"date"=>"2021-03-02",
"type"=>"overdrafts",
"od_amount"=>1000
),
array(
"date"=>"2021-03-02",
"type"=>"payout",
"od_amount"=>2300
),
);
Not 100% sure of how to add the type for each of the different types automatically in a nice way, I'd suggest just adding the types directly on the initial arrays, or looping through each before merging them together.
As for the merging and sorting it's pretty easy and can be done with built-in functionality.
$merged_arr = array_merge($transactions, $overdrafts, $payouts);
usort($merged_arr, function ($left, $right) {
return new DateTime($left['date']) <=> new DateTime($right['date']);
});
// Demo merged array.
print "<pre>";
var_export($merged_arr);
The sort function will work with PHP 7 and up.
Related
I am using Animate.css for Bootstrap carousel and I need to sort the animations.
I have made an array called $anims, which contains all the animations. I want to make a new variable which contains only entrance animations, for example. So this is my array.
$anims = array(
"Bouncing Entrances" => array(
"bounceIn",
"bounceInDown",
....
),
"Bouncing Exits" => array(
"bounceOut",
"bounceOutDown",
....
),
"Fading Entrances" => array(
"fadeIn",
"fadeInDown",
....
),
......
)
$enrtyAnims = ...
?>
After processing it should look like that:
$anims = array(
"Bouncing Entrances" => array(
"bounceIn",
"bounceInDown",
....
),
"Fading Entrances" => array(
"fadeIn",
....
)
)
But I don't have any idea how to do it with the keys. I want to be able to say I want new array with keys X and Y and it makes it.
You can simply use array_filter function with third flag parameter along with the flag ARRAY_FILTER_USE_KEY so your code looks like as
$result_array = array_filter($anims,function($k){
return (strpos($k,"Entrances") !== false);
},ARRAY_FILTER_USE_KEY);
Note: Flags as third parameter are introduced in PHP versions >= 5.6.0
Demo
Access the desired elements by key and create a new array using these items, like this:
$anims = array(
'Bouncing Entrances' => $anims['Bouncing Entrances'],
'Fading Entrances' => $anims['Fading Entrances'],
// ...
);
EDIT: forgot to preserve the keys.
If there's a certain pattern by which you want to filter the animations, you can use array_filter() function, like #Uchiha said.
I have a multilevel array as below
array(
(int)0=>array(
'User' => array(
'PostType' => array(
'PHP' => array(
'id' => '2',
'type_title' => 'Core Questions',
'type_description' => 'none',
'type_sort_order' => '7'
'Post'=>array(
......
),
),
'ASP' => array(
'id' => '1',
'type_title' => 'Core Questions',
'type_description' => 'none',
'type_sort_order' => '1'
'Post'=>array(
......
),
),
),
)));
I have fetched a user with its post categorized by postType
postType has type_sort_order field
I want to sort sub array PostType by type_sort_order field
so that ASP come before PHP
I tried usort as below
usort($arr,function(){
return ($a[0]['User']['PostType']['post_sort_order'] < $b[0]['User']['PostType']['post_sort_order'])?1:-1;
});
and also many other sort but not getting correct result
array_multisort will work. Solution as follows:
$list = //Your array
$sortOrders = array();
foreach ($list[0]['User']['PostType'] as $postType) {
$sortOrders[] = $postType['type_sort_order'];
}
array_multisort($sortOrders, $list[0]['User']['PostType']);
This will grab all the sort orders in an array ordered the same as your starting array. Using array_multisort on both arrays will sort the first array, and also apply that new order to the second array, giving you the result you're after.
Your looking for http://php.net/manual/en/function.array-multisort.php
Loop though PostType and take all type_sort_order and place in a seperate array. then use that array as an argument for array_multi_sort
it seems you are just trying to sort the sub array PostType so just pass that array
I want to create a multidimensional array through class object. Don't know the syntax. Kindly guide me regarding that.
class gridcell {
var $place;
var $latitude;
$spatial=array();
$temporal=array();
$taxi=array();
$raiwind = new gridell();
$raiwind->place("raiwind");
$raiwind->latitude("31.4279");
Now I wanted to put the following array in the $raiwind->$spatial
What would be its synatx. Different objects have different elements in $spatial array.
spatial(
array(
array(
'lda',
'31.4104',
3
),
array(
'ali',
'31.3998',
3
),
array(
'multan',
'31.4675',
10
)
)
);
Simple as this:
$raiwind->spatial[] = array(
array('lda','31.4104',3),
array('ali','31.3998',3),
array('multan','31.4675',10)
);
I don't understand the docs regarding the Hash 'path', so I've had no luck. I'm trying to sort each layer alphabetically:
array(
'music' => array(
'genre' => array(
(int) 0 => 'Dubstep',
(int) 1 => 'Blues',
(int) 2 => 'Classical'
),
'instrument' => array(
(int) 0 => 'Guitar (Electric)',
(int) 1 => 'Bassoon',
(int) 2 => 'Harmonica (Diatonic)'
),
'anotherLot' => array(
I need to sort the first later of arrays by key, then the second later in each by key, and the third by the values, so I imagine the two deeper layers would be done with a nested foreach.
I'm not familiar with CakePHP's Hash class, but here is a plain PHP solution:
ksort($data); // sort main array by keys
foreach ($data as &$outer)
{
ksort($outer); // sort next layer by keys
foreach($outer as &$inner)
{
asort($inner); // sort inner arrays by values
}
}
I want to sort an array by 'hits', but I also want to look for a particular ID and set that as the first iteration, then continue the 'hits' sort.
For example, I have a multidimensional array:
$myarray = array(
array(
"id"=>10,
"hits"=>80
),
array(
"id"=>14,
"hits"=>50
),
array(
"id"=>15,
"hits"=>700
),
array(
"id"=>18,
"hits"=>200
)
);
I want to test whether the id is something particular, i.e. if the id==18 then put it first, then sort by hits. How would I do this, using usort and a custom function?
I think I'm looking for something similar to:
function customsort($a,$b){
if($a["id"]==18){ //or b==18?
return -1;
} else {
return $a["hits"]>$b["hits"];
}
}
usort($myarray,"customsort");
The outcome I would like is for the order to be :
array(
"id"=>18,
"hits"=>200
),
array(
"id"=>14,
"hits"=>50
),
array(
"id"=>10,
"hits"=>80
),
array(
"id"=>15,
"hits"=>700
)
(or if they were labelled ABCD then I need it to be DBAC)
The only thing in your code that might make this NOT work is the return $a["hits"]>$b["hits"];. Your function should return 1/-1 only (not true/false), so change that line to: return $a["hits"]>$b["hits"]?1:-1; and it should work as expected.
Sure enough, it works: http://codepad.org/ItyIa7fB