JSON data parsing cannot understand - php

array(2) {
["success"]=>
string(1) "1"
["return"]=>
array(125) {
[0]=>
array(11) {
["marketid"]=>
string(3) "141"
["label"]=>
string(6) "42/BTC"
["primary_currency_code"]=>
string(2) "42"
["primary_currency_name"]=>
string(6) "42Coin"
["secondary_currency_code"]=>
string(3) "BTC"
["secondary_currency_name"]=>
string(7) "BitCoin"
["current_volume"]=>
string(10) "0.11628537"
["last_trade"]=>
string(12) "223.00000000"
["high_trade"]=>
string(12) "256.88999999"
["low_trade"]=>
string(12) "205.00000000"
["created"]=>
string(19) "2014-01-12 19:35:49"
}
}
I was trying to process this in PHP
$result = api_query("getmarkets");
$json = json_decode($result);
var_dump($result);
I tried many times to start processing this data but how would I start this
I was thinking something like $json['return'][0]['marketid'] would grab the market id.

Looks like it is already php object, so no need to parse as json.
try...
$result['return'][0]['marketid']
... and delete ...
$json = json_decode($result);

Related

xmlfile to php array

i want to create an php array from an xml-file like this
test.xml
object(SimpleXMLElement)#3 (2) { ["uebertragung"]=> object(SimpleXMLElement)#5 (1) { ["#attributes"]=> array(9)
{ ["art"]=> string(7) "OFFLINE" ["umfang"]=> string(4) "TEIL"
["modus"]=> string(3) "NEW" ["version"]=> string(5) "1.2.7" ["sendersoftware"]=> string(7) "crm11" ["senderversion"]=> string(3) "1.1"
["techn_email"]=> string(18) "support#mail.com" ["timestamp"]=> string(19)
"2014-06-01T10:00:00" ["regi_id"]=> string(7) "ABCD143" } }
["anbieter"]=> object(SimpleXMLElement)#4 (3) { ["anbieternr"]=>
string(6) "144185" ["firma"]=> string(14) "redfirm"
["immobilie"]=> object(SimpleXMLElement)#6 (7) { ["objektkategorie"]=> object(SimpleXMLElement)#7 ...
mycode:
$xml = simplexml_load_file("test.xml") or die("Error: Cannot read xml file");
var_dump($xml);
echo "show1: " .$xml->openimmo->uebertragung->{'art'} . "<br>";
echo "show2: " .$xml->openimmo->uebertragung['art'];
show 1+2 show nothing, can someone help me, i dont understand the array structure ?
The art is an attribute, so you can get its value by
$xml->openimmo->uebertragung->attributes()->art
More here
http://php.net/manual/en/simplexmlelement.attributes.php

How to refer to data in an array (php/json)

I am obtaining a json object using the following:
$json = file_get_contents("url-here");
$data = json_decode($json, true);
//test
var_dump($data);
This gives me something like this:
array(2) { ["ok"]=> bool(true) ["result"]=> array(1) { [0]=> array(2)
{ ["update_id"]=> int(44893465) ["message"]=> array(5) {
["message_id"]=> int(16) ["from"]=> array(3) { ["id"]=> int(29595794)
["first_name"]=> string(3) "Bob" ["username"]=> string(14) "Bobo" }
["chat"]=> array(3) { ["id"]=> int(29595794) ["first_name"]=>
string(3) "Bob" ["username"]=> string(14) "Bobo" } ["date"]=>
int(1435354253) ["text"]=> string(7) "/q 3.33" } } } }
I would then like to add certain values into variables. For example I would like to extract username, text, message_id, etc
But no matter what I try my variables are empty:
//let's test it
echo "Username: " . $data[1][0]["username"];
//another test
echo $data->username;
Neither of these are working and my research has not helped me find a solution. I am stumped on this one.
Any pointers in the right direction would really be appreciated.
array(2) {
["ok"]=> bool(true)
["result"]=> array(1)
{
[0]=> array(2)
{
["update_id"]=> int(44893465)
["message"]=> array(5)
{
["message_id"]=> int(16)
["from"]=> array(3)
{
["id"]=> int(29595794)
["first_name"]=> string(3) "Bob"
["username"]=> string(14) "Bobo"
}
["chat"]=> array(3)
{
["id"]=> int(29595794)
["first_name"]=> string(3) "Bob"
["username"]=> string(14) "Bobo"
}
["date"]=> int(1435354253)
["text"]=> string(7) "/q 3.33"
}
}
}
}
You are using wrong array index. $data[1][0]["username"]; not exists.
$data["result"][0]["message"]["from"]["username"];
$data["result"][0]["message"]["chat"]["username"];
This will give you the proper username
$json = file_get_contents("url-here");
$data = json_decode($json, true);
//test
echo $data["result"][0]['message']['from']['username'];
output
Bobo

Converting Json String into PHP array then using php array

Im new to json & php and I'm having some issues with json into php string
My json string looks like this
{"status":"OK","cards":
[{"id":100001,"name":"batman","image":11111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T11:37:07Z"},
{"id":100002,"name":"superman","image":111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T12:30:09Z"},
{"id":100003,"name":"catwoman","image":1111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T12:39:42Z"},
{"id":100004,"name":"bane","image":1111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-09-08T12:56:04Z"}
]}
So Far i have created my string
$json_raw = '{"status":"OK","cards": [{"id":100001,"name": .....
Decoded the json
$arr = json_decode($json_raw, TRUE);
I var_dump($arr);
then it returns
array(2) { ["status"]=> string(2) "OK" ["cards"]=> array(4) { [0]=> array(8) { ["id"]=> int(100001) ["name"]=> string(6) "batman" ["image"]=> int(11111) ["size"]=> int(75) ["region_id"]=> int(1) ["locked"]=> bool(false) ["status"]=> string(6) "active" ["created_at"]=> string(20) "2013-08-15T11:37:07Z" } [1]=> array(8) { ["id"]=> int(100002) ["name"]=> string(8) "superman" ["image"]=> int(111111) ["size"]=> int(75) ["region_id"]=> int(1) ["locked"]=> bool(false) ["status"]=> string(6) "active" ["created_at"]=> string(20) "2013-08-15T12:30:09Z" } [2]=> array(8) { ["id"]=> int(100003) ["name"]=> string(8) "catwoman" ["image"]=> int(1111111) ["size"]=> int(75) ["region_id"]=> int(1) ["locked"]=> bool(false) ["status"]=> string(6) "active" ["created_at"]=> string(20) "2013-08-15T12:39:42Z" } [3]=> array(8) { ["id"]=> int(100004) ["name"]=> string(4) "bane" ["image"]=> int(1111111) ["size"]=> int(75) ["region_id"]=> int(1) ["locked"]=> bool(false) ["status"]=> string(6) "active" ["created_at"]=> string(20) "2013-09-08T12:56:04Z" } } }
Now all I want to do is be able to use this data
e.g if name = batman then
I know this is a stupid question but I am struggling :(
Thank in Advance
json_decode() with TRUE as second parameter gives you an associative array. You need to access the correct index to do what you want.
To list the complete associative array with nice formatting, you can do:
echo '<pre>', print_r($arr), '</pre>';
Now, to access the name in your array:
$man = $arr['cards'][0]['name'];
To check if it's Batman (yay!):
if( isset($man) && $man == 'batman' ) {
# code ...
}
For getting the name of all similar names:
$man = $json['cards']['0']['name'];
for ($i=0; $i < count($json['cards']); $i++) {
echo $json['cards'][$i]['name']."\n";
}
See it live!
when you got the array
$arr = json_decode($json_raw, TRUE);
then check if cards key exist
if(array_key_exists('cards', $arr)){
foreach($arr['cards'] as $key=>$val){
echo $key; ///name, id..
echo $val; /// batman,...
if($key == 'name' && $val =='batman'){
//-------do your stuff
}
}
}
Try with:
$cards = $arr['cards'];
foreach($cards as $card) {
if($card['name'] == 'batman') echo 'Hello batman!';
}
EDIT:
Ok, so this worked for me using code above, try it yourself if you want:
<?php
$json_raw = '{"status":"OK","cards":
[{"id":100001,"name":"batman","image":11111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T11:37:07Z"},
{"id":100002,"name":"superman","image":111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T12:30:09Z"},
{"id":100003,"name":"catwoman","image":1111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T12:39:42Z"},
{"id":100004,"name":"bane","image":1111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-09-08T12:56:04Z"}
]}';
$arr = json_decode($json_raw, TRUE);
$cards = $arr['cards'];
foreach($cards as $card) {
if($card['name'] == 'batman') echo 'Hello batman!';
}
?>

Wordpress Parsing Array in PHP

How can I parse this in PHP:
a:8:{s:3:"key";s:19:"field_501743d4baa78";s:5:"label";s:8:"Category";s:4:"name";s:8:"category";s:4:"type";s:8:"checkbox";s:12:"instructions";s:0:"";s:8:"required";s:1:"1";s:7:"choices";a:17:{s:4:"Arts";s:4:"Arts";s:8:"Business";s:8:"Business";s:14:"Communications";s:14:"Communications";s:16:"Criminal Justice";s:16:"Criminal Justice";s:13:"Culinary Arts";s:13:"Culinary Arts";s:9:"Education";s:9:"Education";s:11:"Engineering";s:11:"Engineering";s:11:"Health Care";s:11:"Health Care";s:22:"Information Technology";s:22:"Information Technology";s:13:"International";s:13:"International";s:5:"Legal";s:5:"Legal";s:17:"Political Science";s:17:"Political Science";s:10:"Psychology";s:10:"Psychology";s:8:"Religion";s:8:"Religion";s:7:"Science";s:7:"Science";s:9:"Technical";s:9:"Technical";s:10:"Veterinary";s:10:"Veterinary";}s:8:"order_no";s:1:"3";}
to get a list of all the "categorys" such as Arts, Business, Communications etc etc
Thanks for your help
UPDATE
After running unserialize this is what i get:
array(8) { ["key"]=> string(19) "field_501743d4baa78" ["label"]=> string(8) "Category" ["name"]=> string(8) "category" ["type"]=> string(8) "checkbox" ["instructions"]=> string(0) "" ["required"]=> string(1) "1" ["choices"]=> array(17) { ["Arts"]=> string(4) "Arts" ["Business"]=> string(8) "Business" ["Communications"]=> string(14) "Communications" ["Criminal Justice"]=> string(16) "Criminal Justice" ["Culinary Arts"]=> string(13) "Culinary Arts" ["Education"]=> string(9) "Education" ["Engineering"]=> string(11) "Engineering" ["Health Care"]=> string(11) "Health Care" ["Information Technology"]=> string(22) "Information Technology" ["International"]=> string(13) "International" ["Legal"]=> string(5) "Legal" ["Political Science"]=> string(17) "Political Science" ["Psychology"]=> string(10) "Psychology" ["Religion"]=> string(8) "Religion" ["Science"]=> string(7) "Science" ["Technical"]=> string(9) "Technical" ["Veterinary"]=> string(10) "Veterinary" } ["order_no"]=> string(1) "3" }
however i'm not sure how to loop through and just get the category names - sorry - i'm new to PHP - probably just doing something stupid - thanks for your help
That's just a serialized array. Just unserialize it and getting the values is easy:
$array = unserialize(a:8:{s:3:"key";s:19:"field_501743d4baa78";s:5:"label";s:8:"Category";s:4:"name";s:8:"category";s:4:"type";s:8:"checkbox";s:12:"instructions";s:0:"";s:8:"required";s:1:"1";s:7:"choices";a:17:{s:4:"Arts";s:4:"Arts";s:8:"Business";s:8:"Business";s:14:"Communications";s:14:"Communications";s:16:"Criminal Justice";s:16:"Criminal Justice";s:13:"Culinary Arts";s:13:"Culinary Arts";s:9:"Education";s:9:"Education";s:11:"Engineering";s:11:"Engineering";s:11:"Health Care";s:11:"Health Care";s:22:"Information Technology";s:22:"Information Technology";s:13:"International";s:13:"International";s:5:"Legal";s:5:"Legal";s:17:"Political Science";s:17:"Political Science";s:10:"Psychology";s:10:"Psychology";s:8:"Religion";s:8:"Religion";s:7:"Science";s:7:"Science";s:9:"Technical";s:9:"Technical";s:10:"Veterinary";s:10:"Veterinary";}s:8:"order_no";s:1:"3";});
var_dump($array);
$sample_arr = unserialize($array); //unserialize here
$sample_arr = $sample_arr['choices']; //get the array
then you have the category list saved to $sample_arr.
foreach($sample_arr as $temp) {
//do stuff with each element here
}
Examples include outputting this as a html list
echo '<ul>';
foreach($sample_arr as $temp) {
echo '<li>' . $temp . '</li>';
}
echo '</ul>';

How do I loop through array of values posted

I'm posting an array of ids and want to loop over those values. I'm trying the following to populate an array with key/value pairs but it looks like the array is coming out empty.
$arr = array();
foreach($_POST['ids'] as $id)
{
$arr[$id] = GetStuff($id);
}
UPDATE: Looks like array was populated fine. I'm trying to return contents of array by doing echo json_encode($arr) but response is blank.
Here is output of var_dump($_POST);
array(1) {
["ids"]=>
array(18) {
[0]=>
string(6) "156795"
[1]=>
string(6) "156800"
[2]=>
string(4) "4292"
[3]=>
string(6) "796053"
[4]=>
string(6) "660520"
[5]=>
string(4) "4293"
[6]=>
string(4) "4287"
[7]=>
string(6) "488339"
[8]=>
string(6) "837701"
[9]=>
string(7) "1152093"
[10]=>
string(7) "1186434"
[11]=>
string(7) "1324432"
[12]=>
string(6) "796051"
[13]=>
string(6) "144860"
[14]=>
string(5) "15065"
[15]=>
string(7) "1324434"
[16]=>
string(5) "13066"
[17]=>
string(4) "6969"
}
}
foreach($_POST['ids'] AS $i=>$id) {
//do stuff
}
Don't forget about quotes..
$arr = array();
foreach($_POST['ids'] as $id)
{
$arr[$id] = GetStuff($id);
}
Notice the tick marks around ids in $_POST.
This should work:
foreach($_POST['ids'] as $id)
{
$arr[$id] = $_POST['ids'][$id];
}
or even faster, if you are just wanting an array identical to the posted ids:
$arr = $_POST['ids'];
unless I am misunderstanding the question.

Categories