Anyone know how to reach $wanted_array given the $initial_array dynamically?
Thanks!
// $initial_array
Array
(
[0] => Array
(
[thingy] => 4
[date] => 27/11/2013
)
[1] => Array
(
[thingy] => 4
[date] => 27/11/2013
)
[2] => Array
(
[thingy] => 2
[date] => 27/11/2015
)
[3] => Array
(
[thingy] => 2
[date] => 27/11/2015
)
[4] => Array
(
[thingy] => 2
[date] => 27/11/2015
)
)
// $wanted_array
// The quantity key will count instances that array item in $initial_array
Array
(
[0] => Array
(
[thingy] => 4
[date] => 27/11/2013
[quantity] = 2
)
[1] => Array
(
[thingy] => 2
[date] => 27/11/2015
[quantity] = 3
)
I've broken this down into two steps.
Arranging data according to dates and storing the result in $result variable.
$result = array();
foreach($array as $arr) {
$result[$arr['date']][] = $arr;
}
This gives:
Array
(
[27/11/2013] => Array
(
[0] => Array
(
[thingy] => 4
[date] => 27/11/2013
)
[1] => Array
(
[thingy] => 4
[date] => 27/11/2013
)
)
[27/11/2015] => Array
(
[0] => Array
(
[thingy] => 2
[date] => 27/11/2015
)
[1] => Array
(
[thingy] => 2
[date] => 27/11/2015
)
[2] => Array
(
[thingy] => 2
[date] => 27/11/2015
)
)
)
Looping through the array and formatting it as per our needs. Note that the sub array length indicates the quantity.
foreach($result as $res) {
$final[] = array_merge(
current($res),
array("quantity" => count($res))
);
}
Output:
Array
(
[0] => Array
(
[thingy] => 4
[date] => 27/11/2013
[quantity] => 2
)
[1] => Array
(
[thingy] => 2
[date] => 27/11/2015
[quantity] => 3
)
)
Related
How to get all the pricing values from the JSON array? I need to split two segments tld's (com,in,in.net,etc...) and the corresponding tld pricing value I tried to get the values from the array but com, in etc those are key values.
My JSON Array
Array
(
[result] => success
[pricing] => Array
(
[com] => Array
(
[categories] => Array
(
[0] => gTLD
[1] => Popular
)
[addons] => Array
(
[dns] => 1
[email] => 1
[idprotect] => 1
)
[group] => sale
[register] => Array
(
[1] => 637.70
)
[transfer] => Array
(
[1] => 637.70
)
[renew] => Array
(
[1] => 637.70
)
)
[in] => Array
(
[categories] => Array
(
[0] => ccTLD
[1] => Geography
)
[addons] => Array
(
[dns] => 1
[email] => 1
[idprotect] => 1
)
[group] => sale
[register] => Array
(
[1] => 1014.67
)
[transfer] => Array
(
[1] => 1014.67
)
[renew] => Array
(
[1] => 1014.67
)
)
[info] => Array
(
[categories] => Array
(
[0] => gTLD
[1] => Popular
)
[addons] => Array
(
[dns] => 1
[email] => 1
[idprotect] => 1
)
[group] => sale
[register] => Array
(
[1] => 200.00
)
[transfer] => Array
(
[1] => 200.00
)
[renew] => Array
(
[1] => 200.00
)
)
[net] => Array
(
[categories] => Array
(
[0] => gTLD
[1] => Popular
)
[addons] => Array
(
[dns] => 1
[email] => 1
[idprotect] => 1
)
[group] => sale
[register] => Array
(
[1] => 829.76
)
[transfer] => Array
(
[1] => 829.76
)
[renew] => Array
(
[1] => 829.76
)
)
[biz] => Array
(
[categories] => Array
(
[0] => gTLD
[1] => Popular
)
[addons] => Array
(
[dns] => 1
[email] => 1
[idprotect] => 1
)
[group] => sale
[register] => Array
(
[1] => 878.33
)
[transfer] => Array
(
[1] => 878.33
)
[renew] => Array
(
[1] => 878.33
)
)
[org] => Array
(
[categories] => Array
(
[0] => gTLD
[1] => Popular
)
[addons] => Array
(
[dns] => 1
[email] => 1
[idprotect] => 1
)
[group] => sale
[register] => Array
(
[1] => 939.45
)
[transfer] => Array
(
[1] => 939.45
)
[renew] => Array
(
[1] => 939.45
)
)
[asia] => Array
(
[categories] => Array
(
[0] => gTLD
[1] => ccTLD
[2] => Geography
[3] => Popular
)
[addons] => Array
(
[dns] => 1
[email] => 1
[idprotect] => 1
)
[group] => sale
[register] => Array
(
[1] => 1527.88
)
[transfer] => Array
(
[1] => 1527.88
)
[renew] => Array
(
[1] => 1527.88
)
)
[co.uk] => Array
(
[categories] => Array
(
[0] => ccTLD
[1] => Geography
)
[addons] => Array
(
[dns] => 1
[email] => 1
[idprotect] => 1
)
[group] => sale
[register] => Array
(
[1] => 602.53
)
[transfer] => Array
(
[1] => 602.53
)
[renew] => Array
(
[1] => 602.53
)
)
[in.net] => Array
(
[categories] => Array
(
[0] => Other
)
[addons] => Array
(
[dns] => 1
[email] => 1
[idprotect] => 1
)
[group] => sale
[register] => Array
(
[1] => 100.00
)
[transfer] => Array
(
[1] => 100.00
)
[renew] => Array
(
[1] => 100.00
)
)
[uk] => Array
(
[categories] => Array
(
[0] => ccTLD
[1] => Geographic
[2] => Popular
)
[addons] => Array
(
[dns] => 1
[email] => 1
[idprotect] => 1
)
[group] => sale
[register] => Array
(
[1] => 200.00
)
[transfer] => Array
(
[1] => 300.00
)
[renew] => Array
(
[1] => 400.00
)
)
)
)
How to split the array and get the transfer values from the above JSON array?
If I don't misunderstood your question then this is what you need with simple foreach() loop to get tlds with their corresponding prices
$expected = [];
foreach($result['pricing'] as $tld=>$array){
// this is the register value, you can change it for transfer or renew if you wish
$expected[$tld] = $array['register'][1];
}
print_r($expected);
This is the array which i get from post
Array
(
[data] => Array
(
[Invoice] => Array
(
[itemNo] => Array
(
[0] => rtgrg
[1] => 4t4t
[2] => ththt
[3] => thth
)
[itemName] => Array
(
[0] => rtgrt
[1] => 4t5t5
[2] => hthtyh
[3] => gnghnn
)
[itemDiscription] => Array
(
[0] => 5tr5t
[1] => t45t4
[2] => tyhthtyh
[3] => gnghnh
)
[price] => Array
(
[0] => 2
[1] => 10
[2] => 9
[3] => 12
)
[itemQuantity] => Array
(
[0] => 2
[1] => 12
[2] => 9
[3] => 9
)
[itemDiscount] => Array
(
[0] => 11
[1] => 0.14
[2] => 0.13
[3] => 0.1
)
[itemTotal] => Array
(
[0] => 333333
[1] => 34535
[2] => 55555555555555
[3] => 666666666
)
[itemStartDate] => Array
(
[0] =>
[1] =>
[2] =>
[3] => 2016-06-17
)
[itemEndDate] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
)
[itemCustomcol] => Array
(
[0] => 4t4t4
[1] => t5trgr
[2] => htht
[3] => gngh
)
[itemCustomcol2] => Array
(
[0] => t4t4t
[1] => rtgtr
[2] => thth
[3] => gng
)
[itemttax2] => Array
(
[0] => tax1
)
[itemttax3] => Array
(
[0] => tax1
)
[itemttax4] => Array
(
[0] => tax1
)
[itemtCsTax] => Array
(
[0] => gngn
)
[itemtCsTaxPer] => Array
(
[0] => 0.1
)
[itemtDiscount] => Array
(
[0] => 0.18
)
[itemtInvReceived] => Array
(
[0] => gngn
)
[itemtInvToReturn] => Array
(
[0] => gngng
)
[itemInvDue] => Array
(
[0] => nghnghng
)
[itemInvComment] => Array
(
[0] => hngnhg
)
)
)
There is a separate table for dynamic input like itemName, itemDiscription to itemCustomcol2 and another table for itemttax2 to itemInvComment .
I tried foreach loop for multiple element array but this doent work for me as it returns a string. one of them is shown below
$capture_field_vals ="";
foreach($_POST["data"]["Invoice"]["itemNo"] as $key => $text_field){
$capture_field_vals .= $text_field .", ";
}
echo $capture_field_vals;
What i want is collect all element[0] and then insert it in a row mysql and then another element another row and so on.
Is there any reason you can't use the following format?
Array
(
[data] => Array
(
[Invoice] => Array
(
[0] => Array
(
[itemNo] => number
[itemName] => name
[itemDescription] => desc
[price] => 0.00
),
[1] => Array
(
[itemNo] => number
[itemName] => name
[itemDescription] => desc
[price] => 0.00
),...
with this method you can loop through retrieving all details of each item in the invoice?
If I have misunderstood your problem, I apologise
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'))){
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 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);