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.
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 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"
}
I get a returned result from an API in stdClass format. I don't know anything about this type of data. So here it looks like:
object(stdClass)#41 (1) {
["return"]=>
object(stdClass)#42 (7) {
["afterPayOrderReference"]=> string(32) "d4ab78df6ab2ef84194dd1c1d66240b8"
["checksum"]=> string(32) "4f8826a99e9c0a67e578d04b6a625117"
["resultId"]=> int(0)
["statusCode"]=> string(1) "A"
["timestampIn"]=> float(1408533108515)
["timestampOut"]=> float(1408533113616)
["transactionId"]=> int(129525)
}
}
What I need is retrieving the statusCode value. I tried doing like in a post I read:
$array = (array) $stringResult;
$array[0]->statusCode;
But it didn't work. Please, someone explain to me in the simplest way because it's really new to me. Thanks.
Object properties are accessed with the -> operator. Just do:
echo $stringResult->return->statusCode;
If you wanted an array you would access like this since the array contains an object:
$array = (array)$stringResult;
echo $array['return']->statusCode;
Its an object array, so just as you call the array element,
echo $stringResult["return"]->statusCode
I am a beginner in php rogramming and I try to get some information per PHP out of an JSON string.
Therefore I used:
$json = file_get_contents('data.json')
var_dump(json_decode($json));
to get more information how php will parse my JSON file. Output of it looks like:
array(10) {
[0]=> object(stdClass)#1 (1) {
["links"]=> array(4) {
[0]=> object(stdClass)#2 (6) {
["localIP"]=> string(14) "172.29.126.189"
["remoteIP"]=> string(14) "172.29.118.193"
["validityTime"]=> int(586277)
["linkQuality"]=> float(1)
["neighborLinkQuality"]=> float(0.396)
["linkCost"]=> float(2.524) }
[1]=> object(stdClass)#3 (6) {
["localIP"]=> string(14) "172.29.126.189"
["remoteIP"]=> string(14) "172.29.149.193"
["validityTime"]=> int(551339)
["linkQuality"]=> float(1)
["neighborLinkQuality"]=> float(0.396)
["linkCost"]=> float(2.524) } } } }
...
I want to know how I would be able to display for example:
the "remoteIP" Attribute of Links[0]
all Attributes of Links[0]
Thank you for your suggestions
Whenever you have an array, you use square brackets to access any index of it. Whenever you have an object, you use the -> operator to access a property of it.
So for your examples, it would be:
$json = file_get_contents('data.json');
$data = json_decode($json);
var_dump($data[0]->links[0]->remoteIP); // remoteIP of links[0]
var_dump($data[0]->links[0]); // All attributes of links[0], as object
Another way would be (as Leo Bali pointed out), to let PHP convert all objects to arrays. In that case, you always use square brackets:
$json = file_get_contents('data.json');
$data = json_decode($json, true); // Add true here as second parameter
var_dump($data[0]['links'][0]['remoteIP']); // remoteIP of links[0]
var_dump($data[0]['links'][0]); // All attributes of links[0], as array