Hii I need help in grouping array where i have this sort of array :
Array
(
[0] => Array
(
[name] => rose
[price] => 1
)
[1] => Array
(
[name] => daisy
[price] => 3
)
[2] => Array
(
[name] => orchid
[price] => 1
)
[3] => Array
(
[name] => rose
[price] => 2
)
[4] => Array
(
[name] => daisy
[price] => 3
)
[5] => Array
(
[name] => orchid
[price] => 1
)
[6] => Array
(
[name] => rose
[price] => 2
)
[7] => Array
(
[name] => daisy
[price] => 3
)
[8] => Array
(
[name] => orchid
[price] => 2
)
)
and i want it to be like :
Array
(
[0] => Array
(
[0] => Array
(
[name] => rose
[price] => 1
)
[1] => Array
(
[name] => daisy
[price] => 1
)
[2] => Array
(
[name] => orchid
[price] => 1
)
)
[1] => Array
(
[0] => Array
(
[name] => rose
[price] => 2
)
[1] => Array
(
[name] => daisy
[price] => 2
)
[2] => Array
(
[name] => orchid
[price] => 2
)
)
[2] => Array
(
[0] => Array
(
[name] => rose
[price] => 3
)
[1] => Array
(
[name] => daisy
[price] => 3
)
[2] => Array
(
[name] => orchid
[price] => 3
)
)
)
I mean want to group them where same "price" value occurs . You can better understand them from given arrays .
$group = array();
foreach ( $array as $value ) {
$group[$value['price']][] = $value;
}
var_dump($group);
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);
Update: I have a solution - please see below for details.
I have an array where the keys are levels (in a navigation tree for example) - something like
Array
(
[0] => Array
(
[100] => Array
(
[name] => foo100
[slug] => foo100
[id] => 100
[parent] => 0
[level] => 0
)
[101] => Array
(
[name] => foo101
[slug] => foo101
[id] => 101
[parent] => 0
[level] => 0
)
)
[1] => Array
(
[200] => Array
(
[name] => foo200
[slug] => foo200
[id] => 200
[parent] => 100
[level] => 1
)
[201] => Array
(
[name] => foo201
[slug] => foo201
[id] => 201
[parent] => 101
[level] => 1
)
)
[2] => Array
(
[300] => Array
(
[name] => foo300
[slug] => foo300
[id] => 300
[parent] => 200
[level] => 2
)
[301] => Array
(
[name] => foo301
[slug] => foo301
[id] => 301
[parent] => 201
[level] => 2
)
)
[3] => Array
(
[400] => Array
(
[name] => foo400
[slug] => foo400
[id] => 400
[parent] => 300
[level] => 3
)
)
[4] => Array
(
[500] => Array
(
[name] => foo500
[slug] => foo500
[id] => 500
[parent] => 400
[level] => 4
)
)
)
I need to create an array from this which iterates from the top most level and creates an array with the key being the slug of that level - to produce the following:
Array
(
[foo500] => Array
(
[4] => Array
(
[name] => foo500
)
[3] => Array
(
[name] => foo400
)
[2] => Array
(
[name] => foo300
)
[1] => Array
(
[name] => foo200
)
[0] => Array
(
[name] => foo100
)
)
[foo400] => Array
(
[3] => Array
(
[name] => foo400
)
[2] => Array
(
[name] => foo300
)
[1] => Array
(
[name] => foo200
)
[0] => Array
(
[name] => foo100
)
)
[foo300] => Array
(
[2] => Array
(
[name] => foo300
)
[1] => Array
(
[name] => foo200
)
[0] => Array
(
[name] => foo100
)
)
[foo301] => Array
(
[2] => Array
(
[name] => foo301
)
[1] => Array
(
[name] => foo201
)
[0] => Array
(
[name] => foo101
)
)
[foo200] => Array
(
[1] => Array
(
[name] => foo200
)
[0] => Array
(
[name] => foo100
)
)
[foo201] => Array
(
[1] => Array
(
[name] => foo201
)
[0] => Array
(
[name] => foo101
)
)
[foo100] => Array
(
[0] => Array
(
[name] => foo100
)
)
[foo101] => Array
(
[0] => Array
(
[name] => foo101
)
)
)
I hope this explains the issue - struggling to get this right! Any help much appreciated!
Update - solution.
For this I removed the first level of keys to leave
Array
(
[107] => Array
(
[id] => 107
[name] => About Us
[indexID] => about
[level] => 0
[parent] => 0
)
[109] => Array
(
[id] => 109
[name] => Home
[indexID] => index
[level] => 0
[parent] => 0
)
}
etc etc
Assuming $data is the above array I went with:
foreach ($data as $k => $v) {
if ($v['parent'] == 0) {
$bc[$v['indexID']][0]['name'] = $v['name'];
$bc[$v['indexID']][0]['indexID'] = $v['indexID'];
}
else {
$nextParent = $v['parent'];
$currentIndexID = $v['indexID'];
$currentName = $v['name'];
$bc[$v['indexID']][0]['name'] = $currentName;
$bc[$v['indexID']][0]['indexID'] = $currentIndexID;
for($i=1;$i<=$level;$i++) {
foreach ($data as $a => $b) {
if ($a == $nextParent) {
$nextParent = $b['parent'];
$bc[$v['indexID']][$i]['name'] = $b['name'];
$bc[$v['indexID']][$i]['indexID'] = $b['indexID'];
}
}
}
}
}
I get this array in response from webservice, so How do I read it in a foreach cycle? or some easy way to read it. The [group] they are more than 12 [id]
there is.
Array
( [response] => Array (
[single] => Array ( [parameters] => Array ( [0] => Array ( [name] => msgCode [value] => 0101 )
[1] => Array ( [name] => msgDesc [value] => OK )
[2] => Array ( [name] => status [value] => 1)
[3] => Array ( [name] => message [value] => Normal) ) )
[group] => Array ( [id] => N4BD767 [parameters] => Array ( [0] => Array ( [name] => idFee [value] => 000 ) echo
[1] => Array ( [name] => typeFee [value] => Cuota)
[2] => Array ( [name] => entryDate [value] => 2014-12-17T14:06:47-03:00 )
[3] => Array ( [name] => expirationDate [value] => 2015-12-05T00:00:00-03:00)
[4] => Array ( [name] => amountOrigin [value] => 221980)
[5] => Array ( [name] => surcharges [value] => 1856)
[6] => Array ( [name] => entity [value] => ONLINE)
[7] => Array ( [name] => feeStatus [value] => inicial )
[8] => Array ( [name] => tranNumber [value] => 27) ) ) ) )
Okay guys, this is what I did and it work fine. With no foreach.
$output = array();
array_walk_recursive($result, function($item,$key) use (&$output){
array_push($output,$key,$item);
});
echo var_dump($output);
Hi guys i have this array when i do print_r($p)
Array
(
[0] => Array
(
[product] => Array
(
[title] => test
[id] => 9
[created_at] => 2015-08-11 19:32:05
[isNew] =>
[type] => simple
[status] => publish
[price] => 10.00
[regular_price] => 10.00
[sale_price] => 6.00
[stock_quantity] => 19999985
[featured] => 1
[on_sale] =>
[description] =>
[short_description] =>
[categories] => Array
(
)
[tags] => Array
(
)
[images] => Array
(
[0] => Array
(
[src] =>
)
)
[featured_src] =>
[attributes] => Array
(
)
[variations] =>
)
)
[1] => Array
(
[product] => Array
(
[title] => test222222
[id] => 97
[created_at] => 2015-08-31 17:40:54
[isNew] =>
[type] => variation
[status] => publish
[price] => 1
[regular_price] => 2
[sale_price] => 1
[stock_quantity] => 1999974
[featured] => 1
[on_sale] => 1
[description] => <p>tasdasd</p>
[short_description] =>
[categories] => Array
(
)
[tags] => Array
(
)
[images] => Array
(
[0] => Array
(
[src] =>
)
)
[featured_src] =>
[attributes] => Array
(
[0] => Array
(
[name] => Color
[slug] => Color
[position] => 0
[visible] => 1
[variation] => 1
[options] => Array
(
[0] => black
[1] => White
)
)
)
[variations] => Array
(
[0] => Array
(
[id] => 98
[price] => 1
[regular_price] => 2
[stock] => 199969
[color] => black
)
[1] => Array
(
[id] => 97
[price] => 1
[regular_price] => 2
[stock] => 1999974
[color] => White
)
)
)
)
[2] => Array
(
[product] => Array
(
[title] => test222222
[id] => 98
[created_at] => 2015-08-31 17:40:54
[isNew] =>
[type] => variation
[status] => publish
[price] => 1
[regular_price] => 2
[sale_price] => 1
[stock_quantity] => 199969
[featured] => 1
[on_sale] => 1
[description] => <p>tasdasd</p>
[short_description] =>
[categories] => Array
(
)
[tags] => Array
(
)
[images] => Array
(
[0] => Array
(
[src] =>
)
)
[featured_src] =>
[attributes] => Array
(
[0] => Array
(
[name] => Color
[slug] => Color
[position] => 0
[visible] => 1
[variation] => 1
[options] => Array
(
[0] => black
[1] => White
)
)
)
[variations] => Array
(
[0] => Array
(
[id] => 98
[price] => 1
[regular_price] => 2
[stock] => 199969
[color] => black
)
[1] => Array
(
[id] => 97
[price] => 1
[regular_price] => 2
[stock] => 1999974
[color] => White
)
)
)
)
[3] => Array
(
[product] => Array
(
[title] => test222222
[id] => 76
[created_at] => 2015-08-31 17:40:54
[isNew] =>
[type] => variable
[status] => publish
[price] => 0.00
[regular_price] => 0.00
[sale_price] => 0.00
[stock_quantity] => 50000
[featured] => 1
[on_sale] => 1
[description] => <p>tasdasd</p>
[short_description] =>
[categories] => Array
(
)
[tags] => Array
(
)
[images] => Array
(
[0] => Array
(
[src] => https://localhost/Leminiscate/wp-content/uploads/2015/08/lemniscate_by_eon_brush-d7y8np7-e1441070793605.jpg
)
)
[featured_src] => https://localhost/Leminiscate/wp-content/uploads/2015/08/lemniscate_by_eon_brush-d7y8np7-e1441070793605.jpg
[attributes] => Array
(
[0] => Array
(
[name] => Color
[slug] => Color
[position] => 0
[visible] => 1
[variation] => 1
[options] => Array
(
[0] => black
[1] => White
)
)
)
[variations] => Array
(
[0] => Array
(
[id] => 98
[price] => 1
[regular_price] => 2
[stock] => 199969
[color] => black
)
[1] => Array
(
[id] => 97
[price] => 1
[regular_price] => 2
[stock] => 1999974
[color] => White
)
)
)
)
)
null
i get this with this function
public function test(){
global $wpdb;
global $Pproduct;
global $woocommerce;
$productIds = 9_97_98_76;
$pId = explode("_", $productIds);
foreach($pId as $productID){
$product[] = $Pproduct->get_product($productID, $fields);
$p = $product;
}
print_r($p);
how do i do a foreach loop again to get variation attributes? in given product id 9_97_98_76, product id 97 & 98 are variation products of 76.
I want to get the title of the product and variable attributes, how do i code foreach so that the result returns as the following array : test_test222222 white_test222222 black_test222222 ???
try this.. Hope you are getting the product object for the productID using the get_product() function. Then try array_push to insert all filtered product objects in 1 array.
$product = Array();
foreach($pId as $productID){
array_push($product, $Pproduct->get_product($productID, $fields));
}
now try the following
foreach($product as $temp) {
echo $temp[variations];
}
Try this will get your title
$pId = explode("_", $products);
foreach($pId as $id){
$product = $Pproduct->get_product($id, $fields);
$title = $product['product']['title'];
echo $title."</br>";
}
I want a recursive function to group all child categories under root category. I've an array like this;
Array
(
[0] => Array
(
[uid] => 1
[title] => Car
[Child] => Array
(
[0] => Array
(
[uid] => 3
[title] => Color
[Child] => Array
(
[0] => Array
(
[uid] => 5
[title] => Red
)
[1] => Array
(
[uid] => 6
[title] => Blue
)
)
)
[1] => Array
(
[uid] => 4
[title] => Door
)
)
)
[1] => Array
(
[uid] => 2
[title] => Two Wheeler
[Child] => Array
(
[0] => Array
(
[uid] => 7
[title] => Type
[Child] => Array
(
[0] => Array
(
[uid] => 9
[title] => Scooter
)
[1] => Array
(
[uid] => 10
[title] => Bike
)
)
)
[1] => Array
(
[uid] => 8
[title] => Company
)
)
)
)
My requirement is to bring all the subchild values to the main category child value. I mean I need the following structure;
Array
(
[0] => Array
(
[uid] => 1
[title] => Car
[Child] => Array
(
[0] => Array
(
[uid] => 3
[title] => Color
)
[1] => Array
(
[uid] => 4
[title] => Door
)
[2] => Array
(
[uid] => 5
[title] => Red
)
[3] => Array
(
[uid] => 6
[title] => Blue
)
)
)
[1] => Array
(
[uid] => 2
[title] => Two Wheeler
[Child] => Array
(
[0] => Array
(
[uid] => 7
[title] => Type
)
[1] => Array
(
[uid] => 8
[title] => Company
)
[2] => Array
(
[uid] => 9
[title] => Scooter
)
[3] => Array
(
[uid] => 10
[title] => Bike
)
)
)
)
)
How can I achieve this using a PHP function?