looping through an object twice - php - php

I'm playing around with a zencart trying to make it do what I want but I've run out of ideas of what to Google.
When I query the DB using the zencart function the software returns an object which looks like:
queryFactoryResult Object
(
[is_cached] =>
[resource] => Resource id #117
[cursor] => 11
[EOF] =>
[fields] => Array
(
[products_id] => 5582
[products_description] => description here
[products_name] => Lucky magnet – Each petal...
[products_type] => 1
[products_quantity] => 0
[products_image] => EachPetalMag.jpg
[products_price] => 3.4000
[products_status] => 1
[products_ordered] => 14
[master_categories_id] => 21
[supplier_id] => 7
)
)
I have to loop through once to count how many master_categories are there before I can do anything else:
while (!$products->EOF) {
$products_count++;
$supcats[$products->fields['master_categories_id']] = $products->fields['master_categories_id'];
$products->MoveNext();
}
I then need to loop through the object again using the while loop like above, I've tried:
reset($products);
and
$products->EOF = FALSE;
but they don't work. Is there a way to do this with out having to send the query again?

Regular array-like operations on Zen Cart queryFactoryResult won't work as it's not an array, so this won't work:
reset($products);
To loop through the variable again use queryFactoryResult->Move(row_number) method:
$products->Move(0);

Related

Decoding JSON in PHP can't access the first key

I have a PHP script which successfully decodes a JSON string into a PHP object using:
$amount_detail = json_decode($tuitionfee->amount_detail);
when I print it out, this is what I get
stdClass Object
(
[1] => stdClass Object
(
[amount] => 0
[date] => 2023-01-08
[amount_discount] => 55200
[amount_fine] => 0
[description] =>
[collected_by] => Super Admin(356)
[payment_mode] => Cash
[received_by] => 1
[inv_no] => 1
)
[2] => stdClass Object
(
[amount] => 36800
[date] => 2023-01-08
[description] => Collected By: Super Admin
[amount_discount] => 0
[amount_fine] => 0
[payment_mode] => Cash
[received_by] => 1
[inv_no] => 2
)
)
In trying to get the first object [amount_discount], I went further to do this:
if (is_object($amount_detail)) {
foreach ($amount_detail as $amount_detail_key => $amount_detail_value) {
$discount = $amount_detail_value->amount_discount;
}
}
But this is collecting data from the second key [amount_discount].
So instead of getting 55200, I'm getting 0.
How do I get to access data from the first key too?
The $discount variable gets over-written each time loop is executed. So you will always get the last data.
So if you want only the first index value then use current()
$discount = current((Array)$amount_detail_value)->amount_discount;
Output: https://3v4l.org/8Wvqv
Note: In case you want all discount output then in your loop echo the $discount variable.

PHP search/filter array with given conditions, recursively

I am getting data from an api (that I cannot query agains, just get lump of data), and then I need to query against those data like I would do using database. Only It would be great if I could do it recursively.
Data example
[0] => Array
(
[id] => 1
[url] => https://domain.com/api/1.0/item/1/
[name] => some_item
[category] => some category
[created_by] => Array
(
[id] => 1
[screen_name] => tomino
)
[current_user_domain_access] => Array
(
[is_active] => 1
[is_administrator] => 1
)
[alerts_enabled] => 0
)
(much shortened version)
I receive an array of objects like that and then I need to select/filter/search by values.
Something like this
SomeModel::find(['category'=>'some category','current_user_domain_access' => ['is_administrator' => 1]]);
Is that something that would be possible in PHP? I was thinking about flattening the array, but then there might be key conflicts
1) select data : You can select data by (array_name->id),(array_name->url) and so on...
2)Filter : add conditions according to requirement
3)search : in_array(),array_search

How to access array value in smarty when key starts with a period?

I am using a CMS that uses Smarty, I am not that familiar with Smarty. I can access array values like this: {{$data.video.title}}
I need to access video information but came across this issue:
[formats] => Array
(
[.mp4] => Array
(
[postfix] => .mp4
[dimensions] => Array
(
[0] => 1280
[1] => 720
)
[duration] => 330
[duration_string] => 5:30
[duration_array] => Array
(
[minutes] => 5
[seconds] => 30
)
[file_size] => 51928676
[file_size_string] => 49.52 Mb
[timeline_screen_amount] => 0
[timeline_screen_interval] => 0
[file_name] => 5.mp4
[file_path] => 8ad883ae4989f1aaf1da077bf56d9495/0/5/5.mp4
[timeline_directory] =>
)
)
I would like to know how I can access the [.mp4] values, since it begins with a period it causes problems. I have tried many variations such as:
{{$data.formats..mp3.file_size_string}}
{{$data.formats[.mp3].file_size_string}}
{{$data.[formats][.mp3][file_size_string]}}
etc...
Any help would be greatly appreciated!
I asked the CMS support for a solution, they provided the following in case anyone else needs to know how to do this:
You have to assign an extra variable:
{{assign var="postfix" value=".mp3"}}
{{$data.formats[$postfix].file_size_string}}
You can use the following syntax to achieve that:
{{$data.formats['.mp4'].file_size_string}}
You don't need to create extra variable.

Rewrite array keys by matching it to first child key, possible in php?

I'm wondering if there is an easy way to match array key to logo_id?
If I cannot find a way to do this, I will need to use array search which can become quite slow with an array of 200 items. Right?
p.s. this is result returned by mysqli fetch result call. Maybe this can be modified to provide array which I need?
Array
(
[0] => Array
(
[logo_id] => 1
[logo_name] => beeline
[logo_level] => 1
[logo_image_path] => logos/1.png
[logo_value] => 2
[logo_hints] =>
)
[1] => Array
(
[logo_id] => 2
[logo_name] => geocell
[logo_level] => 1
[logo_image_path] => logos/2.png
[logo_value] => 4
[logo_hints] =>
)
[2] => Array
(
[logo_id] => 3
[logo_name] => google
[logo_level] => 1
[logo_image_path] => logos/3.png
[logo_value] => 5
[logo_hints] =>
)
[3] => Array
(
[logo_id] => 5
[logo_name] => coca cola
[logo_level] => 1
[logo_image_path] => logos/5.png
[logo_value] => 2
[logo_hints] =>
)
)
Did I explain it good? phh, sorry for bad wording.
this is result returned by mysqli fetch result call. Maybe this can be modified to provide array which I need?
Yes. I assume you mean mysqli_result::fetch_all. Use mysqli_result::fetch_row in a loop instead and construct your array manually with whatever keys you like to.

Having trouble extracting values from the Yelp API via PHP

Let me be up front: I'm a PHP hack. There's probably some stupid mistakes in here. Please point them out if you see them.
What I'm trying to do: I'm creating a page for a restaurant that would like their Yelp reviews displayed. I'm using the Yelp Phone API to grab the reviews for the specific business. Please view the sample response on the Yelp API documentation located here: http://www.yelp.com/developers/documentation/phone_api#sampleResponse
What I've done:
Successfully connected to the API and returned a response
echoed values from the response array in a foreach loop.
If you view the documentation, you can see there are a few levels of the response. I can easily print, echo, whatever values from the second tier, but what I'm really after is all nested in the "reviews" section of the response. I'm having trouble figuring out how to echo the values within the reviews section (eg user_name, review_excerpt etc).
My Code:
$yelpstring = file_get_contents("http://api.yelp.com/phone_search?phone=[redactedphonenumber]&ywsid=[redactedapikey]", true);
$obj = json_decode($yelpstring);
foreach($obj->businesses as $key => $business)
{
$reviews = $business->reviews;
//print_r($reviews);
echo $reviews['user_name'];
}
If I echo $reviews, I just get the word "Array". If I print_r($reviews), I get an expected list of keys and values. If I try to echo a specific value from the array(echo $reviews['user_name'], I get nothing. Any light shed on what I'm doing wrong would be greatly appreciated. I'm sure I'm missing something simple. Thank you for your time!
Edit: print_r($reviews) output:
Array ( [0] => stdClass Object ( [rating_img_url_small] => http://media4.px.yelpcdn.com/static/201012164278297776/img/ico/stars/stars_small_2.png [user_photo_url_small] => http://media2.px.yelpcdn.com/static/201012162819681786/img/gfx/blank_user_extra_small.gif [rating_img_url] => http://media4.px.yelpcdn.com/static/201012163489049252/img/ico/stars/stars_2.png [rating] => 2 [user_url] => http://www.yelp.com/user_details?userid=vZbcPrYPSMFIDIfTub5H1g [url] => http://www.yelp.com/biz/jelly-cafe-denver#hrid:u9ckRV6tKApe6Bu93M93CA [mobile_uri] => http://m.yelp.com/biz/5G2X2q9p7QFdm-LbyutltQ?srid=u9ckRV6tKApe6Bu93M93CA [text_excerpt] => I wanted to like this place. It's got the contemporary name and it's full of hipsters. The place looked clean and the style was fun and cute. I felt like... [user_photo_url] => http://media3.px.yelpcdn.com/static/201012161186834854/img/gfx/blank_user_small.gif [date] => 2011-09-07 [user_name] => boycott p. [id] => u9ckRV6tKApe6Bu93M93CA ) [1] => stdClass Object ( [rating_img_url_small] => http://media4.px.yelpcdn.com/static/201012164278297776/img/ico/stars/stars_small_2.png [user_photo_url_small] => http://media1.px.yelpcdn.com/upthumb/MWu84G5QtmBmT9GoqjT_kg/ss [rating_img_url] => http://media4.px.yelpcdn.com/static/201012163489049252/img/ico/stars/stars_2.png [rating] => 2 [user_url] => http://www.yelp.com/user_details?userid=izF2cGrmqt-u_Z2tDZ8dbg [url] => http://www.yelp.com/biz/jelly-cafe-denver#hrid:OYLeeCMgnpZkk1c9LWu97g [mobile_uri] => http://m.yelp.com/biz/5G2X2q9p7QFdm-LbyutltQ?srid=OYLeeCMgnpZkk1c9LWu97g [text_excerpt] => Food is decent and overpriced, but service is a joke. Your food will take a minimum of 20 minutes, for the basic breakfast. Then when your food does come... [user_photo_url] => http://media1.px.yelpcdn.com/upthumb/MWu84G5QtmBmT9GoqjT_kg/ms [date] => 2011-09-06 [user_name] => April H. [id] => OYLeeCMgnpZkk1c9LWu97g ) [2] => stdClass Object ( [rating_img_url_small] => http://media2.px.yelpcdn.com/static/20101216418129184/img/ico/stars/stars_small_4.png [user_photo_url_small] => http://media1.px.yelpcdn.com/upthumb/3euzdGdLZRFxImY68MSg7w/ss [rating_img_url] => http://media2.px.yelpcdn.com/static/201012164084228337/img/ico/stars/stars_4.png [rating] => 4 [user_url] => http://www.yelp.com/user_details?userid=bHR9UU4vtx2QKZD44O0E5g [url] => http://www.yelp.com/biz/jelly-cafe-denver#hrid:njvNAzfSII3PxXyUymLZ1w [mobile_uri] => http://m.yelp.com/biz/5G2X2q9p7QFdm-LbyutltQ?srid=njvNAzfSII3PxXyUymLZ1w [text_excerpt] => Stopped here for breakfast on a friday morning. We were seated immediately and had a really friendly waitress. I ordered a side order of the Chai french... [user_photo_url] => http://media1.px.yelpcdn.com/upthumb/3euzdGdLZRFxImY68MSg7w/ms [date] => 2011-09-05 [user_name] => Diane F. [id] => njvNAzfSII3PxXyUymLZ1w ) )
Based on the output of print_r you can't reference $reviews['user_name'];
Note that $reviews is an Array of objects. So to access user_name you need to use
echo $reviews[0]->user_name;
And if you have more than one item in the array, you will need a loop like
for ($i = 0; $i<count($reviews); $i++) {
echo $reviews[$i]->user_name;
}
I hope this helps.
$reviews is an array of review objects. You'll need to loop over it to get to the data you're after.

Categories