parse a json array inside a json array in php - php

I am having a json object $result
I do this:
$json = json_decode($result, true);
Here is the output if I use this:
var_dump($json)
is this:
array(15) { ["id"]=> int(1) ["name"]=> array(16) { ... } }
If I do this:
echo $json['id'];
echo $json['name'];
The id is printed correctly : 1
But in the name this is printed: Array
How can I get that array and print it?

Several ways:
print_r($json['name']);
var_dump($json['name']);
Or manual with preferred delimiter:
echo implode(", ", $json['name']);
However you should check the function responsible for making that JSON string, because you expect a string instead of array.

Related

Json_decode gives me an string, how can I extract values from it?

I can't get values from my JSON DECODED STRING
This is what I have tried
$data = json_decode(stripslashes($_POST['data']),true);
$data = TYPE --> String
Gives me:
"[{"name":"option_page","value":"gpinator_plugin_options"},
{"name":"action","value":"update"},
{"name":"_wpnonce","value":"b0b29d4682"},
{"name":"_wp_http_referer","value":"/wordpress/wordpress-4.9.1/wordpress/wp-admin/admin.php?page=gpinator_settings_admin_page"},
{"name":"gpinator_category_allowed","value":"on"},
{"name":"gpinator_languages_swedish","value":"on"}]"
$_POST['data'] Gives me
"\"{\\\"name\\\":\\\"option_page\\\",\\\"value\\\":\\\"gpinator_plugin_options\\\"}
,{\\\"name\\\":\\\"action\\\",\\\"value\\\":\\\"update\\\"},
{\\\"name\\\":\\\"_wpnonce\\\",\\\"value\\\":\\\"b0b29d4682\\\"}, {\\\"name\\\":\\\"_wp_http_referer\\\",\\\"value\\\":\\\"/wordpress/wordpress-
4.9.1/wordpress/wp-admin/admin.php?page=gpinator_settings_admin_page\\\"},
{\\\"name\\\":\\\"gpinator_category_allowed\\\",\\\"value\\\":\\\"on\\\"},{\\\"name\\\":\\\"gpinator_languages_swedish\\\",\\\"value\\\":\\\"on\\\"}]\""
No I have tried in all ways to extract the data.
If I do
$obj = $data[0];
I only get
"["
I run these functions
$josnerr = json_last_error(); result --> 0
$josnerree = json_last_error_msg(); result "No error"
When doing a print_r($data);
it gives me
[{"name":"option_page","value":"gpinator_plugin_options"},
{"name":"action","value":"update"},
{"name":"_wpnonce","value":"b0b29d4682"},
{"name":"_wp_http_referer","value":"/wordpress/wordpress-4.9.1/wordpress/wp-admin/admin.php?page=gpinator_settings_admin_page"},
{"name":"gpinator_category_allowed","value":"on"},
{"name":"gpinator_languages_swedish","value":"on"}]
How can I access my data?
I think your json object is a string representation of an object. Like if you json_encode a structure twice you first get a json_string, then you get a json encoded string of that string. Example: Imagine you pass this to json_decode:
var_dump(json_decode('[{"a": "b"}, {"b": "a"}]', true))
// ==>
array(2) {
[0] =>
array(1) {
'a' =>
string(1) "b"
}
[1] =>
array(1) {
'b' =>
string(1) "a"
}
}
While a small change like this:
var_dump(json_decode('"[{\"a\": \"b\"}, {\"b\": \"a\"}]"', true))
// ==>
string(24) "[{"a": "b"}, {"b": "a"}]"
Perhaps you should look at the data before you pass it to json_decode? Anyways if it is so then it's the client side the problem resides.
UPDATE
Looking at your post data you have supplied it is indeed what I imagined. You need to fix this on the client side. If you have no choice but to fix it on your end you can doubly decode the value:
$strJson = json_decode('"[{\"a\": \"b\"}, {\"b\": \"a\"}]"');
$arrPayload = json_decode($strJson, true);
var_dump($arrPayload);
// ==>
array(2) {
[0] =>
array(1) {
'a' =>
string(1) "b"
}
[1] =>
array(1) {
'b' =>
string(1) "a"
}
}

Name of array in result of json_decode() [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 4 years ago.
If I do a json_decode like this:
$json = json_decode($jsondata);
echo var_dump($json);
I get something like this:
object(stdClass)#1 (1)
{ ["QRY_JISGesch"]=> array(3)
{ [0]=> object(stdClass)#2 (8)
{ ["JISGeschID"]=> int(7) ["StandorteID"]=> int(0) ["FSKID"]=> int(23)
}
[1]=> object(stdClass)#3 (8)
{ ["JISGeschID"]=> int(8) ["StandorteID"]=> int(0) ["FSKID"]=> int(22)
}
[2]=> object(stdClass)#4 (8)
{ ["JISGeschID"]=> int(9) ["StandorteID"]=> int(0) ["FSKID"]=> int(1)
}
}
}
How do I find out "QRY_JISGesch" in code?
You could use reset() to get the first member of an object (or an array).
$json = json_decode($jsondata);
$first = reset($json);
If you only want the get the first "key", you can use key();
$json = json_decode($jsondata);
$key = key($json); // QRY_JISGesch
Try this. It will help you :-
$json = json_decode($jsondata, true);
// true means objects will be converted into associative arrays
and you can access QRY_JISGesch like this way :- $json['QRY_JISGesch']
Is there only one item given in the array, and you are not interested in the name itself, but the values? Then array_values could help.
Otherwise, you could use array_keys to read all keys and iterate over them? Or use a foreach loop which does not care about the array key neither.
get_object_vars (object $object) will give you a list of all accessible properties of the returned object.

in php, How can I extract a JSON from an array?

I'm receiving a string from the database and I only want to use the JSON so I can use a json_decode on it.
this is the string:
array(1) { [0]=> array(4) { ["reporte_id"]=> int(1) ["tabla_id"]=> int(1) ["configuracion"]=> string(1000) "{"tabla":"servtp","config":[{"name":"Client","type":"smallint","notnull":"0","pk":"0"},{"name":"Distribuidor_","type":"varchar(40)","notnull":"0","pk":"0"},{"name":"Branch","type":"smallint","notnull":"0","pk":"0"},{"name":"Cve","type":"smallint","notnull":"0","pk":"0"},{"name":"FechaApertura","type":"date","notnull":"0","pk":"0"},{"name":"FechaFactura","type":"date","notnull":"0","pk":"0"},{"name":"Dias","type":"smallint","notnull":"0","pk":"0"},{"name":"WorkingDays","type":"real","notnull":"0","pk":"0"},{"name":"Mes_","type":"smallint","notnull":"0","pk":"0"},{"name":"NumeroOT","type":"varchar(10)","notnull":"0","pk":"0"},{"name":"VentasNetas_","type":"real","notnull":"0","pk":"0"},{"name":"TipoOrden","type":"varchar(30)","notnull":"0","pk":"0"},{"name":"Type","type":"varchar(3)","notnull":"0","pk":"0"},{"name":"Taller","type":"varchar(30)","notnull":"0","pk":"0"},{"name":"Clasificacion_","type":"varchar(16)","notnull":"0","pk":"0"},{"name":"Retencion_","type":"varchar(1)","notnull":" ["nom_reporte"]=> string(33) "Monthly Service Operation Report " } }
If the variable is called $variable, ... you have to:
$json = $archivio[0]['configuration'];
$arrayConf = json_decode($json);
The string you are presenting is a var_dump of a php array, it is not properly formed. Where you are getting that string from should fix their side to properly format the array formatting.

Object type array elements php

I stored some values in db using json_encode.Now on fetching i got values like this ["ab","cd"].I have tried by exploding,json_encode and then decode.But nothing works.some of tried code is below
$array = "["ab","cd"]";
$value = (array)$array;
//-------------
$array = (array) $array;
// get_object_vars
$array = get_object_vars($object);
print_r($array);
when i loop directly on array i didn't get any values.Thanks for any help in advance.
on this i got like this :
var_dump(json_decode($object));
print_r($object);
OUTPUT :
NULL ["MKD","KD3"]
If I am understanding your question, I think you are looking for json_decode.
$json_encoded_str = '["ab","cd"]';
// Will return an array of elements in your string
var_dump(json_decode($json_encoded_str));
The result would be
array(2) {
[0]=> string(2) "ab"
[1]=> string(2) "cd"
}

How can two arrays created in the same way become different?

The var_dumps of the two arrays are different but they were created the exact same way . Why is this?????
first array dump:
array(2) {
[0]=>
array(0) {
}
[1]=>
string(6) ",2,2,1"
}
second array dump:
array(3) {
[0]=>
array(0) {
}
[1]=>
string(1) "2"
[2]=>
string(1) "2"
[3]=>
string(1) "1"
}
array 1 is made on fileA.php
while ($row = mysql_fetch_assoc($res))
{
$iState[] = $row["state"];
}///end while
then i use ajax to send to fileB.php
js_array=<? echo json_encode($iState); ?>;
var url_js_array=js_array.join(',');
xmlhttp.open("GET","fileB.php?istate="+ url_js_array,true);
xmlhttp.send();
then in fileB.php(ajax response file) i retrieve the array
$iStateValues[] =$_GET["istate"] ;
then I create array 2 on fileB.php
while ($row = mysql_fetch_array($res))
{
$currentiState[]= $row["state"];
}///end while
then I compred the two
echo"\n\nsame test\n";
if($iStateValues==$currentiState)
echo "same";
else
echo "not same";
The problem:
Currently, you're sending the comma-separated string to fileB.php.
$iStateValues[] = $_GET["istate"] ;
$_GET["istate"] contains the comma-separated string, and in the above statement, you're pushing the value into an empty array $iStateValues.
Now, in fileB.php, you create another array $currentiState using a while loop. Then you try to compare them.
$iStateValues is a string, not an array. $currentiState is a real array. So the comparison will always return FALSE.
How to fix the issue:
Instead of sending the comma-separated string to fileB.php, send the actual JSON string. So your code will look like:
js_str=<? echo json_encode($iState); ?>;
xmlhttp.open("GET","fileB.php?istate="+ js_str,true);
xmlhttp.send();
Now, in fileB.php, you can receive the JSON string and then decode it, like so:
$jsonArray = json_decode($_GET['istate'], true);
(The second parameter in JSON decode says it should be decoded to an associative array instead of an object).
Then do the comparison.

Categories