How can I sort this array by the value of the "seasons" field first, then of the "series" field? And save the keys
Array
(
[2] => Array
(
[0] => Array
(
[season] => 2
[series] => 3
[title] => Boom
)
[1] => Array
(
[season] => 2
[series] => 1
[title] => Boom
)
[2] => Array
(
[season] => 2
[series] => 2
[title] => Boom
)
)
[1] => Array
(
[0] => Array
(
[season] => 1
[series] => 1
[title] => Boom
)
[1] => Array
(
[season] => 1
[series] => 3
[title] => Boom
)
[2] => Array
(
[season] => 1
[series] => 2
[title] => Boom
)
)
)
)
usort, ksort functions does not save the keys, but sorting by seasons and series values really good
After usort function:
Array
(
[0] => Array
(
[0] => Array
(
[season] => 1
[series] => 1
[title] => Boom
)
[1] => Array
(
[season] => 1
[series] => 2
[title] => Boom
)
[2] => Array
(
[season] => 1
[series] => 3
[title] => Boom
)
)
[1] => Array
(
[0] => Array
(
[season] => 2
[series] => 1
[title] => Boom
)
[1] => Array
(
[season] => 2
[series] => 2
[title] => Boom
)
[2] => Array
(
[season] => 2
[series] => 3
[title] => Boom
)
)
)
Who can help me solve this problem?
Solved by array_walk
array_walk($arr, function($a, $b){
return $a['season'] - $b['season'];
});
Related
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'))){
Below are my arrays echoed inside a for-each loop.
Array
(
[0] => 1
[1] => 2
[2] => 2
)
Array
(
[0] => 1
[1] => 2
[2] => 1
)
Array
(
[0] => 2
[1] => 1
[2] => 1
)
Array
(
[0] => 3
[1] => 3
[2] => 1
)
Array
(
[0] => 3
[1] => 3
[2] => 3
)
Array
(
[0] => 3
[1] => 3
[2] => 2
)
Array
(
[0] => 4
[1] => 2
[2] => 2
)
Array
(
[0] => 4
[1] => 2
[2] => 1
)
I would like to group these arrays based on the value of the first item (index = 0) and get the following dimension array.
Array
(
[1] => Array
(
[0] => 1
[1] => 2
[2] => 2
)
[2] => Array
(
[0] => 1
[1] => 2
[2] => 1
)
)
Array
(
[3] => Array
(
[0] => 2
[1] => 1
[2] => 1
)
)
Array
(
[4] => Array
(
[0] => 3
[1] => 3
[2] => 1
)
[5] => Array
(
[0] => 3
[1] => 3
[2] => 3
)
[6] => Array
(
[0] => 3
[1] => 3
[2] => 2
)
)
Array
(
[7] => Array
(
[0] => 4
[1] => 2
[2] => 2
)
[8] => Array
(
[0] => 4
[1] => 2
[2] => 1
)
)
I have spent hours to figure this out but due to less experience I still cannot get this done. Please help me with some algoritme.
Won't that do the trick?
$result = [];
foreach ($originalArrays as $array) {
$result[$array[0]][] = $array;
}
I want a recursive function to group all child categories under root category. I've an array like this;
Array
(
[0] => Array
(
[uid] => 1
[title] => Car
[Child] => Array
(
[0] => Array
(
[uid] => 3
[title] => Color
[Child] => Array
(
[0] => Array
(
[uid] => 5
[title] => Red
)
[1] => Array
(
[uid] => 6
[title] => Blue
)
)
)
[1] => Array
(
[uid] => 4
[title] => Door
)
)
)
[1] => Array
(
[uid] => 2
[title] => Two Wheeler
[Child] => Array
(
[0] => Array
(
[uid] => 7
[title] => Type
[Child] => Array
(
[0] => Array
(
[uid] => 9
[title] => Scooter
)
[1] => Array
(
[uid] => 10
[title] => Bike
)
)
)
[1] => Array
(
[uid] => 8
[title] => Company
)
)
)
)
My requirement is to bring all the subchild values to the main category child value. I mean I need the following structure;
Array
(
[0] => Array
(
[uid] => 1
[title] => Car
[Child] => Array
(
[0] => Array
(
[uid] => 3
[title] => Color
)
[1] => Array
(
[uid] => 4
[title] => Door
)
[2] => Array
(
[uid] => 5
[title] => Red
)
[3] => Array
(
[uid] => 6
[title] => Blue
)
)
)
[1] => Array
(
[uid] => 2
[title] => Two Wheeler
[Child] => Array
(
[0] => Array
(
[uid] => 7
[title] => Type
)
[1] => Array
(
[uid] => 8
[title] => Company
)
[2] => Array
(
[uid] => 9
[title] => Scooter
)
[3] => Array
(
[uid] => 10
[title] => Bike
)
)
)
)
)
How can I achieve this using a PHP function?
I have the following 2D-Array:
Array (
[ID] => Array
(
[0] => 150
[1] => 250
)
[Group] => Array
(
[0] => 120
[1] => 120
)
[Name] => Array
(
[0] => Name 1
[1] => Name 2
)
[Price] => Array
(
[0] => 9.99
[1] => 11.99
)
[Date] => Array
(
[0] => 12.04.2013
[1] => 11.04.2013
)
[SortIndex] => Array
(
[0] => 20
[1] => 10
)
)
and I want to sort them after the SortIndex-Array ascending. But the different Values from the Arrays are related to the others. So the association can't be changed.
How I could sort them?
Is there is any reason not to convert array to more suitable form?
Array (
[0] => Array
(
[ID] => 150
[Group] => 120
)
[1] => Array
(
[ID] => 250
[GROUP] => 120
)
....
I've been looking for the follow solution to create the array structure I need.
For a a list with updates for a specific user who can be following different things, I need to get first the followed items and the data compare to this item. my output for this is as follow.
array how it is right now
Array
(
[0] => Array
(
[website_id] => Array
(
[0] => Array
(
[id] => 1
[follower_id] => 1
)
[1] => Array
(
[id] => 1
[follower_id] => 2
)
[2] => Array
(
[id] => 1
[follower_id] => 3
)
[3] => Array
(
[id] => 1
[follower_id] => 4
)
[4] => Array
(
[id] => 1
[follower_id] => 5
)
)
)
[1] => Array
(
[website_id] => Array
(
[0] => Array
(
[id] => 1
[follower_id] => 1
)
)
)
)
I want to create the above array like tho on below
array like it has to be
Array
(
[0] => Array
(
[0] => Array
(
[id] => 1
[follower_id] => 1
)
[1] => Array
(
[id] => 1
[follower_id] => 2
)
[2] => Array
(
[id] => 1
[follower_id] => 3
)
[3] => Array
(
[id] => 1
[follower_id] => 4
)
[4] => Array
(
[id] => 1
[follower_id] => 5
)
[5] => Array
(
[id] => 1
[follower_id] => 1
)
)
Thanks in advice.
With kind regards,
Nicky
Seems to me that array_merge() should be sufficient:
$newArray = array_merge($myArray[0]['website_id'], $myArray[1]['website_id']);