I have the following array I want to get the element one by one.
"attachment":"["Apple","car","home","scooter"]"
If the array is in JSON form you do json_decode and the iterate over the array
Related
So basically I have this array stored in SQL and I'm trying to retrieve the first element from it using PHP
I'm not sure if this is an associative or multidimensional array
a:3:{i:0;s:11:"Downpayment";i:1;s:28:"Variable 1 ";i:2;s:28:"Variable 2";}
How do I extract elements from this array ?
This is a PHP representation of serialized data. Use unserialize() on it.
If you not sure in this array you can use current() function or foreach to get first element of this array
For set inner pointer on first element use reset()
I am making a call to an API which should return a JSON array. I receive this:
Array
(
[0] => {"msg_id":"0t5OxT1ZP9VcF45L2","_text":"I want to reserve","entities":{"intent":[{"confidence":1,"value":"make_reservation","type":"value"}]}}
)
I know it's JSON because a) the docs say that's what I should be getting and b) I ran isJson($response) and got true.
I have tried to use json_decodebut the code just dies when I do (it errs saying it's expecting a string and got an array which makes sense but if I do json_encode that would just further encode the json from what I can understand).
As I understand it, I just need a way to traverse this array and get the "value:" key inside entities: intent:. However I can't figure out how to get it or where I'm wrong.
I have tried doing:
$val = $jsonArray[0]['entitites']['intent'][0]['value'] but nothing comes out.
You are trying to decode a PHP array that has encoded values.
You should try json_decode($jsonArray[0]) instead, so that you decode the value of the first array key, as that is the actual json string.
The data you posted is a php array where the value of the first element of the array is a json string.
json_decode($response[0]);
I'm trying to push some data into a multidimensional array, but can't find the correct syntax anywhere.
array_push($price, $row["ShopID"], $row["URL"]);
This is my code so far, but this does not make the array multidimensional, it just inserts each item as an individual row in the array. How can I fix this, and make the array multidimensional?
Are you pushing an array? Then:
array_push($price, array($row["ShopID"], $row["URL"]));
or maybe:
array_push($price, $row);
I have an xpath query on a simplexml element like so:
$theSimpleXmlObject->xpath('//path/to/*/node');
it returns an array of results which is great but it is an array of simplexml objects and i just want a flat array.
Is this possible?
Ideally i would have a simple array returned with the matching node values instead of having to iterate over the results to prepare the array.
Try in this way to get Array
$xml = json_decode(json_encode((array) simplexml_load_string($string)), 1);
When you cast xml to array it will cast only "first level", so we can made it by convert it in JavaScript Object Notation and decode it again to array. Serialization will not help, becouse it keep data-structure.
I have only worked with JSON once before, but I don't recall how to use it with PHP.
If I have a script that returns JSON like this:
{
"bunisess":[
"business",
"bonuses",
"burnooses",
"boniness",
"burnoose's"
]
}
how can I take that and make each value a value in a PHP array. The keys just being numbers from 0 onwards?
Use json_decode, but pass true as the second parameter to get an associative array back:
$json='{"bunisess":["business","bonuses","burnooses","boniness","burnoose\'s"]}';
$data=json_decode($json, true);
Now, you can use array_values to get a numerically indexed array as you required:
$data=array_values($data);
A simple google search could have lead you to this page : http://php.net/manual/fr/function.json-decode.php
The subarray will be respecting exatcly what you're asking for.
Assuming that $data is the complete JSON string
$stdObject = json_decode($data, true);
print_r($array);
You should get an array with one key bunisess and value another numeric array with the other values.