I am new to JSON and I am having a problem accessing particular values in the JSON array that is being returned from the API call. For instance, I would like to access the name value in the response data below (these are the first two responses from the API call). I know I am missing something obvious but I can't quit get it. Any help would be appreciated:
ReadResponse::__set_state(array(
0 =>
array (
'address' => '6866 N Rochester Rd',
'category' => 'Food & Beverage > Restaurants',
'category_ids' =>
array (
0 => 347,
),
'category_labels' =>
array (
0 =>
array (
0 => 'Social',
1 => 'Food and Dining',
2 => 'Restaurants',
),
),
'country' => 'us',
'factual_id' => '8e6aacfa-b435-407d-83f9-90cec16c1cf8',
'latitude' => 42.69804075,
'locality' => 'Rochester Hills',
'longitude' => -83.134586375,
'name' => 'Sushi Little Tokyo',
'neighborhood' =>
array (
0 => '\\Livernois-Tienken\\',
),
'postcode' => '48306',
'region' => 'MI',
'status' => '1',
'tel' => '(248) 608-1260',
),
1 =>
array (
'address' => '2964 S Rochester Rd',
'category' => 'Food & Beverage > Restaurants',
'category_ids' =>
array (
0 => 347,
),
'category_labels' =>
array (
0 =>
array (
0 => 'Social',
1 => 'Food and Dining',
2 => 'Restaurants',
),
),
'country' => 'us',
'factual_id' => '35d2fc06-a941-4dbf-9dcb-204964fec730',
'latitude' => 42.636786765426,
'locality' => 'Rochester Hills',
'longitude' => -83.131888674506,
'name' => 'Pudthi and Sushi',
'postcode' => '48307',
'region' => 'MI',
'status' => '1',
'tel' => '(248) 299-6890',
'website' => 'http://www.pudthaiandsushi.com',
),
Try this PHP function json_decode() to translate your JSON response into a PHP multidimensional array where you can easily access the values in the array like
the name would be
$name = $myPHPArray->country->name;
This is the result of a var_export. The data is all contained in an instance of the ReadResponse. To find out how you can access the data, read the source or documentation of that class.
Related
I'd like to return the value of the Birthday field from the following massive mixed array of data containing nested arrays and stdClass Objects. I tried the following code but it didn't work, I would be extremely grateful for any help.
$data->_field_data['nid']['entity']->field_player_birthday['und'][0]['value'];
PHP:
stdClass::__set_state(array(
'node_field_data_field_game_players_nid' => '2931',
'node_title' => '1539634465',
'nid' => '8114',
'node_language' => 'tr',
'field_data_field_player_birthday_node_entity_type' => 'node',
'_field_data' => array (
'node_field_data_field_game_players_nid' => array (
'entity_type' => 'node',
'entity' => stdClass::__set_state(
array( 'vid' => '2931',
'uid' => '1',
'title' => 'Fernando Muslera',
'log' => '',
'status' => '1',
'comment' => '1',
'promote' => '0',
'sticky' => '0',
'ds_switch' => '',
'nid' => '2931',
'type' => 'player',
'language' => 'und',
'created' => '1520727843',
'changed' => '1539137801',
'tnid' => '0',
'translate' => '0',
'revision_timestamp' => '1539137801',
'revision_uid' => '1',
'body' =>
array ( ), 'field_player_address' => array ( 'und' => array ( 0 =>
array ( 'country' => 'AR', 'administrative_area' => 'B',
'sub_administrative_area' => NULL, 'locality' => 'Buenos Aires',
'dependent_locality' => '', 'postal_code' => '', 'thoroughfare' => '',
'premise' => '', 'sub_premise' => NULL, 'organisation_name' => NULL,
'name_line' => NULL, 'first_name' => NULL, 'last_name' => NULL, 'data'
=> NULL, ), ), ), 'field_geofield' => array ( 'und' => array ( 0 => array ( 'geom' => 'POINT (-58.3815591 -34.6036844)', 'geo_type' =>
'point', 'lat' => '-34.603684400000', 'lon' => '-58.381559100000',
'left' => '-58.381559100000', 'top' => '-34.603684400000', 'right' =>
'-58.381559100000', 'bottom' => '-34.603684400000', 'geohash' =>
'69y7pkxfgjdq4', ), ), ), 'field_player_search_news' => array ( ),
'field_player_map' => array ( ), 'field_player_birthday' => array (
'und' => array ( 0 => array ( 'value' => '1986-06-16 00:00:00',
'timezone' => 'America/New_York', 'timezone_db' => 'America/New_York',
'date_type' => 'datetime', ), ), ), 'field_player_team' => array (
'und' => array ( 0 => array ( 'tid' => '328', ), ), ), 'field_image'
=> array ( ), 'field_game_rating' => array ( 'und' => array ( 0 => array ( 'rating' => '90', 'target' => NULL, ), ), ), 'rdf_mapping' =>
array ( 'rdftype' => array ( 0 => 'sioc:Item', 1 => 'foaf:Document',
), 'title' => array ( 'predicates' => array ( 0 => 'dc:title', ), ),
'created' => array ( 'predicates' => array ( 0 => 'dc:date', 1 =>
'dc:created', ), 'datatype' => 'xsd:dateTime', 'callback' =>
'date_iso8601', ), 'changed' => array ( 'predicates' => array ( 0 =>
'dc:modified', ), 'datatype' => 'xsd:dateTime', 'callback' =>
'date_iso8601', ), 'body' => array ( 'predicates' => array ( 0 =>
'content:encoded', ), ), 'uid' => array ( 'predicates' => array ( 0 =>
'sioc:has_creator', ), 'type' => 'rel', ), 'name' => array (
'predicates' => array ( 0 => 'foaf:name', ), ), 'comment_count' =>
array ( 'predicates' => array ( 0 => 'sioc:num_replies', ), 'datatype'
=> 'xsd:integer', ), 'last_activity' => array ( 'predicates' => array ( 0 => 'sioc:last_activity_date', ), 'datatype' => 'xsd:dateTime',
'callback' => 'date_iso8601', ), ), 'path' => array ( 'pathauto' =>
'1', ), 'cid' => '0', 'last_comment_timestamp' => '1520727843',
'last_comment_name' => NULL, 'last_comment_uid' => '1',
'comment_count' => '0', 'name' => 'efet', 'picture' => '0', 'data' =>
'a:7:{s:7:"contact";i:1;s:5:"block";a:1:{s:6:"system";a:1:{s:4:"main";i:1;}}s:16:"ckeditor_default";s:1:"t";s:20:"ckeditor_show_toggle";s:1:"t";s:14:"ckeditor_width";s:4:"100%";s:13:"ckeditor_lang";s:2:"en";s:18:"ckeditor_auto_lang";s:1:"t";}',
)), ), ), 'field_field_player_birthday' => array ( 0 => array (
'rendered' => array ( '#markup' => '1986', '#access' => true, ), 'raw'
=> array ( 'value' => '1986-06-16 00:00:00', 'timezone' => America/New_York', 'timezone_db' => 'America/New_York', 'date_type'
> => 'datetime', ), ), ), ))
You are close (as I've had to change the objects slightly to test, I hope this works)...
echo $data->_field_data['node_field_data_field_game_players_nid']['entity']
->field_player_birthday['und'][0]['value'];
Basically instead of nid, it's node_field_data_field_game_players_nid.
I need to Search collection in mongodb having
'food_name' => 'fish'
and
'room_features' =>
array (
0 => 'Shower',
1 => 'Hairdryer',
),
I tried the following code. But the result is not-correct. I think multiple $eq is not allowed (same index in array).
array (
'$and' =>
array (
array (
'food' =>
array (
'$elemMatch' =>
array (
'food_name' =>
array (
'$eq' => 'fish',
),
),
),
),
array (
'room' =>
array (
'$elemMatch' =>
array (
'room_features' =>
array (
'$elemMatch' =>
array (
'$eq' => 'Shower'
'$eq' => 'Hairdryer'
),
),
'roomrate' =>
array (
'$eq' => new MongoInt32(2500),
),
),
),
),
),
)
Here is the document I need to search.
array (
'_id' => new MongoId("59670aca7fafd8342e3c9869"),
'subcat_name' => 'Test',
'place' => '',
'description' => '',
'created_date' => '1499970060',
'created_by' => 'Admin',
'openingtime' => '',
'closingtime' => '',
'hotel_class_id' => '594245f67fafd87e243c986a',
'hotel_type_id' => '594244177fafd884563c9869',
'latitude' => '0',
'longitude' => '0',
'dist_id' => '5911966a7fafd8c83c3c986a',
'cat_id' => '58fb230e7fafd883183c986d',
'featured' => '0',
'visited' => new MongoInt64(5),
'subcat_slug' => 'test-trivandrum-1',
'image' => NULL,
'food' =>
array (
0 =>
array (
'food_id' => '149992634012642164',
'region_id' => '5944ba947fafd883333c9869',
'food_name' => 'fish',
'type' => 'veg',
'rate' => '100',
),
1 =>
array (
'food_id' => '14999366891994980639',
'region_id' => '595c75c17fafd835173c986c',
'food_name' => 'curry',
'type' => 'veg',
'rate' => '1000',
),
),
'room' =>
array (
0 =>
array (
'room_id' => '14999346791721342880',
'roomtype' => 'DELUXE KING ROOM1',
'roomrate' => new MongoInt64(2500),
'image' => 'beach_icon33.png',
'room_features' =>
array (
0 => 'Shower',
1 => 'Hairdryer',
),
),
1 =>
array (
'room_id' => '14999346901389554873',
'roomtype' => 'DELUXE KING ROOM new',
'roomrate' => new MongoInt64(4000),
'image' => 'beach_icon34.png',
'room_features' =>
array (
0 => 'Shower',
1 => 'Bathrobe',
),
),
),
)
Please Give me an alternate way to search multiple item from array.
Thanks in advance.
I think if you want to query list you can add the list in query,
try this
{
"food" : {
"$elemMatch": {
"food_name" : "fish"
}
},
"room" : {
"$elemMatch": {
"room_features" : ["Shower", "Hairdryer"]
}
},
}
Hope this help.
have an array like this:
array (
'#attributes' =>
array (
'status' => 'ok',
),
'time_entries' =>
array (
'#attributes' =>
array (
'page' => '1',
'per_page' => '100',
'pages' => '1',
'total' => '33',
),
'time_entry' =>
array (
0 =>
array (
'time_entry_id' => '1411884',
'staff_id' => '22384',
'project_id' => '11116',
'task_id' => '3296',
'hours' => '1.75',
'date' => '2017-02-20',
'notes' => 'What is Quadra, Events Slider, Event Page Setup',
'billed' => '0',
),
1 =>
array (
'time_entry_id' => '1411254',
'staff_id' => '22384',
'project_id' => '11116',
'task_id' => '3296',
'hours' => '1.5',
'date' => '2017-02-17',
'notes' => 'Events Slider, Background overlay, Intro',
'billed' => '0',
),
2 =>
array (
'time_entry_id' => '1410694',
'staff_id' => '22384',
'project_id' => '11116',
'task_id' => '3296',
'hours' => '2.75',
'date' => '2017-02-16',
'notes' => 'Background Image SVGs, Header, Footer',
'billed' => '0',
),
3 =>
array (
'time_entry_id' => '1410586',
'staff_id' => '22384',
'project_id' => '11116',
'task_id' => '3296',
'hours' => '0.5',
'date' => '2017-02-15',
'notes' => 'Site Background
Assign Less Variables',
'billed' => '0',
),
4 =>
array (
'time_entry_id' => '1409621',
'staff_id' => '22384',
'project_id' => '11116',
'task_id' => '3296',
'hours' => '0.25',
'date' => '2017-02-14',
'notes' => 'Theme Install',
'billed' => '0',
),
...it actually goes on further than those first 4. What I am looking to do is sort these arrays by the key ["task_id"] so that I can group those together, and then add together the ["hours"] key - so that at the end I can output the total number of hours worked on under each task_id.
I've tried a bit of 'array_merge_recursive'and similar but I'm at a loss; this is PHP a good bit above my level. Help very appreciated.
So you're probably just better off doing a group by and summation by generating a new set of data rather than trying to modify this array.
Something like this will give you what you're looking for:
$time_entries = $your_array["time_entries"]["time_entry"];
$task_hours = [];
foreach ($time_entries as $time_entry) {
if (!isset($task_hours[$time_entry["task_id"]])) {
$task_hours[$time_entry["task_id"]] = 0;
}
$task_hours[$time_entry["task_id"]] += (float) $time_entry["hours"];
}
$task_hours would give you a value like:
Array
(
[3296] => 5
[1879] => 0.25
)
Instead of sorting the array and then summing the hours why not process over the relevant part of that array and do the summation all in one go.
$tots = array();
foreach( $array['time_entry'] as $task ) {
if ( isset($tots['task_id']) ) {
$tots['task_id'] += $task['hours'];
} else {
$tots['task_id'] = $task['hours'];
}
}
You should now have an array where the key is the task_id and its value is the summation of all that keys hours.
If you want the task_id's in order then sort the $tots array before outputing any results
ksort($tots);
If you want the results in order of the number of hours
asort($tots);
I'm playing around with the twitter API and i'm trying to parse the json output. However somehow i'm doing something wrong and I'm not getting what I would like to get.
In this case i'm writing in PHP. Started looping thru the array and screen_name and text are working accordingly.
foreach ($statuses as $obj) {
$screen_name = filter_var($obj->user->screen_name, FILTER_SANITIZE_STRING);
$text = filter_var($obj->text, FILTER_SANITIZE_STRING);
$urls = filter_var($obj->entities->urls->url, FILTER_SANITIZE_URL);
}
the JSON array output:
array (
0 =>
stdClass::__set_state(array(
'created_at' => 'Fri Oct 07 15:31:01 +0000 2016',
'text' => 'test',
'entities' =>
stdClass::__set_state(array(
'hashtags' =>
array (
0 =>
stdClass::__set_state(array(
'text' => '30Under30',
'indices' =>
array (
0 => 36,
1 => 46,
),
)),
),
'symbols' =>
array (
),
'user_mentions' =>
array (
),
'urls' =>
array (
0 =>
stdClass::__set_state(array(
'url' => 'https://Forbes.com',
'expanded_url' => 'https://twitter.com/i/web/status/784415744427687936',
'display_url' => 'twitter.com/i/web/status/7…',
'indices' =>
array (
0 => 117,
1 => 140,
),
)),
),
)),
'source' => 'Sprinklr',
'in_reply_to_screen_name' => NULL,
'user' =>
stdClass::__set_state(array(
'id' => 91478624,
'screen_name' => 'test',
'url' => 'http://Forbes.com',
'entities' =>
stdClass::__set_state(array(
'url' =>
stdClass::__set_state(array(
'urls' =>
array (
0 =>
stdClass::__set_state(array(
'url' => 'http://Forbes.com',
'expanded_url' => 'http://forbes.com',
'display_url' => 'forbes.com',
'indices' =>
array (
0 => 0,
1 => 22,
),
)),
),
)),
'description' =>
stdClass::__set_state(array(
'urls' =>
array (
0 =>
stdClass::__set_state(array(
'url' => 'http://Forbes.com',
'expanded_url' => 'http://Forbes.com',
'display_url' => 'Forbes.com',
'indices' =>
array (
0 => 28,
1 => 48,
),
)),
),
)),
)),
'protected' => false,
'notifications' => false,
)),
'geo' => NULL,
'lang' => 'en',
)),
)
Since urls is an array, you need to index it.
$urls = filter_var($obj->entities->urls[0]->url, FILTER_SANITIZE_URL);
After fill a form with two adresses: Billing address and personal address(using FormHelper):
...
<div><?php echo $this->Form->input('Contact.1.name'); ?></div></td>
<td> <div>name1</div>
<div><?php echo $this->Form->input('Contact.2.name'); ?></div></td>
...
, i get this array:
'Contact' => array(
(int) 1 => array(
'name' => 'asdf',
'nif' => '123123123',
'address' => 'pcsa',
'zipcode' => '1234',
'street' => 'das'
),
(int) 2 => array(
'name' => 'fsad',
'nif' => '321321321',
'address' => 'asdp',
'zipcode' => '1234',
'street' => 'fas'
)
)
But before i save this array to send data for DB i need to set manually user_id=32 (for example).
Can you give me any suggestion how to do that? so i can get this result:
'Contact' => array(
(int) 1 => array(
'name' => 'asdf',
'nif' => '123123123',
'address' => 'pcsa',
'zipcode' => '1234',
'street' => 'das',
'user_id' => 32 <----------------
),
(int) 2 => array(
'name' => 'fsad',
'nif' => '321321321',
'address' => 'asdp',
'zipcode' => '1234',
'street' => 'fas',
'user_id' => 39 <---------------
)
)
My purpose here is to save data from a form to a table "contacts" for the same user_id. for that reason i can't use user_id as a input form, i need to do that after form and before inserte on my DB. For that reason, how can i set this array with user_id?
Checkout the CakePHP Hash library you can do stuff like
$this->request->data = Hash::insert($this->request->data, 'Contact.{n}.user_id', 123);
Example
$contacts = array(
'Contact' => array(
array(
'name' => 'asdf',
'nif' => '123123123',
'address' => 'pcsa',
'zipcode' => '1234',
'street' => 'das'
),
array(
'name' => 'fsad',
'nif' => '321321321',
'address' => 'asdp',
'zipcode' => '1234',
'street' => 'fas'
)
)
);
$contacts = HASH::insert($contacts, 'Contact.{n}.user_id', 123);
pr($contacts);
Output
Array
(
[Contact] => Array
(
[0] => Array
(
[name] => asdf
[nif] => 123123123
[address] => pcsa
[zipcode] => 1234
[street] => das
[user_id] => 123
)
[1] => Array
(
[name] => fsad
[nif] => 321321321
[address] => asdp
[zipcode] => 1234
[street] => fas
[user_id] => 123
)
)
)
Try this
foreach ($this->request->data['Contact'] as &$contact) {
$contact['user_id'] = 32;
}
thanks a lot for your suggestions, but this is the final solution for me.
$this->request->data['Contact'][1]['user_id'] = 32;
$this->request->data['Contact'][2]['user_id'] = 39;
Simply as that, i didn't see it because i was debugging wrong.