I'm trying to fetch the values within the fruit object and within the options object. You can see there are a list of items within it 100% Orange Juice, Apple, Pear, Pineapple and etc. How can loop through a very nested object in order to fetch those values.
I've tried using foreach to fetch those items but it seems I'm doing it incorrectly.
$fruitsArray = $product->get_attributes('');
foreach ( $fruitsValue as $fruitsArray ) {
echo $fruitsValue
}
<?php var_dump($product->get_attributes('')); ?>
array(2) {
["size"]=>
object(WC_Product_Attribute)#2724 (1) {
["data":protected]=>
array(6) {
["id"]=>
int(0)
["name"]=>
string(4) "Size"
["options"]=>
array(3) {
[0]=>
string(6) "400 ml"
[1]=>
string(2) "1L"
[2]=>
string(2) "2L"
}
["position"]=>
int(0)
["visible"]=>
bool(true)
["variation"]=>
bool(false)
}
}
["fruit"]=>
object(WC_Product_Attribute)#2723 (1) {
["data":protected]=>
array(6) {
["id"]=>
int(0)
["name"]=>
string(5) "Fruit"
["options"]=>
array(8) {
[0]=>
string(17) "100% Orange Juice"
[1]=>
string(5) "Apple"
[2]=>
string(4) "Pear"
[3]=>
string(9) "Pineapple"
[4]=>
string(12) "Passionfruit"
[5]=>
string(15) "Red Dragonfruit"
[6]=>
string(13) "Baobab Powder"
[7]=>
string(17) "Grapeseed Extract"
}
["position"]=>
int(1)
["visible"]=>
bool(true)
["variation"]=>
bool(false)
}
}
}
I have a shopping cart and I use two array: one for getting the cart products from session, and one to display the products. I need to get the count value from cart array, to corespondent product from products array. I need the count value in products->barrels array, becouse I will use a placeholder to show existing value.
The chemID and catID values makes the product unique.
I don't want to change the array structure, only to add the count value to products array....Please help
Cart array:
array(2) {
[0]=>
object(stdClass)#224 (9) {
["chemID"]=>
string(3) "657"
["product_number"]=>
string(8) "14004015"
["size"]=>
string(6) "15 GAL"
["catID"]=>
string(2) "24"
["list_price"]=>
string(6) "459.00"
["count"]=>
string(1) "2"
["attribute"]=>
string(6) "Yellow"
}
[1]=>
object(stdClass)#225 (9) {
["chemID"]=>
string(3) "658"
["product_number"]=>
string(9) "14004015C"
["size"]=>
string(6) "15 GAL"
["catID"]=>
string(2) "24"
["list_price"]=>
string(3) "434"
["count"]=>
string(1) "3"
}
}
Products array:
array(2) {
[657]=>
array(4) {
["attribute"]=>
string(6) "Yellow"
["barrels"]=>
array(3) {
[0]=>
object(stdClass)#293 (9) {
["product_number"]=>
string(8) "14004005"
["size"]=>
string(5) "5 Gal"
["catID"]=>
string(2) "13"
["list_price"]=>
string(6) "169.00"
["chemID"]=>
string(3) "657"
["attribute"]=>
string(6) "Yellow"
}
[1]=>
object(stdClass)#294 (9) {
["product_number"]=>
string(8) "14004015"
["size"]=>
string(6) "15 GAL"
["catID"]=>
string(2) "24"
["list_price"]=>
string(6) "459.00"
["chemID"]=>
string(3) "657"
["attribute"]=>
string(6) "Yellow"
}
[2]=>
object(stdClass)#295 (9) {
["product_number"]=>
string(8) "14004030"
["size"]=>
string(6) "30 Gal"
["catID"]=>
string(1) "2"
["list_price"]=>
string(6) "874.00"
["chemID"]=>
string(3) "657"
["attribute"]=>
string(6) "Yellow"
}
}
}
[658]=>
array(4) {
["attribute"]=>
string(5) "Clear"
["barrels"]=>
array(3) {
[0]=>
object(stdClass)#296 (9) {
["product_number"]=>
string(9) "14004005C"
["size"]=>
string(5) "5 Gal"
["catID"]=>
string(2) "13"
["list_price"]=>
string(6) "159.00"
["chemID"]=>
string(3) "658"
["attribute"]=>
string(5) "Clear"
}
[1]=>
object(stdClass)#297 (9) {
["product_number"]=>
string(9) "14004015C"
["size"]=>
string(6) "15 GAL"
["catID"]=>
string(2) "24"
["list_price"]=>
string(3) "434"
["chemID"]=>
string(3) "658"
["attribute"]=>
string(5) "Clear"
}
[2]=>
object(stdClass)#298 (9) {
["product_number"]=>
string(9) "14004030C"
["size"]=>
string(6) "30 Gal"
["catID"]=>
string(1) "2"
["list_price"]=>
string(6) "799.00"
["chemID"]=>
string(3) "658"
["attribute"]=>
string(5) "Clear"
}
}
}
}
To do this, you'll need to loop through both objects and count the matches.
$cnt = 0;
foreach($products as $pkey=>$pobj) {
foreach($carts as $ckey=>$cobj) {
if($cobj->chemID == $pobj->barrels->chemID && $cobj->catID == $pobj->barrels->catID) {
$cnt++;
}
}
}
echo 'Count of matches: ' . $cnt;
Here is a PHP Fiddle demo.
If you want the count value from cart_array written into the objetcs in the procucts array you could try this:
foreach($cart_array as $cart_value){
foreach($products_array[$cart_value->chemID]]["barrels"] as $products_value){
if($products_value->catID == $cart_value->catID){
$products_value->count=$cart_value->count;
}
}
}
#Set count=0 in all barrels in products_array
foreach($products_array as $value1){
foreach($value1["barrels"] as $value2){
if(!isset($value2->count)){
$value2->count = 0;
}
}
}
I do get the strings I intend to. But I also get the first letter (or number) of the other data in the json file.
E.g. I would like to have or should get the result:
/Barrafina
Exceptional cooking here. Such simple but amazing dishes. Highly recommend the crab croquettes, lambs kidneys, skewers and the brain. Sherries are also superb here.
Matt Ta-Min/
But instead I get
/Barrafina
ee
Exceptional cooking here. Such simple but amazing dishes. Highly recommend the crab croquettes, lambs kidneys, skewers and the brain. Sherries are also superb here.
5 5 E Eu uh h
Matt Ta-Min
/
What did I do wrong? here is the code:
(If you have time and energy to look the JSON you will see from where eg. 5 5 E Eu uh h comes.)
I appreciate any help.
<?php
$client_id = 'CLIENT_ID';
$client_secret = 'SECRET';
$request_date = date('Ymd');
$base_url = 'https://api.foursquare.com/v2/';
$endpoint = 'venues/explore?';
$params = 'near=London&limit=3§ion=food&radius=1000';
$auth = "&client_id=$client_id&client_secret=$client_secret&v=$request_date";
$url = $base_url.$endpoint.$params.$auth;
$results = file_get_contents($url);
$json_results = json_decode($results,true);
//var_dump($json_results);
$items0 = $json_results['response']['groups']['0']['items']['0'];
$items1 = $json_results['response']['groups']['0']['items']['0']['tips'];
$items2 = $json_results['response']['groups']['0']['items']['0']['tips']['0'];
//<div style="align:center;">
foreach ( $items0+$items1 as $item){
echo '<li>';
echo $item['name'];
echo $item['text'];
}
foreach ( $items2 as $item){
print $item['firstName']; print ' '; print $item['lastName'];
}
HERE is the json decoded:
array(2) { ["meta"]=> array(2) { ["code"]=> int(200) ["requestId"]=> string(24) "55fb1f91498e70ad2e246eed" } ["response"]=> array(10) { ["suggestedFilters"]=> array(2) { ["header"]=> string(12) "Tap to show:" ["filters"]=> array(2) { [0]=> array(2) { ["name"]=> string(13) "With specials" ["key"]=> string(8) "specials" } [1]=> array(2) { ["name"]=> string(8) "Open now" ["key"]=> string(7) "openNow" } } } ["geocode"]=> array(8) { ["what"]=> string(0) "" ["where"]=> string(6) "london" ["center"]=> array(2) { ["lat"]=> float(51.50853) ["lng"]=> float(-0.12574) } ["displayString"]=> string(38) "London, Greater London, United Kingdom" ["cc"]=> string(2) "GB" ["geometry"]=> array(1) { ["bounds"]=> array(2) { ["ne"]=> array(2) { ["lat"]=> float(51.691643999656) ["lng"]=> float(0.33418999705203) } ["sw"]=> array(2) { ["lat"]=> float(51.284674044171) ["lng"]=> float(-0.50855792793694) } } } ["slug"]=> string(6) "london" ["longId"]=> string(17) "72057594040571679" } ["warning"]=> array(1) { ["text"]=> string(114) "There aren't a lot of results near you. Try something more general, reset your filters, or expand the search area." } ["headerLocation"]=> string(6) "London" ["headerFullLocation"]=> string(6) "London" ["headerLocationGranularity"]=> string(4) "city" ["query"]=> string(4) "food" ["totalResults"]=> int(246) ["suggestedBounds"]=> array(2) { ["ne"]=> array(2) { ["lat"]=> float(51.510800358799) ["lng"]=> float(-0.12174369837641) } ["sw"]=> array(2) { ["lat"]=> float(51.508100699014) ["lng"]=> float(-0.13015278468604) } } ["groups"]=> array(1) { [0]=> array(3) { ["type"]=> string(18) "Recommended Places" ["name"]=> string(11) "recommended" ["items"]=> array(1) { [0]=> array(4) { ["reasons"]=> array(2) { ["count"]=> int(0) ["items"]=> array(1) { [0]=> array(3) { ["summary"]=> string(20) "This spot is popular" ["type"]=> string(7) "general" ["reasonName"]=> string(23) "globalInteractionReason" } } } ["venue"]=> array(17) { ["id"]=> string(24) "53bab96d498e7e355fb53d6c" ["name"]=> string(9) "Barrafina" ["contact"]=> array(6) { ["phone"]=> string(13) "+442074401456" ["formattedPhone"]=> string(16) "+44 20 7440 1456" ["twitter"]=> string(13) "barrafinaadst" ["facebook"]=> string(15) "705351912878392" ["facebookUsername"]=> string(23) "BarrafinaAdelaideStreet" ["facebookName"]=> string(25) "Barrafina Adelaide Street" } ["location"]=> array(10) { ["address"]=> string(14) "10 Adelaide St" ["crossStreet"]=> string(13) "William IV St" ["lat"]=> float(51.509450528906) ["lng"]=> float(-0.12594824153122) ["postalCode"]=> string(8) "WC2N 4HZ" ["cc"]=> string(2) "GB" ["city"]=> string(6) "London" ["state"]=> string(14) "Greater London" ["country"]=> string(14) "United Kingdom" ["formattedAddress"]=> array(5) { [0]=> string(30) "10 Adelaide St (William IV St)" [1]=> string(6) "London" [2]=> string(14) "Greater London" [3]=> string(8) "WC2N 4HZ" [4]=> string(14) "United Kingdom" } } ["categories"]=> array(1) { [0]=> array(6) { ["id"]=> string(24) "4bf58dd8d48988d150941735" ["name"]=> string(18) "Spanish Restaurant" ["pluralName"]=> string(19) "Spanish Restaurants" ["shortName"]=> string(7) "Spanish" ["icon"]=> array(2) { ["prefix"]=> string(52) "https://ss3.4sqi.net/img/categories_v2/food/spanish_" ["suffix"]=> string(4) ".png" } ["primary"]=> bool(true) } } ["verified"]=> bool(false) ["stats"]=> array(3) { ["checkinsCount"]=> int(357) ["usersCount"]=> int(278) ["tipCount"]=> int(26) } ["url"]=> string(22) "http://barrafina.co.uk" ["price"]=> array(3) { ["tier"]=> int(2) ["message"]=> string(8) "Moderate" ["currency"]=> string(2) "£" } ["rating"]=> float(9) ["ratingColor"]=> string(6) "00B551" ["ratingSignals"]=> int(80) ["allowMenuUrlEdit"]=> bool(true) ["hours"]=> array(2) { ["status"]=> string(19) "Open until 11:00 PM" ["isOpen"]=> bool(true) } ["specials"]=> array(2) { ["count"]=> int(0) ["items"]=> array(0) { } } ["photos"]=> array(2) { ["count"]=> int(31) ["groups"]=> array(0) { } } ["hereNow"]=> array(3) { ["count"]=> int(0) ["summary"]=> string(11) "Nobody here" ["groups"]=> array(0) { } } } ["tips"]=> array(1) { [0]=> array(9) { ["id"]=> string(24) "55db9142498ede18f5b31b81" ["createdAt"]=> int(1440452930) ["text"]=> string(164) "Exceptional cooking here. Such simple but amazing dishes. Highly recommend the crab croquettes, lambs kidneys, skewers and the brain. Sherries are also superb here." ["type"]=> string(4) "user" ["canonicalUrl"]=> string(52) "https://foursquare.com/item/55db9142498ede18f5b31b81" ["likes"]=> array(3) { ["count"]=> int(2) ["groups"]=> array(0) { } ["summary"]=> string(7) "2 likes" } ["logView"]=> bool(true) ["todo"]=> array(1) { ["count"]=> int(0) } ["user"]=> array(5) { ["id"]=> string(8) "98683884" ["firstName"]=> string(4) "Matt" ["lastName"]=> string(6) "Ta-Min" ["gender"]=> string(4) "male" ["photo"]=> array(2) { ["prefix"]=> string(31) "https://irs1.4sqi.net/img/user/" ["suffix"]=> string(30) "/98683884-1MJ0OTISTPZWSBRJ.jpg" } } } } ["referralId"]=> string(30) "e-3-53bab96d498e7e355fb53d6c-0" } } } } } }
Here is JSON
{"meta":{"code":200,"requestId":"55fbde58498e7fdbd12f8fb2"},"response":{"suggestedFilters":{"header":"Tap to show:","filters":[{"name":"With specials","key":"specials"},{"name":"Open now","key":"openNow"}]},"geocode":{"what":"","where":"london","center":{"lat":51.50853,"lng":-0.12574},"displayString":"London, Greater London, United Kingdom","cc":"GB","geometry":{"bounds":{"ne":{"lat":51.691643999655895,"lng":0.33418999705203406},"sw":{"lat":51.28467404417054,"lng":-0.5085579279369435}}},"slug":"london","longId":"72057594040571679"},"warning":{"text":"There aren't a lot of results near you. Try something more general, reset your filters, or expand the search area."},"headerLocation":"London","headerFullLocation":"London","headerLocationGranularity":"city","query":"food","totalResults":246,"suggestedBounds":{"ne":{"lat":51.5108003587992,"lng":-0.12174369837640672},"sw":{"lat":51.50810069901375,"lng":-0.1301527846860385}},"groups":[{"type":"Recommended Places","name":"recommended","items":[{"reasons":{"count":0,"items":[{"summary":"This spot is popular","type":"general","reasonName":"globalInteractionReason"}]},"venue":{"id":"53bab96d498e7e355fb53d6c","name":"Barrafina","contact":{"phone":"+442074401456","formattedPhone":"+44 20 7440 1456","twitter":"barrafinaadst","facebook":"705351912878392","facebookUsername":"BarrafinaAdelaideStreet","facebookName":"Barrafina Adelaide Street"},"location":{"address":"10 Adelaide St","crossStreet":"William IV St","lat":51.50945052890648,"lng":-0.1259482415312226,"postalCode":"WC2N 4HZ","cc":"GB","city":"London","state":"Greater London","country":"United Kingdom","formattedAddress":["10 Adelaide St (William IV St)","London","Greater London","WC2N 4HZ","United Kingdom"]},"categories":[{"id":"4bf58dd8d48988d150941735","name":"Spanish Restaurant","pluralName":"Spanish Restaurants","shortName":"Spanish","icon":{"prefix":"https:\/\/ss3.4sqi.net\/img\/categories_v2\/food\/spanish_","suffix":".png"},"primary":true}],"verified":false,"stats":{"checkinsCount":357,"usersCount":278,"tipCount":26},"url":"http:\/\/barrafina.co.uk","rating":9.0,"ratingColor":"00B551","ratingSignals":80,"allowMenuUrlEdit":true,"hours":{"status":"Closed until Noon","isOpen":false},"specials":{"count":0,"items":[]},"photos":{"count":31,"groups":[]},"hereNow":{"count":0,"summary":"Nobody here","groups":[]}},"tips":[{"id":"55db9142498ede18f5b31b81","createdAt":1440452930,"text":"Exceptional cooking here. Such simple but amazing dishes. Highly recommend the crab croquettes, lambs kidneys, skewers and the brain. Sherries are also superb here.","type":"user","canonicalUrl":"https:\/\/foursquare.com\/item\/55db9142498ede18f5b31b81","likes":{"count":2,"groups":[],"summary":"2 likes"},"logView":true,"todo":{"count":0},"user":{"id":"98683884","firstName":"Matt","lastName":"Ta-Min","gender":"male","photo":{"prefix":"https:\/\/irs1.4sqi.net\/img\/user\/","suffix":"\/98683884-1MJ0OTISTPZWSBRJ.jpg"}}}],"referralId":"e-3-53bab96d498e7e355fb53d6c-0"}]}]}}
I want to get data from an array using PHP loop like FOR or FOREACH.
when I print the content of the array using var_dump it looks like this:
array(2) {
[0]=>
array(2) {
["name"]=>
string(6) "query1"
["fql_result_set"]=>
array(13) {
[0]=>
array(3) {
["aid"]=>
string(19) "2944783819003364347"
["name"]=>
string(16) "Profile Pictures"
["photo_count"]=>
string(2) "37"
}
[1]=>
array(3) {
["aid"]=>
string(19) "2944783820076875780"
["name"]=>
string(7) "only me"
["photo_count"]=>
string(1) "2"
}
[2]=>
array(3) {
["aid"]=>
string(19) "2944783819003517141"
["name"]=>
string(35) "Ways To Tie Your Shoelaces. Nice :)"
["photo_count"]=>
string(1) "8"
}
[3]=>
array(3) {
["aid"]=>
string(19) "2944783819003490957"
["name"]=>
string(12) "Cover Photos"
["photo_count"]=>
string(2) "12"
}
[4]=>
array(3) {
["aid"]=>
string(19) "2944783819003481818"
["name"]=>
string(14) "Mobile Uploads"
["photo_count"]=>
string(2) "55"
}
}
}
what should I do to get: ["aid"] , ["name" ] and ["photo_count"]?
like this
foreach ($array[0]['fql_result_set'] as $record) {
echo "{$record['aid']}, {$record['name']}";
}
I want to know how can I limit the values of array to be displayed. Assuming my $color is equals to Indigo
I tried implementing this code by it doesn't work
foreach($data as $item){
if($item['color'] == $color){
echo $item['size'].'-'.$item['color'].'-'.$item['price'].'<br>';
}
}
And if I remove the if statement the var_dump($data)
array(16) {
[0]=>
array(3) {
["size"]=>
string(1) "2"
["price"]=>
string(6) "$59.00"
["color"]=>
string(11) "Black Rinse"
}
[1]=>
array(3) {
["size"]=>
string(1) "2"
["price"]=>
string(6) "$59.00"
["color"]=>
string(6) "Indigo"
}
[2]=>
array(3) {
["size"]=>
string(1) "4"
["price"]=>
string(6) "$59.00"
["color"]=>
string(11) "Black Rinse"
}
[3]=>
array(3) {
["size"]=>
string(1) "4"
["price"]=>
string(6) "$59.00"
["color"]=>
string(6) "Indigo"
}
[4]=>
array(3) {
["size"]=>
string(1) "6"
["price"]=>
string(6) "$59.00"
["color"]=>
string(11) "Black Rinse"
}
[5]=>
array(3) {
["size"]=>
string(1) "6"
["price"]=>
string(6) "$59.00"
["color"]=>
string(6) "Indigo"
}
[6]=>
array(3) {
["size"]=>
string(1) "8"
["price"]=>
string(6) "$59.00"
["color"]=>
string(11) "Black Rinse"
}
[7]=>
array(3) {
["size"]=>
string(1) "8"
["price"]=>
string(6) "$59.00"
["color"]=>
string(6) "Indigo"
}
[8]=>
array(3) {
["size"]=>
string(2) "10"
["price"]=>
string(6) "$59.00"
["color"]=>
string(11) "Black Rinse"
}
[9]=>
array(3) {
["size"]=>
string(2) "10"
["price"]=>
string(6) "$59.00"
["color"]=>
string(6) "Indigo"
}
[10]=>
array(3) {
["size"]=>
string(2) "12"
["price"]=>
string(6) "$59.00"
["color"]=>
string(11) "Black Rinse"
}
[11]=>
array(3) {
["size"]=>
string(2) "12"
["price"]=>
string(6) "$59.00"
["color"]=>
string(6) "Indigo"
}
[12]=>
array(3) {
["size"]=>
string(2) "14"
["price"]=>
string(6) "$59.00"
["color"]=>
string(11) "Black Rinse"
}
[13]=>
array(3) {
["size"]=>
string(2) "14"
["price"]=>
string(6) "$59.00"
["color"]=>
string(6) "Indigo"
}
[14]=>
array(3) {
["size"]=>
string(2) "16"
["price"]=>
string(6) "$59.00"
["color"]=>
string(11) "Black Rinse"
}
[15]=>
array(3) {
["size"]=>
string(2) "16"
["price"]=>
string(6) "$59.00"
["color"]=>
string(6) "Indigo"
}
}
I want only to display values which is the color is equal to $color. How should I do this the correct way?
You might be dealing with case sensitivity or white space .. you can try :
$data = array(
0 => array("size" => "2","price" => "$59.00","color" => "Black Rinse"),
1 => array("size" => "2","price" => "$59.00","color" => "Indigo"));
$color = "Indigo";
foreach ( $data as $item ) {
if (strtolower($item['color']) == strtolower($color)) {
echo $item['size'] . '-' . $item['color'] . '-' . $item['price'] . '<br>';
}
}
Output
2-Indigo-$59.00
See Live Demo
This should fix your issue (assuming you're comparing two strings, I suggest you use strcasecmp- for case insensitive string comparison in your if loop)
foreach($data as $item){
if(strcasecmp(trim($item['color']),trim($color))==0){
echo $item['size'].'-'.$item['color'].'-'.$item['price'].'<br>';
}
}
Be elegant, be simple:
$items = array(
array('size'=>'4', 'color'=>'Indigo', 'price'=>'$59.00'),
array('size'=>'5', 'color'=>'black', 'price'=>'$59.00'),
array('size'=>'5', 'color'=>'green', 'price'=>'$54.00'),
array('size'=>'7', 'color'=>'Indigo', 'price'=>'$50.00'),
array('size'=>'5', 'color'=>'purple', 'price'=>'$51.00'));
print_r(array_filter($items, function($i){
return in_array('Indigo', $i);
})
);
or PHP < 5.3:
print_r(array_filter($items,
create_function('$i', 'return in_array("Indigo", $i);'))
);
Take advantage of features that PHP offers.