PHP turn data list into an array - php

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.

Related

Store php generated JSON string in a CSV file

I am trying to generate a JSON string using php from an array, and I would like to store that string in a csv file, also with PHP. I want to do this, because I am working with a quite large amount of data, and I would like to use MySQL's LOAD DATA LOCAL INFILE to populate and update my database table.
This is the code I have:
$tmpFileProducts = 'path/to/file';
$tmpFileProductsHandler= fopen($tmpFileProducts, 'w');
foreach ($attributeBatch as $productId => $batch) {
fputcsv($tmpFileProductsHandler, array($productId, $batch['title'], $batch['parsed'], json_encode($batch['attributes'])), "|", "\"");
}
My problem is, that when I am creating the CSV file, the JSON double-quotes are not escaped, thus I end up with simmilar lines in my csv file:
43541|"telefon mobil 3l 2020 4g "|"2020-12-05 17:38:19"|"{""color"":""dark chrome"",""memory_value"":4294967296,""storage_value"":68719476736,""sim_slot"":""dual""}"
My first possible solution would be, to change the string enclosure of my CSV file, but what enclosure should I use, to ensure no conflicts could arrise with the inner json column? I am in complete control of the array that is stringified, it will only contain ASCII characters, in it.
Would there be a way, to keep the current string enclosure, and instead escape the JSON string somehow? Later on, I will need to fetch the data that is included to the database, and convert it to an array again.
DISCLAIMER: I am well aware that instead of storing the data as a JSON string, I could store it in a specific relational table (which I am also doing), but I would need quick access to this data, for a background script that is running, and I would like to save on the time of the queries, to the relational table, as when the background script will use this data, it doesn't need to search in it.
Follow up question: as I am explicitly telling the fputcsv function what to use as string enclosure shouldn't it automatically escape all the simmilar inner strings?

PHP: Converting this data structure into an associative array

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

Pull specific data from PHP array

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;

is this json and how to replace values with php variables?

I have different php variables containing data that I want to put in one of the table in mysql database. The problem is I need to put the content in a specific format so another linked to the database can also use it.
I don't know what type of format is this. It doesn't look like JSON
a:1:{i:0;s:27:"my#example.com";}
a:17:{i:0;a:7:{s:2:"id";s:1:"1";s:4:"slug";s:19:"contact-information";s:4:"name";s:19:"Contact Information";s:4:"type";s:8:"fieldset";s:7:"options";s:0:"";s:9:"parent_id";s:1:"0";s:5:"value";s:0:"";}i:1;a:7:{s:2:"id";s:1:"6";s:4:"slug";s:12:"company-name";s:4:"name";s:12:"Company Name";s:4:"type";s:4:"text";s:7:"options";s:0:"";s:9:"parent_id";s:1:"0";s:5:"value";s:20:"Company Name is here";}i:2;a:7:{s:2:"id";s:1:"8";s:4:"slug";s:12:"contact-name";s:4:"name";s:12:"Contact Name";s:4:"type";s:4:"text";s:7:"options";s:0:"";s:9:"parent_id";s:1:"0";s:5:"value";s:8:"Name";}i:3;a:7:{s:2:"id";s:2:"10";s:4:"slug";s:13:"email-address";s:4:"name";s:13:"Email Address";s:4:"type";s:5:"email";s:7:"options";s:0:"";s:9:"parent_id";s:1:"0";s:5:"value";s:26:"example#gmail.com";}i:4;a:7:{s:2:"id";s:2:"12";s:4:"slug";s:15:"website-address";s:4:"name";s:15:"Website Address";s:4:"type";s:3:"url";s:7:"options";s:0:"";s:9:"parent_id";s:1:"0";s:5:"value";s:25:"http://example.com";}i:5;a:7:{s:2:"id";s:2:"15";s:4:"slug";s:13:"contact-phone";s:4:"name";s:13:"Contact Phone";s:4:"type";s:5:"phone";s:7:"options";s:0:"";s:9:"parent_id";s:1:"0";s:5:"value";s:12:"923246573632";}i:6;a:7:{s:2:"id";s:2:"16";s:4:"slug";s:12:"your-address";s:4:"name";s:12:"Your Address";s:4:"type";s:7:"address";s:7:"options";s:0:"";s:9:"parent_id";s:1:"0";s:5:"value";s:91:"House # 2, Street 27<br>Chah Mrain<br>Lahore, Punjab 54000<br>New Zealand";}i:7;a:7:{s:2:"id";s:2:"27";s:4:"slug";s:19:"listing-information";s:4:"name";s:19:"Listing Information";s:4:"type";s:8:"fieldset";s:7:"options";s:0:"";s:9:"parent_id";s:1:"0";s:5:"value";s:0:"";}i:8;a:7:{s:2:"id";s:2:"17";s:4:"slug";s:13:"listing-title";s:4:"name";s:13:"Listing Title";s:4:"type";s:4:"text";s:7:"options";s:0:"";s:9:"parent_id";s:1:"0";s:5:"value";s:17:"House of the Dead";}i:9;a:7:{s:2:"id";s:2:"19";s:4:"slug";s:12:"listing-type";s:4:"name";s:12:"Listing Type";s:4:"type";s:6:"select";s:7:"options";s:90:"a:4:{i:0;s:11:"Attractions";i:1;s:13:"Accommodation";i:2;s:5:"Tours";i:3;s:9:"Transport";}";s:9:"parent_id";s:1:"0";s:5:"value";s:13:"Accommodation";}i:10;a:7:{s:2:"id";s:2:"20";s:4:"slug";s:17:"listings-overview";s:4:"name";s:19:"Listing\'s Overview";s:4:"type";s:8:"textarea";s:7:"options";s:0:"";s:9:"parent_id";s:1:"0";s:5:"value";s:55:"Here is the overview of listing lets check what happens";}i:11;a:7:{s:2:"id";s:2:"21";s:4:"slug";s:10:"logo-image";s:4:"name";s:10:"Logo Image";s:4:"type";s:11:"file-upload";s:7:"options";s:31:"a:1:{i:0;s:13:"png|jpe?g|gif";}";s:9:"parent_id";s:1:"0";s:5:"value";s:80:"http://example.com/connect-icon.png";}i:12;a:7:{s:2:"id";s:2:"25";s:4:"slug";s:9:"longitude";s:4:"name";s:9:"Longitude";s:4:"type";s:4:"text";s:7:"options";s:0:"";s:9:"parent_id";s:1:"0";s:5:"value";s:0:"";}i:13;a:7:{s:2:"id";s:2:"26";s:4:"slug";s:8:"latitude";s:4:"name";s:8:"Latitude";s:4:"type";s:4:"text";s:7:"options";s:0:"";s:9:"parent_id";s:1:"0";s:5:"value";s:0:"";}i:14;a:7:{s:2:"id";s:1:"2";s:4:"slug";s:12:"verification";s:4:"name";s:12:"Verification";s:4:"type";s:12:"verification";s:7:"options";s:0:"";s:9:"parent_id";s:1:"0";s:5:"value";s:0:"";}i:15;a:7:{s:2:"id";s:1:"3";s:4:"slug";s:27:"please-enter-any-two-digits";s:4:"name";s:27:"Please enter any two digits";s:4:"type";s:6:"secret";s:7:"options";s:0:"";s:9:"parent_id";s:1:"2";s:5:"value";s:2:"14";}i:16;a:7:{s:2:"id";s:1:"4";s:4:"slug";s:6:"submit";s:4:"name";s:6:"Submit";s:4:"type";s:6:"submit";s:7:"options";s:0:"";s:9:"parent_id";s:1:"2";s:5:"value";s:0:"";}}
I just want to replace the value that I have in the variables and save it in database.
That format is the result of PHP's serialize() function. You can use unserialize() to decode it.

How to Extract Data from a field with multiple values?

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
}

Categories