php getting value from object

gileneusz

I've got basic array from my api:

{ "title": "offer title", "body": "offer body", 
"specialities": { "lang": "en", "id": "1", "icon": "0",
"name": "speciality name 1" }, "region": "region1" }

I want to get id value from request for my php variable lets say: $idVariable. How can I do it?

I tried something like:

$idVariable = $request->specialities[0]->id

but it seems not working. What is the right way?

Then how should I work with the arrays of object in this case:

{ "title": "offer title", "body": "offer body", 
"specialities": [
{ "lang": "en", "id": "1", "icon": "0", "name": "speciality name 1" },
 { "lang": "en", "id": "2", "icon": "0", "name": "speciality name 2" },
 { "lang": "en", "id": "2", "icon": "0", "name": "speciality name 3" },
 etc...], "region": "region1" }

To get id's of every object in specialities array? I know that it could be a duplicate question, but I ask for just a basic example.

I tried to use json decode like below:

json_decode($request->get('specialties'))->id

edit:

The almost-right way to do it is to decode json file first:

$data = json_decode($request);

and then get the right property from the array:

$id = $data['specialities'][0]['id'];

the problem now is that id is a string not an integer and by simply using:

$int_id = intval($id)

I've got $int_id = 0 instead of 1,2,3 etc

TIGER

You are getting a response from API in JSON you should use json_decode() and then use the data. Try this code.

$json = '{ "title": "offer title", "body": "offer body", 
"specialities": { "lang": "en", "id": "1", "icon": "0",
"name": "speciality name 1" }, "region": "region1" }';

$data = json_decode($json);
echo $data->specialities->id;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

laravel/php getting object value from request

From Dev

Getting value from HTML object in PHP

From Dev

Getting value from a collection object

From Dev

Getting value from object in javascript

From Dev

Getting value of explode from php

From Dev

Getting value from database with PHP

From Dev

Getting array key(protected)-value from an array inside object (response from RTM-php)?

From Dev

Getting array key(protected)-value from an array inside object (response from RTM-php)?

From Dev

DataRowView object getting null value at time of getting value from DataGrid

From Java

Getting key with the highest value from object

From Dev

Getting Map value from JAXB Response Object

From Dev

Getting checkbox value from json object

From Dev

perl: getting a value from a function of the object

From Dev

Getting value from array object(DateTime)

From Dev

Getting Parent Object, from nested value

From Dev

getting value from stdclass object/array

From Dev

Error in getting value from json object

From Dev

Getting a random object from an array in PHP

From Dev

PHP : Getting each value from the array and calculate

From Dev

Getting the numeric value from a currency string in PHP

From Dev

Php getting a value from a text file

From Dev

Getting specific value from Ajax and PHP

From Dev

Trouble getting value from php and display on html

From Dev

Getting a value from an PHP associative array

From Dev

Getting specified value from array PHP

From Dev

Remove Object from array by object value in php

From Dev

Getting unexpected value while accessing value from Object in Javascript

From Dev

Getting the highest value excluding a certain value from a nested object property

From Dev

Getting Data From stdClass Object From Facebook API with PHP

Related Related

  1. 1

    laravel/php getting object value from request

  2. 2

    Getting value from HTML object in PHP

  3. 3

    Getting value from a collection object

  4. 4

    Getting value from object in javascript

  5. 5

    Getting value of explode from php

  6. 6

    Getting value from database with PHP

  7. 7

    Getting array key(protected)-value from an array inside object (response from RTM-php)?

  8. 8

    Getting array key(protected)-value from an array inside object (response from RTM-php)?

  9. 9

    DataRowView object getting null value at time of getting value from DataGrid

  10. 10

    Getting key with the highest value from object

  11. 11

    Getting Map value from JAXB Response Object

  12. 12

    Getting checkbox value from json object

  13. 13

    perl: getting a value from a function of the object

  14. 14

    Getting value from array object(DateTime)

  15. 15

    Getting Parent Object, from nested value

  16. 16

    getting value from stdclass object/array

  17. 17

    Error in getting value from json object

  18. 18

    Getting a random object from an array in PHP

  19. 19

    PHP : Getting each value from the array and calculate

  20. 20

    Getting the numeric value from a currency string in PHP

  21. 21

    Php getting a value from a text file

  22. 22

    Getting specific value from Ajax and PHP

  23. 23

    Trouble getting value from php and display on html

  24. 24

    Getting a value from an PHP associative array

  25. 25

    Getting specified value from array PHP

  26. 26

    Remove Object from array by object value in php

  27. 27

    Getting unexpected value while accessing value from Object in Javascript

  28. 28

    Getting the highest value excluding a certain value from a nested object property

  29. 29

    Getting Data From stdClass Object From Facebook API with PHP

HotTag

Archive