Reputation: 6891
{
"data": [
{
"business_type": "bar",
"business_name": "KKK",
"number": "1234567",
"business_id": "test1"
},
{
"business_type": "restauratns",
"business_name": "KKKK",
"number": "1234567",
"business_id": "test2"
}
]
}
PHP Code:-
1) Not Worked
foreach ($json_array->data as $obj) {
echo $obj->business_name;
echo $obj->business_type;
echo $obj->business_id;
}
2) Only get the output "data Array" but didn't get the object details
foreach ($json_array as $name=>$value) {
echo "$name: $value\n";
foreach ($value as $entry) {
echo ' ' . $entry->business_type;
echo ' ' . $entry->business_name;
echo ' ' . $entry->business_id;
echo ' ' . $entry->number;
}
}
after this I also try to print details with print_r and var_dump.
Result of var_dump is :-
array(1) {
["data"]=>
array(2) {
[0]=>
array(4) {
["business_type"]=>
string(3) "bar"
["business_name"]=>
string(3) "KKK"
["number"]=>
string(7) "1234567"
["business_id"]=>
string(5) "test1"
}
[1]=>
array(4) {
["business_type"]=>
string(11) "restauratns"
["business_name"]=>
string(4) "KKKK"
["number"]=>
string(7) "1234567"
["business_id"]=>
string(5) "test2"
}
}
}
I am trying to find where I am making mistake but didn't get any idea. Please kindly guide me if anyone has any idea.
Upvotes: 0
Views: 257
Reputation: 1
array.php
<?php
// $json = json_decode($json_data, true);
$json = array(
'data' => array(
array(
'bussiness_type' => 'bar',
'bussiness_name' => 'KKK',
'number' => '1234567',
'bussiness_id' => 'test1'
),
array(
'bussiness_type' => 'restauratns',
'bussiness_name' => 'KKKK',
'number' => '1234567',
'bussiness_id' => 'test2'
)
)
);
/*
array(1) {
["data"]=>
array(2) {
[0]=>
array(4) {
["bussiness_type"]=>
string(3) "bar"
["bussiness_name"]=>
string(3) "KKK"
["number"]=>
string(7) "1234567"
["bussiness_id"]=>
string(5) "test1"
}
[1]=>
array(4) {
["bussiness_type"]=>
string(11) "restauratns"
["bussiness_name"]=>
string(4) "KKKK"
["number"]=>
string(7) "1234567"
["bussiness_id"]=>
string(5) "test2"
}
}
}
*/
foreach( $json['data'] as $value )
{
echo $value['bussiness_type'] . "\n"
. $value['bussiness_name'] . "\n"
. $value['number'] . "\n"
. $value['bussiness_id'] . "\n\n";
}
?>
object.php
<?php
// $json = json_decode($json_data, true);
$json = array(
'data' => array(
array(
'bussiness_type' => 'bar',
'bussiness_name' => 'KKK',
'number' => '1234567',
'bussiness_id' => 'test1'
),
array(
'bussiness_type' => 'restauratns',
'bussiness_name' => 'KKKK',
'number' => '1234567',
'bussiness_id' => 'test2'
)
)
);
$to_json = json_encode($json);
$to_obj = json_decode($to_json);
/*
object(stdClass)#1 (1) {
["data"]=>
array(2) {
[0]=>
object(stdClass)#2 (4) {
["bussiness_type"]=>
string(3) "bar"
["bussiness_name"]=>
string(3) "KKK"
["number"]=>
string(7) "1234567"
["bussiness_id"]=>
string(5) "test1"
}
[1]=>
object(stdClass)#3 (4) {
["bussiness_type"]=>
string(11) "restauratns"
["bussiness_name"]=>
string(4) "KKKK"
["number"]=>
string(7) "1234567"
["bussiness_id"]=>
string(5) "test2"
}
}
}
*/
foreach( $to_obj->data as $value )
{
echo $value->bussiness_type . "\n"
. $value->bussiness_name . "\n"
. $value->number . "\n"
. $value->bussiness_id . "\n\n";
}
?>
Upvotes: 0
Reputation: 522024
You're very apparently decoding the JSON data as array, not as object. json_decode(..., true)
returns arrays, if you leave out the true
parameter it returns objects. Either change your code to expect arrays or change your decoding to return objects.
Upvotes: 2