Merge array with multiple keys (php) - php

The array below is my current situation. Through a loop new data get's added.
I've tried 'array_merge_recursive' as well as this this accepted answer. But it doesn't seem to work or I'm using it wrong.
Array (
[0] => Array (
[Customer] => Array (
[Weekend] => Array (
[2016] => Array (
[01] => Array (
[0] => Array (
[id] => 54
[startDate] => 01-01-2016
[endDate] => 31-12-2016
[price] => 0
)
)
)
)
)
)
[1] => Array (
[Customer] => Array (
[Weekend] => Array (
[2018] => Array (
[01] => Array (
[0] => Array (
[id] => 56
[startDate] => 01-01-2018
[endDate] => 31-12-2018
[price] => 0
)
)
)
)
)
)
[2] => Array (
[Customer] => Array (
[Weekend] => Array (
[2019] => Array (
[01] => Array (
[0] => Array (
[id] => 57
[startDate] => 01-01-2019
[endDate] => 31-12-2019
[price] => 0
)
)
)
)
)
)
)
Desired situation is something like this:
Array (
[Customer] => Array (
[Weekend] => Array (
[2016] => Array (
[01] => Array (
[0] => Array (
[id] => 54
[startDate] => 01-01-2016
[endDate] => 31-12-2016
[price] => 0
)
)
)
[2018] => Array (
[01] => Array (
[0] => Array (
[id] => 56
[startDate] => 01-01-2018
[endDate] => 31-12-2018
[price] => 0
)
)
)
[2019] => Array (
[01] => Array (
[0] => Array (
[id] => 57
[startDate] => 01-01-2019
[endDate] => 31-12-2019
[price] => 0
)
)
)
)
)
)
If any other information is required, please ask! new here

Pretty sure this will work:
$result = call_user_func_array('array_merge_recursive', $array);

This should do what you want:
$new_arr = [];
$arr = // Your current array;
foreach ($arr as $customer)
{
foreach ($customer['Weekend'] as $year => $z)
{
foreach($z as $month => $y)
{
foreach($y as $entry)
{
Store($year, $month, $entry, $new_arr);
}
}
}
}
function Store($year, $month, $entry, & $new_arr)
{
if ( ! isset($new_arr['customer'][$year]))
{
$new_arr['customer'][$year] = array();
}
if ( ! isset($new_arr['customer'][$year][$month]))
{
$new_arr['customer'][$year][$month] = array();
}
$new_arr['customer'][$year][$month][] = $entry;
}

Related

Unable to extract data from array in php

The array is received from the facebook api and i am not able to extract the likes array from the array,Please help me
[data] => Array (
[0] => Array (
[message] => Hello
[id] => 729659027165160_729651713832558
[likes] => Array (
[data] => Array (
[0] => Array (
[id] => 729659027165160
)
)
[paging] => Array (
[cursors] => Array (
[after] => NzI5NjU5MDI3MTY1MTYw
[before] => NzI5NjU5MDI3MTY1MTYw
)
)
)
)
[1] => Array (
[id] => 729659027165160_718306454967084
[likes] => Array (
[data] => Array (
[0] => Array (
[id] => 1719747118259908
)
)
[paging] => Array (
[cursors] => Array (
[after] => MTcxOTc0NzExODI1OTkwOA==
[before] => MTcxOTc0NzExODI1OTkwOA==
)
)
)
)
[2] => Array (
[id] => 729659027165160_541135166017548
[likes] => Array (
[data] => Array (
[0] => Array (
[id] => 1162428970453842
)
)
[paging] => Array (
[cursors] => Array (
[after] => MTE2MjQyODk3MDQ1Mzg0Mg==
[before] => MTE2MjQyODk3MDQ1Mzg0Mg==
)
)
)
)
[3] => Array (
[message] => Panipaata leni prathivaadu philosophy cheppevade.... Wish Facebook introduce an unlike button soon!!!!
[id] => 729659027165160_520677651396633
[likes] => Array (
[data] => Array (
[0] => Array (
[id] => 1162428970453842
)
[1] => Array (
[id] => 806391372817118
)
[2] => Array (
[id] => 928633297192567
)
[3] => Array (
[id] => 824812004311172
)
[4] => Array (
[id] => 10207344532684729
)
[5] => Array (
[id] => 1188171664544003
)
)
[paging] => Array (
[cursors] => Array (
[after] => MTE4ODE3MTY2NDU0NDAwMw==
[before] => MTE2MjQyODk3MDQ1Mzg0Mg==
)
)
)
)
[4] => Array (
[id] => 729659027165160_110578795739856
[likes] => Array (
[data] => Array (
[0] => Array (
[id] => 1162428970453842
)
)
[paging] => Array (
[cursors] => Array (
[after] => MTE2MjQyODk3MDQ1Mzg0Mg==
[before] => MTE2MjQyO
)
)
)
)
)
I am able to extract the id from the above array,but unable to extract the count of likes and message.
Try this code, it iterates in your array and stores all ids and stores the message, the likes Array and the number of likes only if they exist (Supposing that your array is named $myarray):
$result = array();
foreach($myarray['data'] as $data){
$item = array();
$item['id'] = $data['id'];
if( isset($data['message']) || isset($data['likes']) ){
if(isset($data['message'])) $item['message'] = $data['message'];
if(isset($data['likes'])) {
$item['likes'] = array();
foreach($data['likes']['data'] as $like){
$item['likes'][] = $like['id'];
}
$item['countlikes'] = count( $data['likes']['data'] );
}
}
$result[] = $item;
}
print_r($result);
With your example Array the result will be:
Array
(
[0] => Array
(
[id] => 729659027165160_729651713832558
[message] => Hello
[likes] => Array
(
[0] => 729659027165160
)
[countlikes] => 1
)
[1] => Array
(
[id] => 729659027165160_718306454967084
[likes] => Array
(
[0] => 1719747118259908
)
[countlikes] => 1
)
[2] => Array
(
[id] => 729659027165160_541135166017548
[likes] => Array
(
[0] => 1162428970453842
)
[countlikes] => 1
)
[3] => Array
(
[id] => 729659027165160_520677651396633
[message] => Panipaata leni prathivaadu philosophy cheppevade.... Wish Facebook introduce an unlike button soon!!!!
[likes] => Array
(
[0] => 1162428970453842
[1] => 806391372817118
[2] => 928633297192567
[3] => 824812004311172
[4] => 10207344532684729
[5] => 1188171664544003
)
[countlikes] => 6
)
[4] => Array
(
[id] => 729659027165160_110578795739856
[likes] => Array
(
[0] => 1162428970453842
)
[countlikes] => 1
)
)

Parse a multi dimensional array and pull values

How can i parse the below multi dimensional array ($array) and pull values of [productType] , [totalPrice]and [productCategory] if [packageCode] is matching with the value of $pkgcodes[1]...[z]
$pkgcodes is an array of codes
print_r of $pkgcodes
Array ( [0] => TRA1I2 [1] => TREZEC [n] ...)
The array $array is a response from SOAP client
print_r of $array
Array (
[0] => Array (
[packageCode] => TRA1I2
[totalPrice] => 17
[productType] => product Only
[products] => Array (
[0] => Array (
[productCategory] => Simple
[paxes] => Array (
[0] => Array (
[paxType] => Adult
[age] => 30 )
[1] => Array (
[paxType] => Adult
[age] => 30 ) )
[totalproductRate] => 17
[ratesPerNight] => Array (
[0] => Array (
[date] => 2015-01-28
[amount] => 17 ) ) ) ) )
[1] => Array (
[packageCode] => TREZEC
[totalPrice] => 17
[productType] => product Only
[products] => Array (
[0] => Array (
[productCategory] => Complicated
[paxes] => Array (
[0] => Array (
[paxType] => Adult
[age] => 30 )
[1] => Array (
[paxType] => Adult
[age] => 30 ) )
[totalproductRate] => 17
[ratesPerNight] => Array (
[0] => Array (
[date] => 2015-01-28
[amount] => 17 ) ) ) ) ) ).
You help is more appreciated
Try with -
$newArr = array();
foreach($array as $value) {
if (in_array($value['packageCode'], $pkgcodes)) {
$temp['productType'] = $value['productType'];
$temp['totalPrice'] = $value['totalPrice'];
$temp['packageCode'] = $value['packageCode'];
$temp['productCategory'] = $value['products']['productCategory'];
$newArr[] = $temp;
}
}
var_dump($newArr);

how to sort array in a different order

I have an array which looks like this.
Array
(
[0] => Array
(
[cartId] => 667
[isPack] => 1
)
[1] => Array
(
[cartId] => 668
[isPack] => 1
)
[2] => Array
(
[cartId] => 672
[isPack] => 2
)
[3] => Array
(
[cartId] => 673
[isPack] => 2
)
)
I want to make an array by isPack element,which should look something like this.
Array (
[1] => Array
(
[0] => Array
(
[cartId] => 667
)
[1] => Array
(
[cartId] => 668
)
)
[2] => Array
(
[0] => Array
(
[cartId] => 672
)
[1] => Array
(
[cartId] => 673
)
)
)
Array two will be two array in which isPack will be array key,and other information will belong to its subarray.
How can I do this?
thanks in advance :)
$new=array();
$arr = array( array('cartId' => 667,'isPack' => 1),array('cartId' => 668,'isPack' => 1),array('cartId' => 672,'isPack' => 2),array('cartId' => 673,'isPack' => 2));
for($i=0;$i<count($arr);$i++) {
$l = $arr[$i]['isPack'];
$new[$l][]['catid']= $arr[$i]['cartId'];
}
print_r($new);
and output:-
Array
(
[1] => Array
(
[0] => Array
(
[catid] => 667
)
[1] => Array
(
[catid] => 668
)
)
[2] => Array
(
[0] => Array
(
[catid] => 672
)
[1] => Array
(
[catid] => 673
)
)
)
Try with:
$input = array( /* your data*/ );
$output = array();
foreach ( $input as $data ) {
$isPack = $data['isPack'];
$cartId = $data['cartId'];
if ( !isset($output[$isPack]) ) {
$output[$isPack] = array();
}
$output[$isPack][] = array(
'cartId' => $cartId
);
}

Arrange an array by weeks

I have an array in which I have field called date and what I need is to separate all these arrays into weeks. Is it posible to do so? Here is my code:
function getWeeks($query){
$postdate = $query['response']['posts']['date'];
return $posts;
}
Here is part of my array:
Array ( [date] => 07/30/12 [message] => test [post_id] => 1 [impressions] => Array ( [0] => 9638 ) [consumptions] => Array ( [0] => 38 ) [storytellers] => Array ( [0] => 6 ) [engaged_users] => Array ( [0] => 31 ) [story_adds] => Array ( [0] => 6 ) [impressions_unique] => Array ( [0] => 4700 ) [comment] => Array ( [0] => 1 ) [like] => Array ( [0] => 5 ) [share] => Array ( [0] => 0 ) [virality] => Array ( [0] => 0 ) [lifetime] => Array ( [0] => 0 ) [affinity] => Array ( [0] => 0 ) )
Array ( [date] => 07/30/12 [message] => test2 [post_id] => 2 [impressions] => Array ( [0] => 10552 ) [consumptions] => Array ( [0] => 47 ) [storytellers] => Array ( [0] => 5 ) [engaged_users] => Array ( [0] => 44 ) [story_adds] => Array ( [0] => 5 ) [impressions_unique] => Array ( [0] => 4982 ) [comment] => Array ( [0] => 0 ) [like] => Array ( [0] => 4 ) [share] => Array ( [0] => 1 ) [virality] => Array ( [0] => 0 ) [lifetime] => Array ( [0] => 0 ) [affinity] => Array ( [0] => 0 ) )
This will loop through each of your post items and group them together if they are in the same week.
View an Example
<?php
$posts = array(
array('date' => '7/30/10', 'title' => 'july post'),
array('date' => '7/19/10', 'title' => 'another post in july'),
array('date' => '7/22/10', 'title' => 'sup, this will be with another post')
);
$grouped_posts = array();
foreach( $posts as $post ) {
// #see http://us2.php.net/manual/en/function.date.php
$week = date('W', strtotime($post['date']));
// create new empty array if it hasn't been created yet
if( !isset($grouped_posts[$week]) ) {
$grouped_posts[$week] = array();
}
// append the post to the array
$grouped_posts[$week][] = $post;
}
print_r($grouped_posts);

How to combine arrays in php?

I have these arrays:
$array1
Array
(
[0] => Array
(
[state] => AE
[state_pri] => 0
)
[1] => Array
(
[state] => AK
[state_pri] => 0
)
[2] => Array
(
[state] => AL
[state_pri] => 0
)
)
$array2
Array
(
[0] => Array
(
[0] => Array
(
[count1] => 0
)
)
[1] => Array
(
[0] => Array
(
[count1] => 1
)
)
[2] => Array
(
[0] => Array
(
[count1] => 18
)
)
)
$array3
Array
(
[0] => Array
(
[0] => Array
(
[count] => 0
)
)
[1] => Array
(
[0] => Array
(
[count] => 1
)
)
[2] => Array
(
[0] => Array
(
[count] => 18
)
)
)
...and I would like to get something like this:
Array
(
[0] => Array
(
[state] => AE
[state_pri] => 0
[0] => Array
(
[count] => 0
)
[0] => Array
(
[count1] => 0
)
)
[1] => Array
(
[state] => AK
[state_pri] => 0
[1] => Array
(
[count] => 0
)
[1] => Array
(
[count1] => 0
)
)
[2] => Array
(
[state] => AL
[state_pri] => 0
[2] => Array
(
[count] => 0
)
[2] => Array
(
[count1] => 0
)
)
)
Any ideas on how to do this?
Edit: Just to add some more code, if I use array_merge I get:
Array
(
[0] => Array
(
[state] => AE
[state_pri] => 0
)
[1] => Array
(
[state] => AK
[state_pri] => 0
)
[2] => Array
(
[state] => AL
[state_pri] => 0
)
[3] => Array
(
[0] => Array
(
[count] => 0
)
)
[4] => Array
(
[0] => Array
(
[count] => 1
)
)
[5] => Array
(
[0] => Array
(
[count] => 18
)
)
.....
)
What you're looking for is still a bit difficult to make out. Let me know if this achieves the desired results:
foreach ($array1 as $key => $val) {
$array1[$key][] = $array3[$key];
$array1[$key][] = $array2[$key];
}
print_r($array1);
What about the array_merge function?
http://www.php.net/manual/en/function.array-merge.php
$merged = array();
foreach (array($array1,$array2,$array3) as $array) {
foreach ($array as $key=>$value) {
if (!isset($merged[$key])) {
$merged[$key] = array();
}
$merged[$key] += $value;
}
}
print_r($merged);
$combined=$array0 + $array1;
Try that one

Categories