I wrote a function wheere I need to store some data in database.
I have multidimensional associative array.
I dumped one part of the code..
When I dump:
$csv->setCode($data[7]);
dump($data[7]);die;
and I get
"{"id":"131-2-0-615317","o":"raccoon-events-ltd","j":"Member of the...","c":"Sport For All..."}"
Now, I need to pull out just the "ID" part of the code..
I just tried:
dump($data[7]=>id);die;
Its JSON
use this code:
$json = json_decode($data[7],true);
dump($json['id']);die;
Related
I have a field created by word press which contains data with the following structure.
a:16:{s:7:"country"
s:14:"United Kingdom"
s:7:"form_id"
s:2:"35"
s:9:"timestamp"
s:10:"1560869327"
s:7:"request"
s:0:""
s:8:"_wpnonce"
s:10:"125"
s:16:"_wp_http_referer"
s:1:"/"
s:17:"ajaxy-umajax-mode"
s:8:"register"
s:10:"first_name"
s:5:"xxxxx"
s:9:"last_name"
s:5:"xxx"
s:10:"user_email"
s:28:"xxx#xxx.co.uk"
s:7:"Company"
s:16:"xxx LTD"
s:12:"phone_number"
s:10:"0123456789"
s:8:"user_url"
s:20:"http://www.test.com"
s:15:"company_address"
s:18:"999 LockSmith Lane"
s:12:"display_name"
s:12:"XXXX"
s:10:"user_login"
s:10:"xxx123"
}
I want to convert this to an array so I can read the properties of it.
I tried converting it to json but its not json.
Any ideas on how I can parse this data, or access its properties in PHP.
I can not access this data through wordpress as my PHP script is part of something else.
Seems like a serialized array. Try unserializing it to convert it back to normal, see example.
This is Actually not a json.This is an array in serialized format you can just unserilize it by using this function
maybe_unserialize($YOUR_ARRAY).
maybe_unserialize is a wordpress default function to unserialize the array
So I have following link which contains a lot of data (a lot). http://www.gw2spidy.com/api/v0.9/json/all-items/all
The data is a list of ~50,000 sets of data, separated by commas.
Is there anyway I can convert this some 50,000 entries of data to an array?
Here is one entry of data for reference.
{
"data_id":2,
"name":"Assassin Pill",
"rarity":1,
"restriction_level":0,
"img":"https:\/\/render.guildwars2.com\/file\/ED903431B97968C79AEC7FB21535FC015DBB0BBA\/60981.png",
"type_id":3,
"sub_type_id":1,
"price_last_changed":"2014-05-10 20:00:13 UTC",
"max_offer_unit_price":0,
"min_sale_unit_price":0,
"offer_availability":0,
"sale_availability":0,
"sale_price_change_last_hour":0,
"offer_price_change_last_hour":0
}
It is JSON data, Use
$yourarray = json_decode(file_get_contents('http://www.gw2spidy.com/api/v0.9/json/all-items/all'),true);
to convert them into an array.
I want use zachhunters json to html tabel script with my postressql db so i need a specific JSON string format but i dont know how to build it ... (i am new in json)
Link to the Script
I get my data using
$result = pg_fetch_array($rs, NULL, PGSQL_ASSOC);
When i use json_encode($result) i get this:
{"id":"2","surname":"Max","name":"Muster"} etc.
For the Script i need somthing like:
{ "d": "[{\"id\":1,\"Username\":\"Sam Smith\"},{\"id\":2,\"UserName\":\"Fred Frankly\"}]" }
How can i handle this?
In each loop where you fetch an element from your database store the $result in an array $rows.
After the loop simply use json_encode(array('d' => $rows)) to create JSON containing all your data.
I have a problem with the json functionality in zend and js.
I try to encode a single array containing some number of models like this:
echo json_encode(Application_Model_Marker::getMarkers());
var mark = JSON.parse(jsonVal); //in js
where getMarkers is a static method that returns an array of marker models.
This works fine and when I parse it in the js script and try accessing the values of the json object it works fine.
If however I try to create and send an array of array like this:
$allData = array();
$allData['info'] = Application_Model_Marker::getMarkers();
$allData['openingHours'] = Application_Model_Openinghours::getOpeningHours();
$allData['happyHours'] = Application_Model_Happyhour::getHappyHours();
echo json_encode($allData);
It still sends all the correct information when I try to alert(jsonVal.responseText); in js.
It has three arrays each containg some arrays of objects.
However when I try to initialize a variable to the parsed json object like in the first example, I can't access the values and it seems some kind of error occurs as the program stops when I try it.
I don't quite get it as it has all the correct info when i just try to print the response text from the encoded json object.
Any ideas how to do this multidimensional json encoding?
Try this, Hopefully it'll work :
<sctript>
var mark;
eval("mark = "+jsonVal+";");
</sctrip>
IP.Nexus puts custom fields into a table all muddled up into one row and some how pulls the correct data out.
basically my custom fields row looks like so:
a:2:{i:2;s:4:"Test";i:3;s:10:"Accounting";}
How do i select what item to pull as i would like to out put just one item not the whole row, is it with delimiters or string splitter, as i am novice with this part of mySQL
Cause i would like to echo out just the advertising on its own and also vice versa for the testing.
Thanks in advance
It's a serialized array using serialize() function so you can get its contents using unserialize($YourString);.
But I see that you string is corrupted for deserialization, because s:4: in s:4:"advertising" says that after s:4: 4 character long string is expected that advertising is not.
The data in the database is in serialized(json) form.You can turn it into simple array using unserialize.
For example;if $mystr is your string
write
$mystring=unserialize($mystring)
You will get normal array of this string
You can do
foreach($mystring as $str){
$ads[]=$str['advertising'];
}
and the resulting array $ads will be the ads you like in simple array form
Since this looks as JSon, you can use a PHP JSon module to parse each row from MySQL.
<?php
(...)
while (...) {
$obj = json_decode($row);
// do something with $obj
}