Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
hi i want to create a json array in codeigniter of the following format . correct me if i am wrong .
// my json format
$data={"records":[{"name1":[{"id1":"value","subject","subname"},{"id2":"value","subject2","subname"},{"id3":"value","subject3","subname"},{"id4":"value","subject4","subname"}]},
{"name2":[{"id1":"value","subject","subname"},{"id2":"value","subject2","subname"},{"id3":"value","subject3","subname"},{"id4":"value","subject4","subname"}]};
i am trying to create it in php using codeigniter ..... really need your help
Use json_encode() like this.
<?php
$array = array('name'=> 'value', 'name1', $some_var);
$json = json_encode($array);
// or an object
$obj = new stdClass();
$obj->name = 'Hello';
$obj->name1 = 'world';
$json2 = json_encode($obj);
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have a xml with more subcategory.
I want extract in Php the “long_name” where type is “adminkstrative_area_level_3”
How I can do?
This is my xml https://ibb.co/fY27bJ
I tried but don’t work
<?
$string_data = "https://maps.googleapis.com/maps/api/geocode/xml?latlng=41.51,15.16&key=AIzaSyClG_vc2nkQCzXqvDzW1maPrUWLyADI7xI";
$xml = simplexml_load_string($string_data);
$citta = (string) $xml->result[0]->address_component[3]->long_name;
echo "<p>".$citta."</p>";
?>
You are missing geoname in $xml-> name;
Try it like this:
$xml = simplexml_load_string($string_data);
$citta = (string)$xml->geoname->name;
echo $citta;
Demo Php
If you want to loop through mulitple items you could use:
foreach ($xml->geoname as $item) {
echo $item->name;
}
Update:
For the updated part you could use the same technique:
$xml = simplexml_load_file("https://maps.googleapis.com/maps/api/geocode/xml?latlng=41.51,15.16&key=AIzaSyClG_vc2nkQCzXqvDzW1maPrUWLyADI7xI");
foreach ($xml->result as $item) {
if ((string)$item->type === "administrative_area_level_3") {
echo $item->address_component->long_name;
}
}
Or by index [1]:
echo $xml->result[1]->address_component->long_name;
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
{
"success":1,"Data":{"status":"message sent",
"message_id":3594,
"personaluid":"161",
"gid":"0",
"uid":"160",
"message":"cxzczxczxsada123",
"chat_image":"",
"created_at":"2017-02-04 16:52:11",
"utype":"1"
}
}
{
aps =
{alert =
{body = hdhd; title=from some person name;};
badge = 1;
};
"gcm.message_id" = "0:1487247942837269%19aca14d19aca14d";
"gcm.notification.Content_available" = 1;
"gcm.notification.created_at" = "2017-02-16 17:55:41";
"gcm.notification.uid" = 161;
}
convert your json data to php array then use array_merge to merge both. Then again convert it to json data. Example:
<?php
$json1='{"success":1,
"Data":{"status":"message sent","message_id":3594,"personaluid":"161","gid":"0","uid":"160","message":"cxzczxczxsada123","chat_image":"","created_at":"2017-02-04 16:52:11","utype":"1"}
}';
$json2='{
"multicast_id":5114850183838817498,
"success":1,
"failure":0,
"canonical_ids":0,
"results":[{"message_id":"0:1487247086404626%19aca14d19aca14d"}]
}';
$ar1=json_decode($json1,true);
$ar2=json_decode($json2,true);
$ar1=array_merge($ar1,$ar2);
echo json_encode($ar1);
output:
{"success":1,"Data":{"status":"message sent","message_id":3594,"personaluid":"161","gid":"0","uid":"160","message":"cxzczxczxsada123","chat_image":"","created_at":"2017-02-04 16:52:11","utype":"1"},"multicast_id":5.1148501838388e+18,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1487247086404626%19aca14d19aca14d"}]}
You could array merge the 2 decoded arrays.
$data = json_decode($result, true);
$data = array_merge($data[0], $data[1]);
$data = json_encode($data, JSON_FORCE_OBJECTS);`
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have A Json data But I am unable to get desired response.
$content='{"success":true,"results":[{"id":"21390fb46e92","msisdncountrycode":"DE","msisdn":"+491788735000","statuscode":"HLRSTATUS_UNDELIVERED","hlrerrorcodeid":9,"subscriberstatus":"SUBSCRIBERSTATUS_ABSENT","imsi":null,"mccmnc":"26203","mcc":"262","mnc":"03","msin":null,"servingmsc":null,"servinghlr":null,"originalnetworkname":"E-Plus","originalcountryname":"Germany","originalcountrycode":"DE","originalcountryprefix":"+49","originalnetworkprefix":"178","roamingnetworkname":null,"roamingcountryname":null,"roamingcountrycode":null,"roamingcountryprefix":null,"roamingnetworkprefix":null,"portednetworkname":null,"portedcountryname":null,"portedcountrycode":null,"portedcountryprefix":null,"portednetworkprefix":null,"isvalid":"Yes","isroaming":"No","isported":"No","usercharge":"0.0100","inserttime":"2015-11-24 18:56:42.048693+08","storage":"CURL-TEST","route":"IP1"}]}';
i want to capture some contents like
id , msisdncountrycode , msisdncountrycode , statuscode , hlrerrorcodeid
And Other Values As Well Please Help To Solve This problem .
check out this:
$result = json_decode($content);
var_dump($result->results[0]->id);//will get id
var_dump($result->results[0]->msisdncountrycode);//will get Country code
use this(works with multiple results).
$d = json_decode($content);
foreach($d->results as $row){
echo $row->id; //echoes 21390fb46e92
echo $row->msisdncountrycode; //echoes DE
echo $row->statuscode; //echoes HLRSTATUS_UNDELIVERED
echo $row->hlrerrorcodeid; //echoes 9
}
Echoes are based on current input. Since its in loop, it will print all values.
You can try this. Its working fine for me.
json_decode($content)
Try this:
<?php
$content='{"success":true,"results":[{"id":"21390fb46e92","msisdncountrycode":"DE","msisdn":"+491788735000","statuscode":"HLRSTATUS_UNDELIVERED","hlrerrorcodeid":9,"subscriberstatus":"SUBSCRIBERSTATUS_ABSENT","imsi":null,"mccmnc":"26203","mcc":"262","mnc":"03","msin":null,"servingmsc":null,"servinghlr":null,"originalnetworkname":"E-Plus","originalcountryname":"Germany","originalcountrycode":"DE","originalcountryprefix":"+49","originalnetworkprefix":"178","roamingnetworkname":null,"roamingcountryname":null,"roamingcountrycode":null,"roamingcountryprefix":null,"roamingnetworkprefix":null,"portednetworkname":null,"portedcountryname":null,"portedcountrycode":null,"portedcountryprefix":null,"portednetworkprefix":null,"isvalid":"Yes","isroaming":"No","isported":"No","usercharge":"0.0100","inserttime":"2015-11-24 18:56:42.048693+08","storage":"CURL-TEST","route":"IP1"}]}';
echo '<pre>';
$jsonArr = json_decode($content, TRUE);
$outputArr = $jsonArr['results'][0];
print_r($outputArr);
$id = $outputArr['id'];
$msisdncountrycode = $outputArr['msisdncountrycode'];
$statuscode = $outputArr['statuscode'];
$hlrerrorcodeid = $outputArr['hlrerrorcodeid'];
?>
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am a newbie php guy. So i need json datas for my ios application. I can get datas from my database and print as json..I wanna send many type datas with json. So i need edit my json output.
Hereby the json output that i have now:
[{"PlaceId":"1","PlaceAdminId":"5","PlaceName":"Alinin Yeri","PlaceAddress":"Be?ikta?","PlaceImage1":"iVBORw0KGgoAAAANSUhEUgAAAFAAAABQCAYAAACOEfKtAAAAHGlET1QAAAACAAAAAAAAACgAAAAoAAAAKAAAACgAAADWVJb57gAAAKJJREFUeAHs1LEJADAMA8Hsv3SyQ6RGcIVLG\/FYf+69x\/wzAC98IAAB\/K9fQ10+0Af6wOkWTIdvOCy9ASAHcuB0C6bDp\/5q7APIgRw43YLp8A2HpTcA5EAOnG7BdPjUX419ADmQA6dbMB2+4bD0BoAcyIHTLZgOn\/qrsQ8gB3LgdAumwzcclt4AkAM5cLoF0+FTfzX2AeRADpxuwXT4hsPSGw8AAP\/\/ttPzCAAAAKBJREFU7dSxCQAwDAPB7L90skOkRnCFSxvxWH\/uvcf8MwAvfCAAAfyvX0NdPtAH+sDpFkyHbzgsvQEgB3LgdAumw6f+auwDyIEcON2C6fANh6U3AORADpxuwXT41F+NfQA5kAOnWzAdvuGw9AaAHMiB0y2YDp\/6q7EPIAdy4HQLpsM3HJbeAJADOXC6BdPhU3819gHkQA6cbsF0+IbD0hsPozm5V7YwxV0AAAAASUVORK5CYII=","PlaceImage2":null,"PlaceImage3":null,"PlaceImage4":null},
{"ActivityId":"1","ActivityName":"zulfu livaneli","ActivityDescription":"Zulfu konseri","ActivityType":"1","ActivityDate":"2011-00-00","PlaceName":"Alinin Yeri","PlaceId":"1"},
{"ActivityId":"2","ActivityName":"Bir tat bir doku","ActivityDescription":"Y?lmaz Erdo?an oyunu","ActivityType":"3","ActivityDate":"2013-00-00","PlaceName":"Alinin Yeri","PlaceId":"1"},
And this is how i want:
[Places:{{"PlaceId":"1","PlaceAdminId":"5","PlaceName":"Alinin Yeri","PlaceAddress":"Be?ikta?","PlaceImage1":"iVBORw0KGgoAAAANSUhEUgAAAFAAAABQCAYAAACOEfKtAAAAHGlET1QAAAACAAAAAAAAACgAAAAoAAAAKAAAACgAAADWVJb57gAAAKJJREFUeAHs1LEJADAMA8Hsv3SyQ6RGcIVLG\/FYf+69x\/wzAC98IAAB\/K9fQ10+0Af6wOkWTIdvOCy9ASAHcuB0C6bDp\/5q7APIgRw43YLp8A2HpTcA5EAOnG7BdPjUX419ADmQA6dbMB2+4bD0BoAcyIHTLZgOn\/qrsQ8gB3LgdAumwzcclt4AkAM5cLoF0+FTfzX2AeRADpxuwXT4hsPSGw8AAP\/\/ttPzCAAAAKBJREFU7dSxCQAwDAPB7L90skOkRnCFSxvxWH\/uvcf8MwAvfCAAAfyvX0NdPtAH+sDpFkyHbzgsvQEgB3LgdAumw6f+auwDyIEcON2C6fANh6U3AORADpxuwXT41F+NfQA5kAOnWzAdvuGw9AaAHMiB0y2YDp\/6q7EPIAdy4HQLpsM3HJbeAJADOXC6BdPhU3819gHkQA6cbsF0+IbD0hsPozm5V7YwxV0AAAAASUVORK5CYII=","PlaceImage2":null,"PlaceImage3":null,"PlaceImage4":null}},
Activities:{{"ActivityId":"1","ActivityName":"zulfu livaneli","ActivityDescription":"Zulfu konseri","ActivityType":"1","ActivityDate":"2011-00-00","PlaceName":"Alinin Yeri","PlaceId":"1"},
{"ActivityId":"2","ActivityName":"Bir tat bir doku","ActivityDescription":"Y?lmaz Erdo?an oyunu","ActivityType":"3","ActivityDate":"2013-00-00","PlaceName":"Alinin Yeri","PlaceId":"1"},
{"ActivityId":"3","ActivityName":null,"ActivityDescription":"Y?l?n son derbisi","ActivityType":"2","ActivityDate":"2012-00-00","PlaceName":"Alinin Yeri","PlaceId":"1"}}]
and my php code:
$showresult = mysql_query($sqlQuery1);
$showresult1 = mysql_query($sqlQuery2);
$multi_array = array();
$multi_array1 = array();
while($row = mysql_fetch_assoc($showresult)){
$multi_array[] = $row;
}
while($row = mysql_fetch_assoc($showresult1)){
$multi_array1[] = $row;
}
$result = array_merge($multi_array1, $multi_array);
print json_encode($result);
EDIT
Or is there any method to do what i want?
Break up your code into two parts. One part gets the "Places" rows from the database, and the second gets the "Activties" rows from the database. Then you do something like
$php_result=array(
"Places"=> $places_rows,
"Activities"=> $activities_rows
);
$json_result= json_encode($php_result);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
After trying for a long period of time I am a bit stuck... I want to echo a value (url) in a json array with PHP and get it into a variable.
PHP:
$coverphotos = $facebook->api('http://graph.facebook.com/' . $album['id'] . '/picture?type=album&redirect=false&access_token=' . $access_token);
JSON:
{
"data": {
"url": "http://photos-c.ak.fbcdn.net/hphotos-ak-ash4/480080_567808343263483_176928672_a.jpg"
}
}
I want to get the actual url into a php variable... how would I go about doing this?
HOLY THAT TOOK LONG ENOUGH BUT I FIGURED IT OUT:
$coverphotos2 = $coverphotos['id'] . '?type=album&redirect=false&access_token=' . $access_token;
$content = file_get_contents($coverphotos2);
$photoarray = json_decode($content, TRUE);
$coverurl = $photoarray['data']['url'];
From the $coverphotos variable, you can store the URL of the picture with:
$picture = $coverphoto['id'];