Rebuild php array structure - php

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']);

Related

Extract count of multidimensional array

i have a multidimensional array from a ldap search stored in $data:
Array
(
[count] => 1
[0] => Array
(
[objectclass] => Array
(
[count] => 3
[0] => top
[1] => posixGroup
[2] => sambaGroupMapping
)
[0] => objectclass
[cn] => Array
(
[count] => 1
[0] => xxxxx
)
[1] => cn
[gidnumber] => Array
(
[count] => 1
[0] => xxxx
)
[2] => gidnumber
[sambasid] => Array
(
[count] => 1
[0] => xxxx
)
[3] => sambasid
[sambagrouptype] => Array
(
[count] => 1
[0] => 2
)
[4] => sambagrouptype
[displayname] => Array
(
[count] => 1
[0] => xxxx
)
[5] => displayname
[memberuid] => Array
(
[count] => 2
[0] => xxxxx
[1] => xxxxx
)
[6] => memberuid
[count] => 7
[dn] => cn=xx,ou=xx,dc=xx,dc=xx
)
)
I would like to extract the count of the memberuid: [count] => 2 but i'm not able to do it correctly. I can't figoure out the structure..
I tried: $data[0]['displayname']['memberuid']['count'] which gives me an Illegal string offset
Thanks for your help

Compare values of two multidimentional array and insert if not exits

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'))){

Sort Multi-dimensional Array by Value and save

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'];
});

How to make normal arrays become a dimension array based on the first item?

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;
}

sorting 2D-Array in PHP

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
)
....

Categories