I am trying to get user profile from pinterest using oauth api.
code for user data :
$me = $pinterest->users->me(array(
'fields' => 'username,first_name,last_name,image[large]'
));
and echo result by :
echo $me;
the output is as follow :
{"id":"195414208739840616","username":"rajivsharma033","first_name":"Rajiv","last_name":"Sharma","bio":null,"created_at":null,"counts":null,"image":{"large":{"url":"https:\/\/s-media-cache-ak0.pinimg.com\/avatars\/rajivsharma033_1459712414_280.jpg","width":280,"height":280}}}
Now i want to echo this result as
id="195414208739840616"
username="rajivsharma033"
first_name="Rajiv"
and so on...
please help me.
Since you are getting json data so you need to use json_decode():-
<?php
$me = '{"id":"195414208739840616","username":"rajivsharma033","first_name":"Rajiv","last_name":"Sharma","bio":null,"created_at":null,"counts":null,"image":{"large":{"url":"https:\/\/s-media-cache-ak0.pinimg.com\/avatars\/rajivsharma033_1459712414_280.jpg","width":280,"height":280}}}';
$array_data = json_decode($me);
echo "<pre/>";print_r($array_data);
foreach ($array_data as $key=>$value){
if($key == 'image'){
echo $key. " url is=" . $value->large->url .'<br/>';
}else{
echo $key. "=" . $value .'<br/>';
}
}
I would solve it by using two times foreach:
$a = json_decode($me);
foreach ($a as $key=>$value){
echo $key.'="'.$value.'"<br/>';
}
foreach ($a['image']['large'] as $key=>value){
echo 'image-large-'$key.'="'.$value.'"<br/>';
}
Alternatively, you can do this recursive:
function echojson($string=''){
$a = json_decode($me);
foreach ($a as $key=>$value){
if (is_array($value)) echojson($string.'-'.$key);
else
echo $string.$key.'="'.$value.'"<br/>';
}
}
Related
this is my code, I want to echo all the data inside this $json.
$json = '{"year":"2018","month":"4","name":"Apr 2018 - Sept 2018","description":null,"code":"2018\/04","session_from_at":{"date":"2018-04-16 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"session_to_at":{"date":"2018-09-21 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"registered_from_at":{"date":"2018-04-16 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"registered_to_at":{"date":"2018-04-20 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"added_dropped_from_at":null,"added_dropped_to_at":null,"withdrew_from_at":null,"withdrew_to_at":null,"attended_from_at":{"date":"2018-04-20 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"attended_to_at":{"date":"2018-04-29 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"cw_marked_from_at":{"date":"2018-04-20 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"cw_marked_to_at":{"date":"2018-04-29 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"fe_marked_from_at":{"date":"2018-04-29 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"fe_marked_to_at":{"date":"2018-09-21 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"created_by":1,"updated_by":1,"id":2}';
$arr = json_decode($json,true);
foreach($arr as $key=>$value){
echo $key . "<br>";
echo $value . "<br>"; // PHP Notice: Array to string conversion in /workspace/Main.php on line 11
// not displaying the value
}
?>
the problem come went the data loop on session_from_at which have three data in array inside.
I think you are not using blade. Your file name is Main.php
If you are using blade, I advise you to use #foreach instead of foreach.
And after json_decode, you still have an array inside array.
Try this code:
$json = '{"year":"2018","month":"4","name":"Apr 2018 - Sept 2018","description":null,"code":"2018\/04","session_from_at":{"date":"2018-04-16 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"session_to_at":{"date":"2018-09-21 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"registered_from_at":{"date":"2018-04-16 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"registered_to_at":{"date":"2018-04-20 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"added_dropped_from_at":null,"added_dropped_to_at":null,"withdrew_from_at":null,"withdrew_to_at":null,"attended_from_at":{"date":"2018-04-20 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"attended_to_at":{"date":"2018-04-29 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"cw_marked_from_at":{"date":"2018-04-20 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"cw_marked_to_at":{"date":"2018-04-29 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"fe_marked_from_at":{"date":"2018-04-29 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"fe_marked_to_at":{"date":"2018-09-21 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"created_by":1,"updated_by":1,"id":2}';
$arr = json_decode($json, true);
function echoArray($arr) {
foreach($arr as $key=>$value){
echo $key . "<br>";
if (is_array($value)) {
echoArray($value);
} else {
echo $value . "<br>";
}
}
}
echoArr($arr);
You got an error because other $value is an array you cant echo it so try to check if its array or object like this :
foreach($arr as $key=>$value){
echo $key . "<br>";
if(is_object($value) || is_array($value)){
foreach($value as $key2=>$value2){
echo $key2 . "<br>";
echo $value2 . "<br>";
}
}else{
echo $value . "<br>";
}
}
try it:
$arr['year'];
$arr['month'];
you do not need to use foreach.
I am trying to parse a json array string. Though print_r prints all the values properly but while fetching the records I failed.
I must be doing something very terribly wrong. Could you please help to correct it.
So here is my code
<?
$str ='
{
"tenant_view_details": [
{
"status":"S",
"tenant_general":[
{"tenant_id":"6003","code":"ICI001","type":"BANK","category":"SM","gstn":"IWSDFS7372","pan":"KSAH9876AS","cin":"ASHED456","name":"ICICI Bank","address_line_1":"23, Hertz Plaza, Rajiv Chowk","address_line_2":" ","address_city":"New Delhi","address_distict ":"New Delhi","address_state ":"DL","address_state_code ":"07","address_pin ":"119923","contact_name ":"contact_name","contact_phone ":"1234567890","contact_std_code ":"11","contact_landline ":"1234567890","contact_email_id ":"contact#email.com"} ],
"tenant_bank": [
{ "bank_name ":"xyz","bank_account_no ":"12345","account_type ":"abc","ifsc_code ":"xx123","address_line_1 ":"qwer","address_line_2 ":"23","address_city ":"abc","address_district ":"xyz","address_state_name ":"asd","address_state_code ":"02","address_pin ":"123456" } ],
"tenant_agreement": [
{ "category ":"OM","tenancy_type ":"EXT","echarge_type ":"FIX","total_tenancy_value ":"32900.00","energy_charge_value ":"16543.00","rev_share_pct ":"0.05","start_dt":"10-1-2018","end_dt":"31-12-2020","attachment_path ":"na","change_value ":"na","latest_flag ":"Y","w_status ":"20","status ":"ACT"} ],
"tenant_sites": [
{"tenant_id":"6003","site_master_id":"1020","tn_site_code":"UPWMORA00069473","tn_site_name":"Deendayal Nagar","tn_cluster":"CLUSTER-03","circle_code":"UW","tower_type":"MAST","site_type":"ID","tenancy_type":"EXT","echarge_type":"FIX","primary_tenant":"N","latest_flag ":"Y"}, {"tenant_id":"6003","site_master_id":"1019","tn_site_code":"UPWGHAZ00069467","tn_site_name":"DJ College Newari Road, Modina","tn_cluster":"CLUSTER-03","circle_code":"UW","tower_type":"MAST","site_type":"ID","tenancy_type":"EXT","echarge_type":"FIX","primary_tenant":"N","latest_flag ":"Y"}, {"tenant_id":"6003","site_master_id":"1018","tn_site_code":"UPWMORA00069464","tn_site_name":"Bazar Makhbra","tn_cluster":"CLUSTER-03","circle_code":"UW","tower_type":"RTT","site_type":"OD","tenancy_type":"EXT","echarge_type":"FIX","primary_tenant":"N","latest_flag ":"Y"}, {"tenant_id":"6003","site_master_id":"1016","tn_site_code":"UPWGHAZ00069454","tn_site_name":"Hoshdarpur Garhi","tn_cluster":"CLUSTER-03","circle_code":"UW","tower_type":"RTT","site_type":"OD","tenancy_type":"EXT","echarge_type":"FIX","primary_tenant":"N","latest_flag ":"Y"}, {"tenant_id":"6003","site_master_id":"1011","tn_site_code":"UPWMUZN00069430","tn_site_name":"Sikanderpur","tn_cluster":"CLUSTER-03","circle_code":"UW","tower_type":"RTT","site_type":"ID","tenancy_type":"EXT","echarge_type":"FIX","primary_tenant":"N","latest_flag ":"Y"}, {"tenant_id":"6003","site_master_id":"1008","tn_site_code":"UPWALIG00069299","tn_site_name":"Mukund Vihar","tn_cluster":"CLUSTER-03","circle_code":"UW","tower_type":"RTT","site_type":"","tenancy_type":"EXT","echarge_type":"FIX","primary_tenant":"N","latest_flag ":"Y"} ]
}
]
}
';
$dataset = json_decode($str, true);
//print_r ($dataset);
foreach ($dataset['tenant_view_details'] as $newdata) {
$general = $newdata['tenant_general'];
$bank = $newdata['tenant_bank'];
$agreement = $newdata['tenant_agreement'];
$sites = $newdata['tenant_sites'];
}
//prinitng data
echo "<br/>";
print_r($general);
echo "<br/>";
print_r($bank);
echo "<br/>";
print_r($agreement);
echo "<br/>";
foreach($general as $general_data){
(isset($general_data['tenant_id']) && !empty($general_data['tenant_id']))? $tenant_id=$general_data['tenant_id'] : $tenant_id="not set";
echo "<br/>tenant_id::" . $tenant_id;
}
foreach($bank as $bank_data){
(isset($bank_data['bank_name']) && !empty($bank_data['bank_name']))? $bank_name=$bank_data['bank_name'] : $bank_name="not set";
echo "<br/>bank_name:" . $bank_data['bank_name'];
}
foreach($agreement as $agreement_data){
(isset($agreement_data['category']) && !empty($agreement_data['category']))? $category=$agreement_data['category'] : $category="not set";
(isset($agreement_data['tenancy_type']) && !empty($agreement_data['tenancy_type']))? $tenancy_type=$agreement_data['tenancy_type'] : $tenancy_type="not set";
(isset($agreement_data['echarge_type']) && !empty($agreement_data['echarge_type']))? $echarge_type=$agreement_data['echarge_type'] : $echarge_type="not set";
echo "<br/>category:" . $category;
echo "<br/>tenancy_type:" . $tenancy_type;
echo "<br/>echarge_type:" . $echarge_type;
}
?>
Your problem is that in your $bank and $category arrays, all the keys have trailing spaces on them. If you change the references to include those spaces, your code works fine. See https://3v4l.org/GX5iC
Alternatively you can trim the array keys, using something like this code inside your foreach loops:
$bank_keys = array_map('trim', array_keys($bank_data));
$bank_data = array_combine($bank_keys, array_values($bank_data));
You could also create a recursive function to trim the entire $dataset value. For example:
function trim_keys($array) {
foreach ($array as $key => $value) {
echo "trimming key '$key' to '" . trim($key) . "'\n";
unset($array[$key]);
$array[trim($key)] = $value;
if (is_array($value))
$array[trim($key)] = trim_keys($value);
else
$array[trim($key)] = $value;
}
return $array;
}
Then, by using
$dataset = trim_keys($dataset);
Your code will work fine. Demo on 3v4l.org
So I have this array that saves a json string.
Array
(
[0] => "2jDQoU9D2wu04wqkg0ImUI":{"date":"2016-08-02 14:08:49","type":"story","story_id":"2jDQoU9D2wu04wqkg0ImUI","series_id":"1RAv0uDbcIieYgYqywqYmk"}
)
I want to be able to access just the value "2jDQoU9D2wu04wqkg0ImUI" at the start of the json string and then also be able to do something like $value[0]['type'] to get the type from this json string object. I'm pretty new to PHP and struggling to get this working. I've tried JSON encoding/decoding and can't seem to get anything to work.
What's the proper way to go about this? Thanks in advance.
I hope this code will solve your problem.
$array[0] = '"2jDQoU9D2wu04wqkg0ImUI":{"date":"2016-08-02 14:08:49","type":"story","story_id":"2jDQoU9D2wu04wqkg0ImUI","series_id":"1RAv0uDbcIieYgYqywqYmk"}';
//print_r($arr);
$JsonString = '{' . $array[0] . '}';
$json = json_decode($JsonString);
foreach($json as $key => $value){
echo "Key : $key <br />";
echo "Type : ". $value->type."<br />";
echo "date : ". $value->date."<br />";
echo "story_id : ". $value->story_id."<br />";
echo "series_id : ". $value->series_id."<br />";
}
so you have array $jsonArray and you want to access just the key of it
if its single dimension array :
echo key($jsonArray); //prints 2jDQoU9D2wu04wqkg0ImUI
Otherwise if its multidimensional you can loop through it and do whatever you want with each key
foreach($jsonArray as $key => $value) {
echo $key; //prints 2jDQoU9D2wu04wqkg0ImUI
}
Try this:
$arrayOfObjects = [];
foreach($array as $key => $value) arrayOfObjects[] = json_decode($value);
Now you can loop through this new $arrayOfObjects
foreach($arrayOfObjects as $key => $object){
echo $object->date . '<br>';
echo $object->type . '<br>';
echo $object->story_id . '<br>';
echo $object->series_id . '<br> ==== >br> ';
}
I am in need of a PHP-program which will take all values from a HTML-link (with GET), regardless of how much one changes things in the address-bar, and then prints it out on the page. The code I have now is this:
HTML:
Link
PHP:
<?php
Echo "Value1: "; Echo $_GET["a"]; Echo "\n";
Echo "Value2: "; Echo $_GET["b"]; Echo "\n";
Echo "Value3: "; Echo $_GET ["c"];
?>
It works as intended for just these values, but it cannot cope with, for example, another variable being added in the address bar. I need some kind of PHP-function which can look for all kinds of variables through the GET-function.
Any help is appreciated!
Use a loop (modify as needed):
foreach ($_GET as $num => $value)
{
echo 'Value ' . $num . ': ' . htmlentities($value). "<br>" . PHP_EOL;
}
References:
Control structures
htmlentities()
Something like this:
<?php
foreach ($_GET as $key => $value) {
echo htmlspecialchars($key) . " - " . htmlspecialchars($value) . "<br>";
}
?>
<?php
print_r($_GET);
?>
Use a foreach loop
foreach ($_GET as $key => $val){
echo htmlentities($key).": ".htmlentities($val)."<br />\n";
}
or simply -
echo "<pre>".print_r($_GET,1)."</pre>";
To get all values and their corresponding names:
foreach($_GET as $key => $value)
{
echo $key . ' - ' . $value;
}
$my_get = array(); // store $_GET vars
if ('GET' == $_SERVER['REQUEST_METHOD']) // check that request method is "get"
foreach ($_GET as $key => $val)
{
$my_get[$key] = $val; // store
}
output $my_get:
array (
'a' => 'value1',
'b' => 'value2',
'c' => 'value3',
)
Since $_GET is nothing but an array, you can use for each loop to get the elements of it.
you get all $_GET values using print_r($GET) ...
otherwise you could to something like this
foreach(array_keys($_GET) as $key) {
echo htmlspecialchars($_GET[$key]);
}
Later Edit : took into account the security issue exposed in the comment
using this method is good because you know what kind of variable you have then you could do something cool like :
if ($key == 'a') do this
if ($key == 'b' && $_GET[$key] == 'bar') do that
This is an array i have
<?php
$page['Home']='index.html';
$page['Service']='services.html';
?>
How do i get to echo something like this for individual one like
Home is at index.html
and again how can i do this through a loop and echo all?
foreach($page as $key => $value) {
echo "$key is at $value";
}
For 'without loop' version I'll just ask "why?"
Without a loop, just for the kicks of it...
You can either convert the array to a non-associative one, by doing:
$page = array_values($page);
And then acessing each element by it's zero-based index:
echo $page[0]; // 'index.html'
echo $page[1]; // 'services.html'
Or you can use a slightly more complicated version:
$value = array_slice($page, 0, 1);
echo key($value); // Home
echo current($value); // index.html
$value = array_slice($page, 1, 1);
echo key($value); // Service
echo current($value); // services.html
If you must not use a loop (why?), you could use array_walk,
function printer($v, $k) {
echo "$k is at $v\n";
}
array_walk($page, "printer");
See http://www.ideone.com/aV5X6.
Echo key and value of an array without and with loop
$array = array(
'kk6NFKK'=>'name',
'nnbDDD'=>'claGg',
'nnbDDD'=>'kaoOPOP',
'nnbDDD'=>'JWIDE4',
'nnbDDD'=>'lopO'
);
print_r(each($array));
Output
Array
(
[1] => name
[value] => name
[0] => kk6NFKK
[key] => kk6NFKK
)
for the first question
$key = 'Home';
echo $key." is at ".$page[$key];
function displayArrayValue($array,$key) {
if (array_key_exists($key,$array)) echo "$key is at ".$array[$key];
}
displayArrayValue($page, "Service");
How to echo key and value of an array without and with loop
$keys = array_keys($page);
implode(',',$keys);
echo $keys[0].' is at '.$page['Home'];
My version without a loop would be like this:
echo implode(
"\n",
array_map(
function ($k, $v) {
return "$k is at $v";
},
array_keys($page),
array_values($page)
)
);
array_walk($v, function(&$value, $key) {
echo $key . '--'. $value;
});
Learn more about array_walk
A recursive function for a change;) I use it to output the media information for videos etc elements of which can use nested array / attributes.
function custom_print_array($arr = array()) {
$output = '';
foreach($arr as $key => $val) {
if(is_array($val)){
$output .= '<li><strong>' . ucwords(str_replace('_',' ', $key)) . ':</strong><ul class="children">' . custom_print_array($val) . '</ul>' . '</li>';
}
else {
$output .= '<li><strong>' . ucwords(str_replace('_',' ', $key)) . ':</strong> ' . $val . '</li>';
}
}
return $output;
}
You can try following code:
foreach ($arry as $key => $value)
{
echo $key;
foreach ($value as $val)
{
echo $val;
}
}