create an empty object in JSON with PHP? - php

i have create json from an array but how we can make an empty json
$jsonrows['paymentmethods']['CC']=array()
json_encode($jsonrows['paymentmethods']['CC']=array())
currently output is like this
"CC":[]
what i need is
"CC":{}
please help me with this

Use a class instead of an array:
var_dump(json_encode(new StdClass()));

Try This
$cc['CC'] = new stdClass ;
echo json_encode($jsonrows['paymentmethods']=$cc);
Output is
{"CC":{}}

For code readability, I use $emptyObject = (object)[];
In the context of your example:
$jsonrows['paymentmethods']['CC']=array();
echo json_encode($jsonrows['paymentmethods']);
yields the undesired: {"CC":[]}
$jsonrows['paymentmethods']['CC']=(object)array();
echo json_encode($jsonrows['paymentmethods']);
gives what you want: {"CC":{}}

Related

extract specific data from array in mysql database using php

This is the data that i need to extract like example profile_contact_numbers
so the output will be +639466276715
how can i do it in php code??
any help will do regards
a:2:
{s:23:"profile_contact_numbers";s:13:"+639466276715";s:16:"profile_position";s:7:"Courier";}
I'm not sure 100% this can go into an array but try the unserialize function
$json_resp = {your values};
$array[] = unserialize($json_resp);
To check if it has gone into an array print_r on $array.
Read this link if the code above doesn't work
http://php.net/manual/en/function.unserialize.php
I have managed to fix it
$serialized = array(unserialize('a:2:{s:23:"profile_contact_numbers";s:13:"+639466276715";s:16:"profile_position";s:7:"Courier";}'));
var_dump($serialized);
use code :
$var = preg_split('["]','{s:23:"profile_contact_numbers";s:13:"+639466276715";s:16:"profile_position";s:7:"Courier";}');
echo $var[1].'='.$var[3]; // profile_contact_numbers=+639466276715
echo $var[5].'='.$var[7]; // profile_position=Courier

Using json response in PHP

I am trying to use one object from the many provided in my json request.
Trying to obtain only the country name from the data that is given.
$location = file_get_contents('http://freegeoip.net/json/'.$_SERVER['REMOTE_ADDR']);
echo $location;
The above code gives me the following string:
{"ip":"x.xx.xx.x","country_code":"FR","country_name":"France","region_code":"A2","region_name":"Bretagne","city":"Brest","zipcode":"","latitude":xxxx,"longitude":xxxx,"metro_code":"","area_code":""}
Any help would be greatly appreciated!
$a = json_decode($location);
echo $a->country_name;
You might also want to have a look on this. http://www.php.net/manual/en/function.json-decode.php
Look for manual json_decode
$tmp = json_decode($location);
echo $tmp->country_code

Echo element from php array

Can someone tell me how I can loop through the below array?
http://pastebin.com/rhaF5Zdi
I've tried with out luck:
$_data = json_decode($_data);
foreach ( $_data as $tweet )
{
echo "{$tweet->text}\n";
}
thanks
ps: im follwoing this php script.
http://mikepultz.com/2013/06/mining-twitter-api-v1-1-streams-from-php-with-oauth/
hers another paste bin on the array. there seems to be multiple arrays happening
http://pastebin.com/dduzhpqY
It looks like you might be creating a PHP stnd object instead of an array
//this will create a php standard object
$objOfData=json_decode($json);
Instead Use the version below: (Notice the the 2nd parameter is TRUE)
$associativeArray=json_decode($json, TRUE);
This will turn the object into an associative array and you can access fields like so:
$id=$associativeArray['id'];
More info here: json_decode
The code in posted link is not JSON, but it is output of print_r() function. PHP has no invert function to print_r(), but on php.net in print_r() documentation's comments you can find some user-made functions which can read it.
For example this one:
http://www.php.net/manual/en/function.print-r.php#93529
I hope it will help. Good Luck :)
Ended up using:
if(array_key_exists('text', $_data)){
echo 'Tweet ID = '.$_data['id_str'];
echo 'Tweet Text = '.$_data['text'];
echo '<br /><br />';
}
cheers

How to decode the following code in Json?

I'm trying to decode the following JSON using php json_decode function.
[{"total_count":17}]
I think the square brackets in output is preventing it. How do I get around that? I can't control the output because it is coming from Facebook FQL query:
https://api.facebook.com/method/fql.query?format=json&query=SELECT%20total_count%20FROM%20link_stat%20WHERE%20url=%22http://www.apple.com%22
PHP's json_decode returns an instance of stdClass by default.
For you, it's probably easier to deal with array. You can force PHP to return arrays, as a second parameter to json_decode:
$var = json_decode('[{"total_count":17}]', true);
After that, you can access the variable as $result[0]['total_count']
See this JS fiddle for an example of how to read it:
http://jsfiddle.net/8V4qP/1
It's basically the same code for PHP, except you need to pass true as your second argument to json_decode to tell php you want to use it as associative arrays instead of actual objects:
<?php
$result = json_decode('[{"total_count":17}]', true);
print $result[0]['total_count'];
?>
if you don't pass true, you would have to access it like this: $result[0]->total_count because it is an array containing an object, not an array containing an array.
$json = "[{\"total_count\":17}]";
$arr = Jason_decode($json);
foreach ($arr as $obj) {
echo $obj->total_count . "<br>";
}
Or use json_decode($json, true) if you want associative arrays instead of objects.

How to parse JSON string in php

I'm getting below JSON response:
[{"startDate":"2012-07-12 11:21:38 +0530","totalTime":0},{"startDate":"2012-07-11 11:27:33 +0530","totalTime":0},{"startDate":"2012-07-16 18:38:37 +0530","totalTime":0},{"startDate":"2012-07-17 14:18:32 +0530","totalTime":0}]
i want make array of start date and totalTime, i have used these two lines but it wont work $obj, please suggest..
$obj = json_decode($dateTimeArr);
$dateAr = $obj->{'startDate'};
As everyone said, and you did - use json_decode.
$dateArrays = json_decode($dateTimeArr, true); // decode JSON to associative array
foreach($dateArrays as $dateArr){
echo $dateArr['startDate'];
echo $dateArr['totalTime'];
}
In future, if you are unsure what type or structure of data is in the variable, do var_dump($var) and it will print type of variable and its content.
It is very easy:
$Arr = json_decode($JSON, true);
json_decode() will give you nested PHP types you can then descend to retrieve your data.
use json_decode($json_response,true) to convert json to Array
Guess what you are looking for is json_decode()
Check out http://php.net/manual/en/function.json-decode.php for the inner workings

Categories