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);
Related
Could you please help me? I need to know how to access the values of this response from an API call using PHP.
Array ( [queryIds] => Array ( [0] => lHLLapn7EH ) [timedOutRealtimeData] => [realtimeDataMissing] => [cacheFreshness] => FRESH [prunedResult] => [novaCost] => 0 [novaRequestDuration] => 19 [wasCached] => 1 [minSampleRate] => 1 [timeComputed] => 1558794556710 [novaRuntime] => 199 [hitChunkGroupByLimit] => [throttleTime] => 0 [data] => Array ( [series] => Array ( [0] => Array ( [0] => Array ( [setId] => [value] => 843 ) ) [1] => Array ( [0] => Array ( [setId] => [value] => 22 ) ) ) [seriesLabels] => Array ( [0] => Array ( [0] => 0 [1] => C5241; GB ) [1] => Array ( [0] => 0 [1] => C9999; GB ) ) [seriesMeta] => Array ( [0] => Array ( [eventGroupBys] => Array ( [0] => C5241 [1] => GB ) [segmentIndex] => 0 [eventIndex] => 0 [eventGroupBy] => C5241; GB ) [1] => Array ( [eventGroupBys] => Array ( [0] => C9999 [1] => GB ) [segmentIndex] => 0 [eventIndex] => 0 [eventGroupBy] => C9999; GB ) ) [seriesCollapsed] => Array ( [0] => Array ( [0] => Array ( [setId] => [value] => 843 ) ) [1] => Array ( [0] => Array ( [setId] => [value] => 22 ) ) ) [xValues] => Array ( [0] => 2019-05-25 ) ) [partialMergedAndNewUserInformation] => [transformationIds] => Array ( [0] => 2696 ) [backend] => novaV2 )
You can access them using
Array ( [0] => C9999 [1] => GB ) -> $arr['data']['seriesMeta'][0]['eventGroupBys']
Array ( [setId] => [value] => 20) -> $arr['data']['seriesMeta'][0][0]
I have a problem with merging array's. I'll try array merge and combine but nothing is working.
First array
Array (
[type1] => Array (
[userid] => Array (
[0] => 35
[1] => 37
)
[from] => Array (
[0] => 07-06-2017
[1] => 09-06-2017
)
[till] => Array (
[0] => 07-07-2017
[1] => 09-07-2017
)
)
[type3] => Array (
[userid] => Array (
[0] => 13
)
[from] => Array (
[0] => 10-06-2017
)
[till] => Array (
[0] => 10-07-2017
)
)
)
Second array
The second array is filled with room details, but the assigned users are not added yet.
Array (
[type1] => Array (
[m2] =>
[price] =>
[rooms] => 1
[extra] =>
[rented_to] => Array
(
[0] => Array
(
[userid] =>
[from] =>
[till] =>
)
)
)
[type3] => Array
(
[m2] =>
[price] =>
[rooms] => 1
[extra] =>
[rented_to] => Array
(
[0] => Array
(
[userid] =>
[from] =>
[till] =>
)
)
)
)
I'll will put these arrays together to one array, so that the data is insert into the "rented_to" section. How can I get this array into an another array like this:
Array (
[type1] => Array (
[m2] =>
[price] =>
[rooms] => 1
[extra] =>
[rented_to] => Array
(
[0] => Array
(
[userid] => 35
[from] => 07-06-2017
[till] => 07-07-2017
)
[1] => Array
(
[userid] => 37
[from] => 09-06-2017
[till] => 09-07-2017
)
)
)
[type3] => Array
(
[m2] =>
[price] =>
[rooms] => 1
[extra] =>
[rented_to] => Array
(
[0] => Array
(
[userid] => 13
[from] => 10-06-2017
[till] => 10-07-2017
)
)
)
)
The code
$manager_add_new_room_rented_to is an array that contains the value of the first array in my example.
$manager_add_new_room_type_desc = $_POST['manager_add_new_room_type_desc'];
$manager_add_new_room_type_m2 = $_POST['manager_add_new_room_type_m2'];
$manager_add_new_room_type_rent_price = $_POST['manager_add_new_room_type_rent_price'];
$manager_add_new_room_type_rooms = $_POST['manager_add_new_room_type_rooms'];
$manager_add_new_room_type_extra = $_POST['manager_add_new_room_type_extra'];
$manager_add_new_room_rented_to = $_POST['manager_add_new_room_rented_to'];
$add_new_room_information = array();
foreach ( $manager_add_new_room_type_desc as $key => $room_type ) :
$add_new_room_information[$room_type] = array(
'm2' => $manager_add_new_room_type_m2[$key],
'price' => $manager_add_new_room_type_rent_price[$key],
'rooms' => $manager_add_new_room_type_rooms[$key],
'extra' => $manager_add_new_room_type_extra[$key],
'rented_to' => array(
array(
'userid' => '',
'from' => '',
'till' => ''
)
)
);
endforeach;
Loop through $manager_add_new_room_rented_to[$room_type] and create the new array that you want.
foreach ( $manager_add_new_room_type_desc as $key => $room_type ) :
$renters = $manager_add_new_room_rented_to[$room_type];
$rented_to = array();
foreach ($renters['userid'] as $index => $userid) :
$rented_to[] = array('userid' => $userid, 'from' => $renters['from'][$index], 'to' => $renters['to'][$index]);
endforeach;
$add_new_room_information[$room_type] = array(
'm2' => $manager_add_new_room_type_m2[$key],
'price' => $manager_add_new_room_type_rent_price[$key],
'rooms' => $manager_add_new_room_type_rooms[$key],
'extra' => $manager_add_new_room_type_extra[$key],
'rented_to' => $rented_to
)
);
endforeach;
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);
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
);
}
I am new to php. I need some help.
I had a array as
Array ( [_] => Array ( [0] => [1] => )
[123_] => Array ( [0] => 123 [1] => )
[1234_] => Array ( [0] => 1234 [1] => )
)
Array ( [_] => Array ( [0] => [1] => )
[12345_] => Array ( [0] => 12345 [1] => )
[1234_] => Array ( [0] => 1234 [1] => )
)
so..whats my problem is i want an array with all these keys and values as
Array ( [_] => Array ( [0] => [1] => )
[123_] => Array ( [0] => 123 [1] => )
[1234_] => Array ( [0] => 1234 [1] => )
[_] => Array ( [0] => [1] => )
[12345_] => Array ( [0] => 12345 [1] => )
[1234_] => Array ( [0] => 1234 [1] => )
)
there would be duplicate keys and values.. but I want all of them as a array.. any help plz..
That is not possible. A PHP array cannot have two identical keys.
As the others said, it's impossible to have a single array with duplicate keys. But you can build an array of array :
<?php
$arr1 = array( '_' => Array ( '0' => '', '1' => ''),
'123_' => Array ( '0' => 123, '1' => ''),
'1234_' => Array ( '0' => 1234, '1' => '')
);
$arr2 = array ( '_' => Array ( '0' => '', '1' => ''),
'12345_' => Array ( '0' => 12345, '1' => ''),
'1234_' => Array ( '0' => 1234, '1' => '')
);
$result = array();
foreach( $arr1 as $key => $val) {
$result[] = array('key'=>$key, 'value'=>$val);
}
foreach( $arr2 as $key => $val) {
$result[] = array('key'=>$key, 'value'=>$val);
}
print_r($result);
?>
Ouput:
Array
(
[0] => Array
(
[key] => _
[value] => Array
(
[0] =>
[1] =>
)
)
[1] => Array
(
[key] => 123_
[value] => Array
(
[0] => 123
[1] =>
)
)
[2] => Array
(
[key] => 1234_
[value] => Array
(
[0] => 1234
[1] =>
)
)
[3] => Array
(
[key] => _
[value] => Array
(
[0] =>
[1] =>
)
)
[4] => Array
(
[key] => 12345_
[value] => Array
(
[0] => 12345
[1] =>
)
)
[5] => Array
(
[key] => 1234_
[value] => Array
(
[0] => 1234
[1] =>
)
)
)
Have a look at PHP's array_merge()-function.