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);
Related
Array
(
[3M] => Array
(
[0] => Array
(
[name] => 3M
[price] => 158.15
)
[440] => Array
(
[name] => 3M
[price] => 156.69
)
)
[AO Smith] => Array
(
[1] => Array
(
[name] => AO Smith
[price] => 47.29
)
[441] => Array
(
[name] => AO Smith
[price] => 47.19
)
)
So I have an Array that is above^^^. I would like to get it into a condensed array format. I need a function that loops through the above and outputs it in the format below.
Array
(
[3M] => Array
(
[price1] => 158.15
[price2] => 156.69
)
[AO Smith] => Array
(
[price1] => 47.29
[price2] => 47.19
)
)
Above is how I would like the data oriented.
Thanks for the help.
What you'll find is the format you want is not good and not as usable or flexible. This however will give you a better format. name and price are descriptive, price1 and price2 are no different than 0 and 1:
foreach($array as $key => $values) {
$result[$key] = array_column($values, 'price');
}
Yields:
Array
(
[3M] => Array
(
[0] => 158.15
[1] => 156.69
)
[AO Smith] => Array
(
[0] => 47.29
[1] => 47.19
)
)
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;
}
I have the following array stored in $data
Array
(
[name] => JimBob
[data] => Array
(
[0] => Array
(
[date] => 2013-15-5
[pole] => 2
[race] => 43
)
[1] => Array
(
[date] => 2013-15-6
[pole] => 15
[race] => 34
)
[2] => Array
(
[date] => 2013-15-7
[pole] => 9
[race] => 54
)
)
)
I need to create a new array which looks like this
Array
(
[name] => JimBob
[data] => Array
(
[0] => 2013-15-5,2
[1] => 2013-15-6,15
[2] => 2013-15-7,9
)
)
So far I have this array_map function
$arr2['data'] = array_map(function($date) {
return $date['date'];
}, $data['data']);
print_r($arr2);
Which outputs the below, how can I get the pole values added to these dates separated by comma?
Array
(
[data] => Array
(
[0] => 2013-15-5
[1] => 2013-15-6
[2] => 2013-15-7
)
)
Use concatenation.
$arr2['data'] = array_map(function($date) {
return $date['date'] . ',' . $date['pole'];
}, $data['data']);
Here's the array structure (only the first element in the array):
Array
(
[1] => Array
(
[pageid] => 1
[step_order] => 1
[pageurl] => http://www.domain.com/
[in_links] => Array
(
[domains] => Array
(
[Direct Entry] => 1520
[www.google.com] => 387
[www.google.co.in] => 14
[search.yahoo.com] => 10
[All other] => 27
)
[impressions] => Array
(
[Direct Entry] => Array
(
[0] => 10654
[1] => 10728
[2] => 10772
)
[www.google.com] => Array
(
[0] => 10991
[1] => 12455
[2] => 12466
[3] => 10757
)
[www.google.co.in] => Array
(
[0] => 9839
[1] => 9837
[2] => 9845
)
[search.yahoo.com] => Array
(
[0] => 12087
[1] => 10864
)
)
)
[out_links] => Array
(
[domain] => Array
(
[Left site] => 1752
[http://www.domain.com/#] => 102
[http://www.domain.com/contact] => 102
[http://www.domain.com/#basic_inline_div] => 2
)
[impressions] => Array
(
[Left site] => Array
(
[0] => 7680
[1] => 9728
[2] => 10496
)
[http://www.domain.com/#] => Array
(
[0] => 259
[1] => 11013
)
[http://www.domain.com/contact] => Array
(
[0] => 12802
[1] => 10757
)
[http://www.domain.com/#basic_inline_div] => Array
(
[0] => 11
[1] => 51
)
)
)
[visitors] => 1958
)
)
I'm trying to loop to get the elements from domains, impressions (and sub elements). I managed to get the first parts: pageid, step_order, page_url. I'm having trouble with in_links and out_links and their child arrays. Anyone have ideas on how to pull that data?
Here's how you get the domains from in_links. The others are similar (but I'm not sure what the indexes in the impressions sub-array represent).
foreach ($array as $element) {
foreach ($element['in_links']['domains'] as $domain => $count) {
echo "Domain: $domain, Count: $count\n";
}
}
I have two arrays that have common indexes (church and office). I need to "merge" the total of the first array into the second array to get the desired output (seen below the double line). I'm not sure how to do this with array_merge(). Any help would be greatly appreciated!
Array
(
[church] => Array
(
[total] => 77
)
[office] => Array
(
[total] => 202
)
)
Array
(
[church] => Array
(
[name] => Array
(
[0] => Bill
[1] => Sally
)
[addr] => Array
(
[0] => Address Same as Billing
[1] => Address Same as Billing
)
[message] => Array
(
[0] =>
[1] =>
)
[amount] => Array
(
[0] => 25
[1] => 50
)
)
[office] => Array
(
[name] => Array
(
[0] => Marta
[1] => Ruth
)
[addr] => Array
(
[0] => Address Same as Billing
[1] => Address Same as Billing
)
[message] => Array
(
[0] =>
[1] =>
)
[amount] => Array
(
[0] => 100
[1] => 100
)
)
)
====================================================
Array
(
[church] => Array
(
[total] => 77
[name] => Array
(
[0] => Bill
[1] => Sally
)
[addr] => Array
(
[0] => Address Same as Billing
[1] => Address Same as Billing
)
[message] => Array
(
[0] =>
[1] =>
)
[amount] => Array
(
[0] => 25
[1] => 50
)
)
[office] => Array
(
[total] => 202
[name] => Array
(
[0] => Marta
[1] => Ruth
)
[addr] => Array
(
[0] => Address Same as Billing
[1] => Address Same as Billing
)
[message] => Array
(
[0] =>
[1] =>
)
[amount] => Array
(
[0] => 100
[1] => 100
)
)
)
Try something like this:
$a = array('church' => array('total' => 5), 'office' => array('total' => 10));
$b = array('church' => array('name' => 'church'), 'office' => array('name' => 'office'));
foreach ( $b as $key => $value ) {
$b[$key] = array_merge($a[$key], $b[$key]);
}