I have json data as shown in the picture below, how do I get highlighted value and change them?
Json Data
EDITED
I try to get the "furniture_id" using the code below...but fails...I don't know what else i can do to get the value
$test = '{"data":[{"owned_id":1,"furniture_id":1,"owned_name":"desk_123"},{"owned_id":2,"furniture_id":2,"owned_name":"chair_123"},{"owned_id":3,"furniture_id":4,"owned_name":"sofa_123"}]}';
foreach ($test as $key => $value) {
dd($furniture_id['id']);
}
$test = '{"data":[{"owned_id":1,"furniture_id":1,"owned_name":"desk_123"},{"owned_id":2,"furniture_id":2,"owned_name":"chair_123"},{"owned_id":3,"furniture_id":4,"owned_name":"sofa_123"}]}';
$response=json_decode($test);
foreach ($response->data as $key => $value) {
echo $value->furniture_id;
echo "<br>";
}
first you have to decode json data using json_encode method.Then if you print json decode response then you will be get an result of standard object
stdClass Object
(
[data] => Array
(
[0] => stdClass Object
(
[owned_id] => 1
[furniture_id] => 1
[owned_name] => desk_123
)
[1] => stdClass Object
(
[owned_id] => 2
[furniture_id] => 2
[owned_name] => chair_123
)
[2] => stdClass Object
(
[owned_id] => 3
[furniture_id] => 4
[owned_name] => sofa_123
)
)
)
Updated
$test = '{"data":[{"owned_id":1,"furniture_id":1,"owned_name":"desk_123"},{"owned_id":2,"furniture_id":2,"owned_name":"chair_123"},{"owned_id":3,"furniture_id":4,"owned_name":"sofa_123"}]}';
$response=json_decode($test);
echo "<pre>";
print_r($response);
$newArray=[];
foreach ($response->data as $key => $value) {
$row=[];
$value->furniture_id= $value->furniture_id+1;
}
echo "<pre>";
print_r($response->data);
Now output will be
Array
(
[0] => stdClass Object
(
[owned_id] => 1
[furniture_id] => 2
[owned_name] => desk_123
)
[1] => stdClass Object
(
[owned_id] => 2
[furniture_id] => 3
[owned_name] => chair_123
)
[2] => stdClass Object
(
[owned_id] => 3
[furniture_id] => 5
[owned_name] => sofa_123
)
)
Try this code
$test = '{"data":[{"owned_id":1,"furniture_id":1,"owned_name":"desk_123"},{"owned_id":2,"furniture_id":2,"owned_name":"chair_123"},{"owned_id":3,"furniture_id":4,"owned_name":"sofa_123"}]}';
$test = json_decode($test, true);
//if you want to change value of furniture_id in index 0
$test['data'][0]['furniture_id'] = 111;
print_r($test); exit;
$test = '{"data":[{"owned_id":1,"furniture_id":1,"owned_name":"desk_123"},{"owned_id":2,"furniture_id":2,"owned_name":"chair_123"},{"owned_id":3,"furniture_id":4,"owned_name":"sofa_123"}]}';
this $test string itself is a json string.As json is similar to javascript object, there is no way to to store json as we can do in javascript.That's why we put json inside a quote in php and represent them as string.
what you can do is turn the json string into a php object using json_decode() and access them in foreach loop
foreach (json_decode($test)->data as $key) {
echo $key->furniture_id;
}
Related
I have this array object:
//$array
Array (
[#insert_long_unique_id] =>
Array (
[0] => WP_Post Object ( [ID] => 770
) )
[#insert_long_unique_id] =>
Array (
[0] => WP_Post Object ( [ID] => 530
) )
The #insert_long_unique_id is an auto-generated ID and I dunno which method or plugin generate it but it's always different.
I need to reach and echo the [ID] => 770 (first-element) only in my project.
You can do it through array_column()
$id_array = array_column($array,'ID');
echo $id_array[0]; //print first-id
//In case if you want to print all ID's
foreach($id_array as $id_arr){
echo $id_arr;
}
If your array variable name is $array, then you can access to ID inside of object like this:
foreach ($array as $key => $value) {
if (!empty($value)) {
if(!empty($value[0]) && is_object($value[0])){
$myid = $value[0]->ID;
}
}
}
This is my json Output. i want to echo 'resolution' only. How is that possible?
Array
(
[uploader] => CoversDamian
[formats] => Array
(
[0] => Array
(
[preference] => -50
[resolution] => 720p
)
[1] => Array
(
[preference] => -100
[resolution] => 1080p
)
)
)
In the case you want to echo all resolutions you need a loop like this:
for ($i = 0; $i < sizeof($array["formats"]); $i++){
echo $array["formats"][$i]["resolution"];
}
Hope it helps!
Assuming the json is in object notation, stored as $json you can loop through the 'formats' as follows :
foreach($json->formats as $key=>$value) {
echo $value->resolution . "\n";
}
If it is not in object notation, you can loop through the sub-keys in the array (assuming it is store in the variable $json) as follows :
foreach($json['formats'] as $key=>$value) {
echo $value['resolution'] . "\n";
}
Notice the subtle difference in the way you can access sub-keys/elements in an object vs in an array.
$arr = array(
'uploader' => 'CoversDamian',
'formats' => array
(
0 => array
(
'preference' => '-50',
'resolution' => '720p'
),
1 => array
(
'preference' => -'100',
'resolution' => '1080p'
)
)
);
foreach ($arr['formats'] as $key=>$val) {
echo $val['resolution'];
}
$myarray = Array
(
[uploader] => CoversDamian
[formats] => Array
(
[0] => Array
(
[preference] => -50
[resolution] => 720p
)
[1] => Array
(
[preference] => -100
[resolution] => 1080p
)
)
)
echo $myarray["formats"][1]["resolution"];
if you have more array in formats key then you can use foreach loop based on formats key. Becuase you want to print all resolution under the formats key.
So
foreach($myarray["formats"] as $key => $value){
echo $value["resolution"]."<br>";
}
When converting xml to object, everything seems fine according to print_r($result);. But if I use $result->title it returns object instead of string and when looping $result->documents it gets really strange..
$xml = '<return>
<id>65510</id>
<title>SMART</title>
<info/>
<documents>
<name>file_1.pdf</name>
<path>http://www.domain.com/documents/file_1.pdf</path>
</documents>
<documents>
<name>file_2.pdf</name>
<path>http://www.domain.com/documents/file_2.pdf</path>
</documents>
<documents>
<name>file_3.pdf</name>
<path>http://www.domain.com/documents/file_3.pdf</path>
</documents>
</return>';
$result = simplexml_load_string($xml);
print_r($result); /* returns:
SimpleXMLElement Object
(
[id] => 65510
[title] => SMART
[info] => SimpleXMLElement Object
(
)
[documents] => Array
(
[0] => SimpleXMLElement Object
(
[name] => file_1.pdf
[path] => http://www.domain.com/documents/file_1.pdf
)
[1] => SimpleXMLElement Object
(
[name] => file_2.pdf
[path] => http://www.domain.com/documents/file_2.pdf
)
[2] => SimpleXMLElement Object
(
[name] => file_3.pdf
[path] => http://www.domain.com/documents/file_3.pdf
)
)
)
*/
$_VALUE['title'] = $result->title;
print_r($_VALUE); /* returns:
Array
(
[title] => SimpleXMLElement Object
(
[0] => SMART
)
)
*/
foreach ($result->documents as $key=>$value) {
echo $key . "<br/>";
} /* returns:
documents
documents
documents
instead of returning:
1
2
3
*/
I need $result->title to return string and $result->documents to be an array with indexes 1,2,3.
There are difference between print_r and echo in this context. Instead Print try echo
echo (string) $result->title;
It will work and output as SMART
and array
$p = 1;
foreach ($result->documents as $value) {
echo $value->name . "<br/>";
//for key
echo $p++.'</br>';
}
I have an array like this:
Array ( [0] => B121933, [1] => B105885, [1] => B105886 )
I need it in this format:
array('B121933','B105885','B105886')
I have used below code but it returns the same result:
foreach ($_finder_sku_array as $key => $value) {
$arr[] = $value;
}
print_r($arr);
Array ( [0] => B121933 [1] => B105885 [2] => B105886 )
So please suggest an idea on how I can get a proper result.
And
print_r(array('B121933','B105885','B105886')); will show you :
Array ( [0] => B121933 [1] => B105885 [2] => B105886 );
try this :
$array = array_values($array);
This function converts the passed array in the string format you require.
<?php
function arr_to_string($array){
$new_array='array("';
for($i=0;$i<sizeof($array)-1;$i++) {
$new_array.=$array[$i].'","';
}
$new_array.=$array[sizeof($array)-1].'")';
return $new_array;
}
$array=array("B121933","B105885","B105886");
echo arr_to_string($array); // Outputs array("B121933","B105885","B105886")
?>
I am working in PHP.
I have one array. i need to create language array like ([CN] => Chinese) this format.
My array response is given below.
Array
(
[0] => stdClass Object
(
[language_name] => Chinese
[language_code] => CN
)
[1] => stdClass Object
(
[language_name] => English
[language_code] => EN
)
[2] => stdClass Object
(
[language_name] => Korea
[language_code] => KO
)
[3] => stdClass Object
(
[language_name] => Vietnamese
[language_code] => VN
)
)
I need to convert this type of array
Array(
[CN]=>Chinese
[EN]=>English
[KO]=>Korea
[VN]=>Vietnamese
)
How can I do this?
Code:
<?php
$newArray = array();
foreach($yourArray as $key => $items) {
$newArray[$items->language_code] = $items->language_name;
}
die('<pre>' . print_r($newArray, true) . '</pre>');
That's it.
Use PHP's foreach :
function convert($array){
$ret = array();
foreach($array as $obj){
$ret[$obj->language_code] = $obj->language_name;
}
return $ret;
}
you could try this:
foreach($objects as $object)
{
$newArray[$object->language_code] = $object->language_name;
}
print_r($newArray);
This code is working fine
foreach($objects as $object)
{
$newArray[$object->language_code] = $object->language_name;
}
echo "<pre>";
print_r($newArray);
echo "</pre>";