getting value from stdclass object/array - php

ive been trying for a while now trying to get a value from this std object
stdClass::__set_state(array(
'regions' =>
array (
0 =>
stdClass::__set_state(array(
'id' => 1,
'name' => 'Canada',
'image_url' => 'https://s3-us-west-2.amazonaws.com/staticimageskiind/flags/CA#3x.png',
)),
1 =>
stdClass::__set_state(array(
'id' => 2,
'name' => 'USA',
'image_url' => 'https://s3-us-west-2.amazonaws.com/staticimageskiind/flags/US#3x.png',
)),
2 =>
stdClass::__set_state(array(
'id' => 3,
'name' => 'Global',
'image_url' => 'https://s3-us-west-2.amazonaws.com/staticimageskiind/flags/GLBL#3x.png',
)),
3 =>
stdClass::__set_state(array(
'id' => 4,
'name' => 'Australia',
'image_url' => 'https://s3-us-west-2.amazonaws.com/staticimageskiind/flags/AU#3x.png',
)),
),
'info' =>
stdClass::__set_state(array(
'code' => 'INFO_MARKETPLACE_RETRIEVED_REGIONS',
'name' => 'Marketplace Regions Retrieved',
'message' => 'A list of marketplace regions has been retrieved.',
)),
));
but i can't seem to get a value from this object, help would be appreciated, ive searched online but couldnt find out a solution

Did you try:
echo $object->regions[0]->name;
For example?

i have managed to get the correct output i wanted by
$abc = $result->regions;
echo $abc[1]->name;
hope this helps anyone in need

Related

Access nested objects and arrays

I am working with the Mailgun API. I have a variable with some objects and arrays inside that looks like this:
$result =
stdClass::__set_state(array(
'http_response_body' =>
stdClass::__set_state(array(
'items' =>
array (
0 =>
stdClass::__set_state(array(
'geolocation' =>
stdClass::__set_state(array(
'country' => 'UK',
'region' => '72',
'city' => 'London',
)),
'tags' =>
array (
),
'mailing-list' =>
stdClass::__set_state(array(
'list-id' => 'email#address.com',
'address' => 'email#address.com',
'sid' => '123456',
)),
'ip' => '123.456.789',
'log-level' => 'info',
'id' => 'ABCA',
'campaigns' =>
array (
0 =>
stdClass::__set_state(array(
'name' => 'TEST',
'id' => 'test123',
)),
),
)),
),
'http_response_code' => 200,
));
?>
I need to access every single information inside (e.g. the IP, the country, the region), . I tried multiple solutions, including using a foreach.
foreach ($result as $row) {
echo $row->items->ip;
}
However I keep getting
Catchable fatal error: Object of class stdClass could not be converted to string
I know I am not accessing the data the right way. Could you advice?
I think that you mean to be looping over $result->http_response_body->items. What you are actually doing is looping over $result->items which doesn't seem to exist.
foreach($result->http_response_body->items as $item) {
echo $item->ip;
}

How to access this array

array (
'_attributes' =>
array (
'name' => 'Rothco Product API - Variations',
),
'item_variations' =>
array (
0 =>
stdclass::__set_state(array(
'item_variation_id' => 2,
'item_index' => 3146,
'rothco_item_no' => '10002',
'upc' => '023063601212',
'inventory' => 99,
'created_date' => '2014-11-28 10:06:45.000',
'weight' => '.4000',
'image_filename' => '10002-A.jpg',
'catalog_page_no' => 183,
'msrp' => '20.9900',
'map' => '.0000',
'diameter' => '',
'price' => '8.100000',
'case_price' => NULL,
'case_quantity' => NULL,
'statuses' => '',
)),
),
)
This is my array $list.
I want to access 'item_variations' value from this array,
but if I try $list['item_variations'], or $list->['item_variations']
it is not working
If you want to reach just item_variations then
echo $list['item_variations'];
is sufficient. Things get more tricky if you would like to i.e. get value if created_date from your sample data as you got mix of arrays and objects so that require different access:
echo $list['item_variations'][0]->created_date;
which would output
2014-11-28 10:06:45.000

cakephp: structure database data returns

a possible return of a DB query looks like this:
array(
(int) 0 => array(
'Question' => array(
'id' => '737',
'question' => 'what is 1x7?',
),
'Answer' => array(
(int) 0 => array(
'id' => '2373',
'question_id' => '737',
'correct' => true,
'answer' => 'possible answer1',
'created' => '2014-05-08 13:46:43',
'modified' => '2014-05-08 13:46:43'
),
(int) 1 => array(
'id' => '2374',
'question_id' => '737',
'correct' => false,
'answer' => 'possible answer2',
'created' => '2014-05-08 13:46:43',
'modified' => '2014-05-08 13:46:43'
)
),
'Linkquestioncategory' => array(
(int) 0 => array(
'id' => '608',
'question_id' => '737',
'category_id' => '5',
'created' => '2014-05-08 13:46:47',
'modified' => '2014-05-08 13:46:47',
'Category' => array(
'id' => '5',
'name' => 'Simple Math',
'active' => true,
'linkquestioncategory_count' => '64',
'created' => '2014-02-03 09:20:54',
'modified' => '2014-03-04 14:47:05'
)
)
)
),
In order to clean it up to avoid sending to much unwanted data => my questions is => how can I get rid of those fields like
Answer.created
Linkquestioncategory.created
Linkquestioncategory.Category.created
I know that I can use conditions 'fields' to select the selected fields, but as far as I know, this works only for 'Question', but how can I manipulate those deeper array data?
Can I do this also with the 'fields' condition? If yes, how?
Thanks!!
http://nuts-and-bolts-of-cakephp.com/2008/09/05/example-of-cakephps-containable-for-deep-model-bindings/
This helped me a lot!!
Thanks!!

Accessing arrays and or objects

*I solved it myself... like 10 minutes later. *
$varbgimg = $row->_field_data['nid']['entity']->field_slideimage['und'][0]['uri'];
Is what I used...
Hope someone can help me. How can I access 'uri' => 'public://veglo8.jpg'?
This is from Drupal and the Views module. If someone could maybe even help me with my ultimate goal, I would appreciate that...
I have a field in Views called slideimage. I want to add a style="background-image:url(image field URL);" to my div. Tried to rewrite the output but it strips the style...
Thanks in advance.
stdClass::__set_state(array(
'nid' => '20',
'node_title' => 'Test 1',
'field_data_field_slideimage_node_entity_type' => 'node',
'field_data_body_node_entity_type' => 'node',
'_field_data' =>
array (
'nid' =>
array (
'entity_type' => 'node',
'entity' =>
stdClass::__set_state(array(
'vid' => '20',
'uid' => '1',
'title' => 'Test 1',
'log' => '',
'status' => '1',
'comment' => '1',
'promote' => '0',
'sticky' => '0',
'nid' => '20',
'type' => 'test',
'language' => 'und',
'created' => '1358336066',
'changed' => '1358337923',
'tnid' => '0',
'translate' => '0',
'revision_timestamp' => '1358337923',
'revision_uid' => '1',
'body' =>
array (
'und' =>
array (
0 =>
array (
'value' => 'Body text here',
'summary' => '',
'format' => 'filtered_html',
'safe_value' => '
Body text here',
'safe_summary' => '',
),
),
),
'field_slideimage' =>
array (
'und' =>
array (
0 =>
array (
'fid' => '8',
'alt' => '',
'title' => '',
'width' => '624',
'height' => '390',
'uid' => '1',
'filename' => 'veglo8.jpg',
'uri' => 'public://veglo8.jpg',
'filemime' => 'image/jpeg',
'filesize' => '27393',
'status' => '1',
'timestamp' => '1358336725',
'rdf_mapping' =>
You can use file_create_url to convert public://... to real world URLs.
$real_url = file_create_url($img_src);
I've used the pathinfo() function for this in the past.
http://php.net/manual/en/function.pathinfo.php
You can apply styles programmatically with this by replacing user-picture[0]['uri'] with your image url. I think you can even place the whole public:// url in there and it will work just fine as well.
THUMBNAIL_STYLE = 'thumbnail';
// now get the full image url from the uri and the style
$default_thumbnail = image_style_url($THUMBNAIL_STYLE, $user->picture[0]['uri']);
http://drupal.org/node/1425836

How to get all the specific key value from multidimensional array in php?

How can I get the all the 'name' and 'city' value back in an array from the following multidimensional array?
$myarray=Array(Array('name' => 'A','id' => '1', 'phone' => '416-23-55',
Base => Array ('city' => 'toronto'),'EBase' => Array('city' => 'North York'),
'Qty' => '1'), (Array('name' =>'B','id' => '1','phone' => '416-53-66','Base' =>
Array ('city' => 'qing'), 'EBase' => Array('city' => 'chong'),'Qty' => '2')));
I expect the returned value be
$namearray=Array('A','B');
$basecityarray=Array('toronto','qing');
$Ebasecityarray=Array('North York','chong');
Thank you!
You can definitely try something like this:
$shop = array( array( 'Title' => "rose",
'Price' => 1.25,
'Number' => 15
),
array( 'Title' => "daisy",
'Price' => 0.75,
'Number' => 25,
),
array( 'Title' => "orchid",
'Price' => 1.15,
'Number' => 7
)
);
You can access the first row as
echo $shop[0][0]." costs ".$shop[0][1]." and you get ".$shop[0][2]."<br />";
or $shop[0]->Title will return to you rose.

Categories