Php get object value by key

get value by key of php object array

The second param in json_decode makes sure it returns only arrays (Manual.). This way you can use array function like array_column (Manual) and array_combine (Manual), and get an array that is very close to the structure you want.

Test Case, since no code is to short for it.

Solution 2

Accessing each property is how you normally access a property of an object.
Note that since «First name» has a space, it cannot be accessed by the arrow notation and must be enclosed in curly braces. For any property that doesn’t have a space, there is no need for the curly braces.

The reason your code was failing is because you were trying to access properties using the square bracket notation used for arrays.

I am aware that you are not able to edit the actual array output, but if you can edit the JSON then this will solve your problems.

< "meta_data": < "First name": < "id": 2113, "key": "First name", "value": "Recipient First Name" >, "Last Name": < "id": 2114, "key": "Last Name", "value": "Recipient Last Name" >, "addressLine 1": < "id": 2115, "key": "addressLine 1", "value": "Recipient Address Line 1" >, "addressLine2": < "id": 2116, "key": "addressLine2", "value": "Recipient Address Line 2" >, "City": < "id": 2117, "key": "City", "value": "Recipient Town/City" >, "Region": < "id": 2118, "key": "Region", "value": "Recipient Region/County" >, "Country": < "id": 2119, "key": "Country", "value": "N/A" >, "Postcode": < "id": 2120, "key": "Postcode", "value": "Recipient Postcode" >> >
var_dump($item_data_decode->meta_data->->value); // outputs "Recipient First Name" 

Ben H

Comments

I have a PHP object and I am trying to get the value by the key without using a foreach. If I do the below I am able to get the value:

$item_data_decode->meta_data[0]->value; 

but the items may be in different orders so cannot count on this method and I need to use the key however this doesn’t work:

$item_data_decode->meta_data['First Name']; 
$item_data_decode = json_decode($item_values); if (!empty($item_data_decode->meta_data)) < $fName = $item_data_decode->meta_data['First Name']->value; > 
$a = new stdClass(); $a->meta_data = array(); $a->meta_data[0] = new stdClass(); $a->meta_data[0]->id = "2113"; $a->meta_data[0]->key = "First Name"; $a->meta_data[0]->value = "Recipient First Name"; $a->meta_data[1] = new stdClass(); $a->meta_data[1]->id = "2114"; $a->meta_data[1]->key = "Last Name"; $a->meta_data[1]->value = "Recipient Last Name"; $a->meta_data[2] = new stdClass(); $a->meta_data[2]->id = "2115"; $a->meta_data[2]->key = "addressLine 1"; $a->meta_data[2]->value = "Recipient Address Line 1"; $a->meta_data[3] = new stdClass(); $a->meta_data[3]->id = "2116"; $a->meta_data[3]->key = "addressLine2"; $a->meta_data[3]->value = "Recipient Address Line 2"; $a->meta_data[4] = new stdClass(); $a->meta_data[4]->id = "2117"; $a->meta_data[4]->key = "City"; $a->meta_data[4]->value = "Recipient Town/City"; $a->meta_data[5] = new stdClass(); $a->meta_data[5]->id = "2118"; $a->meta_data[5]->key = "Region"; $a->meta_data[5]->value = "Recipient Region/County"; $a->meta_data[6] = new stdClass(); $a->meta_data[6]->id = "2119"; $a->meta_data[6]->key = "Country"; $a->meta_data[6]->value = "N/A"; $a->meta_data[7] = new stdClass(); $a->meta_data[7]->id = "2120"; $a->meta_data[7]->key = "Postcode"; $a->meta_data[7]->value = "Recipient Postcode"; // outputs [meta_data] => Array ( [0] => stdClass Object ( [id] => 2113 Php get object value by key => First Name [value] => Recipient First Name ) [1] => stdClass Object ( [id] => 2114 Php get object value by key => Last Name [value] => Recipient Last Name ) [2] => stdClass Object ( [id] => 2115 Php get object value by key => addressLine 1 [value] => Recipient Address Line 1 ) [3] => stdClass Object ( [id] => 2116 Php get object value by key => addressLine2 [value] => Recipient Address Line 2 ) [4] => stdClass Object ( [id] => 2117 Php get object value by key => City [value] => Recipient Town/City ) [5] => stdClass Object ( [id] => 2118 Php get object value by key => Region [value] => Recipient Region/County ) [6] => stdClass Object ( [id] => 2119 Php get object value by key => Country [value] => N/A ) [7] => stdClass Object ( [id] => 2120 Php get object value by key => Postcode [value] => Recipient Postcode ) ) 
Array ( [id] => 232 [order_id] => 320 [name] => Tb [product_id] => 50 [variation_id] => 0 [quantity] => 1 [tax_class] => [subtotal] => 50 [subtotal_tax] => 0 [total] => 50 [total_tax] => 0 [taxes] => Array ( [total] => Array ( ) [subtotal] => Array ( ) ) [meta_data] => Array ( ) ) Array ( [id] => 233 [order_id] => 320 [name] => Turtle Bay Gift Card [product_id] => 50 [variation_id] => 0 [quantity] => 1 [tax_class] => [subtotal] => 30 [subtotal_tax] => 0 [total] => 30 [total_tax] => 0 [taxes] => Array ( [total] => Array ( ) [subtotal] => Array ( ) ) [meta_data] => Array ( [0] => Array ( [id] => 2113 Php get object value by key => First Name [value] => Recipient First Name ) [1] => Array ( [id] => 2114 Php get object value by key => Last Name [value] => Recipient Last Name ) [2] => Array ( [id] => 2115 Php get object value by key => addressLine 1 [value] => Recipient Address Line 1 ) [3] => Array ( [id] => 2116 Php get object value by key => addressLine2 [value] => Recipient Address Line 2 ) [4] => Array ( [id] => 2117 Php get object value by key => City [value] => Recipient Town/City ) [5] => Array ( [id] => 2118 Php get object value by key => Region [value] => Recipient Region/County ) [6] => Array ( [id] => 2119 Php get object value by key => Country [value] => N/A ) [7] => Array ( [id] => 2120 Php get object value by key => Postcode [value] => Recipient Postcode ) ) ) 

Источник

Читайте также:  Python является ли число числом фибоначчи

PHP get object with specific key value [duplicate]

Those are quite similar to arrays and with PHP you can easily convert between those two with something called casting : As this little example shows (I just used the variable to make the cast more visible, you normally can write it as one-liner), the cast from object to array does allow you to use the known function ( ) on the object. Because arrays and stdClass Objects in PHP are so similar, this works in both directions: This also works with other types in PHP, so probably apart from your concrete problem, something worth to read about:

PHP get object with specific key value [duplicate]

I am trying to get only these metafields objects with namespace=»global», how can I do that in PHP ?

$response = json_decode($response, true); $filtered['metafields'] = array_filter($response['metafields'], function ($item) < return $item['namespace'] === 'global'; >); var_dump($filtered); array(1) < ["metafields"]=>array(2) < [0]=>array(2) < ["id"]=>int(30007223558) ["namespace"]=> string(6) "global" > [2]=> array(2) < ["id"]=>int(154644565) ["namespace"]=> string(6) "global" > > > 

Php — how to get object value, Teams. Q&A for work. Connect and share knowledge within a single location that is structured and easy to search. Learn more

How can I get an object «key» from the value? Is there something like array_search for objects?

stdClass Object ( [id] => 1 [items] => stdClass Object ( [0] => 123 [1] => 234 [2] => 345 [3] => 456 ) ) ) 

Let’s call the above object $foo .

Let’s say $v = 234 . Given $foo and $v , how can I return the «key» 1 ?

If $foo->items was an array I would simply do $key = array_search($v,$foo->items); . But this doesn’t work in an object.

Читайте также:  Пример карты на javascript

How can I find the key for $v without looping through the object in some foreach ?

Use get_object_vars and search through the array returned.

Reference : http://php.net/manual/en/function.get-object-vars.php

Here’s an example of how to search through the array returned for a key:

id = 1; $foo->items = array(123, 234, 345, 456); $foo_array = get_object_vars($foo); print_r($foo_array); foreach ($foo_array as $key => $value) < if ($value == 1) < echo $key; >> ?> 
Array ( [id] => 1 [items] => Array ( [0] => 123 [1] => 234 [2] => 345 [3] => 456 ) ) id 

CodePad : http://codepad.org/in4w94nG

As you have shown in your example, you’re dealing with stdClass object(s). Those are quite similar to arrays and with PHP you can easily convert between those two with something called casting :

$object = $foo->items; $key = array_search($v, (array)$object); ^^^^^^^--- cast to array 

As this little example shows (I just used the $object variable to make the cast more visible, you normally can write it as one-liner), the cast from object to array does allow you to use the known function ( array_search ) on the object.

Because arrays and stdClass Objects in PHP are so similar, this works in both directions:

$array = ['property' => 'value']; $object = (object)$array; echo $object->property; # value 

This also works with other types in PHP, so probably apart from your concrete problem, something worth to read about: Type Juggling ­Docs , but take care, that in PHP this has many special rules. But between array and objects, it’s pretty straight forward.

$key = array_search($v, get_object_vars($foo->items));

Php — Get array values by keys, But in any way, if the resulting array should not display the original keys, just wrap that one liner inside a array_values() – Yanick Rochon Nov 21, …

Читайте также:  Changing font sizes in css

Accessing Object with Key as Number PHP [duplicate]

I have an object that looks like this:

stdClass Object ( [page] => stdClass Object ( [1] => stdClass Object ( [element] => stdClass Object ( [background_color] => stdClass Object . 

And when I print print_r($arr->page) :

stdClass Object ( [1] => stdClass Object ( [element] => stdClass Object ( [background_color] => stdClass Object ( 

How can I access the «1» element?

I’ve also tried $arr->page[1] and $arr->page[«1»] but get this error:

 object(stdClass)#3 (1) < [1]=>object(stdClass)#4 (1) < ["element"]=>object(stdClass)#5 (20) < ["background_color"]=>object(stdClass)#6 (7)  

From here: How can I access an object attribute that starts with a number?

You cannot access integer class variables directly. The best option is to not use stdclass at all.

If you cannot control the source of your data, you can Cast to an array via $foo = (array) $foo .

You can also iterate over the elements:

foreach (get_object_vars($obj) as $key => $val) 

PHP get object with specific key value, Stack Overflow Public questions & answers; Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers; Talent Build …

PHP Get keys with values from object

I am using simplexml_load_string() in PHP to parse my XML File.
Now, for better reading, I json_encode() d my object: http://pastebin.com/Hk8YCssR (Example Representation Plan of a german School)
I want one object with all actions in it and one with all keys of the "head".
I tried to loop through these keys, but I wasn't abled to get the value of a keys key, e.g.:

In this case, to get the value "5/2".

I don't have the exact code, but I am writing in sketch what I've done:

$value = current((array)$xml); echo $value; foreach ($xml as $key) < // echo $key[]; >

P.S.: Thank you for reading this

$sv_infch=get_object_vars($xml[0]->info); foreach($sv_infch as $key => $value) < print "$key =>$value\n"; echo " => "; print_r($sv_infch); > 

Arrays - PHP Print keys from an object?, If the 'object' is actually an associative array rather than a true object then array_keys() will give you what you need without warnings or errors.. On the …

Источник

Оцените статью