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.
Related
I have the below formatted code from database.
a:9:{i:8053;a:26:{s:4:"name";s:0:"";s:12:"squareseaten";s:0:"";s:4:"wins";s:0:"";s:9:"timetaken";s:0:"";s:10:"tubeseaten";s:0:"";s:12:"gummieseaten";s:0:"";s:12:"pepperseaten";s:0:"";s:10:"chipseaten";s:0:"";s:13:"bottlechugged";s:1:"1";s:17:"boxesofbiteseaten";s:0:"";s:19:"boxesofbrutalseaten";s:0:"";s:14:"crunchieseaten";s:0:"";s:17:"boxesofdncvseaten";s:0:"";s:15:"bottlesconsumed";s:0:"";s:12:"pieceschewed";s:0:"";s:14:"additionalnuts";s:1:"1";s:15:"additionaltimes";s:0:"";s:18:"additionalattempts";s:0:"";s:3:"pts";s:0:"";s:1:"p";s:0:"";s:1:"w";s:0:""; ...
This looks like JSON. But it's not actually.
This was actually generated by a WordPress plugin called wpDataTables.
How can I parse through this data in PHP?
That's a serialized string. Run that string through unserialize() and you'll get an array.
This is a serialized string you need to use unserialize()
Ex:
<?php
$data = 'a:3:{i:0;s:3:"Red";i:1;s:5:"Green";i:2;s:4:"Blue";}';
$test = unserialize($data);
var_dump($test);
?>
Output
array(3) { [0]=> string(3) "Red" [1]=> string(5) "Green" [2]=> string(4) "Blue" }
The unserialize() function converts serialized data into array.
Syntax:
unserialize(string, options);
string(Required): Specifies the serialized string.
options : Specifies options to be provided to the function, as an associative array. Can be either an array of class names which should be accepted, false to accept no classes, or true to accept all classes. True is default.
im trying to store data from a json file to php variables. But it wont work when i try to echo the variable for later use.
This is my json file:
"[{\"channelId\":150342342,\"channelName\":\"example.no\",\"channelUrl\":\"http:\/\/www.example.no\"},{\"channelId\":1529564329,\"channelName\":\"example2.no\",\"channelUrl\":\"http:\/\/www.example2.no\"},{\"channelId\":1536534822,\"channelName\":\"example3.no\",\"channelUrl\":\"http:\/\/www.example3.no\"}]"
This is my code:
<?php
require_once('config.php');
$channeljson = file_get_contents('channels.json');
$data = json_decode($channeljson, true);
$channelid = $data['channelId'];
$channelname = $data['channelName'];
$channelurl = $data['channelUrl'];
I have tried many other ways too..
Anyone have any tips that could work?
Your file contains a json that represents a string, since it starts and finishes with the " character. Another issue are the escape characters \.
If you don't want to change de routine that writes the json files, you can solve your problem by correcting the invalid json:
$data = json_decode(trim(stripslashes($channeljson), '"'), true); // Removes the escape and the enclosing " characters
There is one more problem: the json represents an array of objects (arrays since you are passing true to the second json_decode function) and you are trying to access $data like a regular array.
See the $data contents:
array(3) {
[0]=>
array(3) {
["channelId"]=>
int(150342342)
["channelName"]=>
string(10) "example.no"
["channelUrl"]=>
string(21) "http://www.example.no"
}
[1]=>
array(3) {
["channelId"]=>
int(1529564329)
["channelName"]=>
string(11) "example2.no"
["channelUrl"]=>
string(22) "http://www.example2.no"
}
[2]=>
array(3) {
["channelId"]=>
int(1536534822)
["channelName"]=>
string(11) "example3.no"
["channelUrl"]=>
string(22) "http://www.example3.no"
}
}
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.
I have an api wrapper i am using that returns something like this
object(stdClass)#7 (1) {
[0]=>
object(stdClass)#6 (2) {
["contactId"]=>
string(2) "nV"
["email"]=>
string(31) "email#domain.com"
}
}
how do i access the email part with PHP
Cast your API returned data to an array.
For example you are saving API returned data in $result variable. Cast it to an array.
$arrayResult = (array) $result;
echo $arrayResult[0]->email;
Try this.
I am trying to go from a cur request to a page with some info.
I have the curl working but I have trouble going from the decode json to individual php variables. the conten after json_decode is:
object(stdClass)#1 (2) { ["response"]=> object(stdClass)#2 (2) { ["request"]=> string(20) "mailboxes/status/get" ["result"]=> string(1) "0" } ["status"]=> string(7) "success" }
I need the value of result which is 0 here.
Thanks in advance.
$result = $data->response->result;
Assuming the variable $data is where you stored your json_decode. It returns an instance of stdClass, and viewing the vardump, you can see the structure and get the data you want.