i have no content to show even i use this code ,
$result=mysqli_query($con,$sql);
$indexCourses=[];
$listCourses=array();
$listCourses=$result->fetch_all(MYSQLI_ASSOC);
mysqli_close($con);
echo json_encode($listCourses,TRUE);
the output of my array with print_r() is :
Array ( [0] => Array ( [id] => 6 [nom] => Marathon de Marrakech [date] =>
28/01/2018 ) [1] => Array ( [id] => 7 [nom] => Semi marathon Meknès [date]
=> 18/02/2018 ) [2] => Array ( [id] => 8 [nom] => 10 km de Casablanca
(ENSEM) [date] => 25/02/2018 ) )
any help please
Judging by your data json_encode probably throws and error when trying to encode non-UTF8 characters. You can check if the error is occurred by using json_last_error_msg function. Additionally, please read on how to use json_encode function (specifically second argument specs), because I think you don't need to pass the second argument to the function in your case.
The solution to your problem might be quite simple - when creating mysqli connection use UTF8 encoding, which will force all the results to be returned in UTF8 as well, in which case json_encode will not have the problem.
mysqli_set_charset($connection,"utf8");
(taken from https://www.w3schools.com/php/func_mysqli_set_charset.asp)
Related
I am trying for get one field value from json response but I am unable to get it. My PHP code is like below
$servers = $sp->server_list();
print_r ($servers);
Its giving me result like below
stdClass Object ( [data] => Array ( [0] => stdClass Object ( [id] => S9rr4Un0SYlPGR6E [name] => localhost [autoupdates] => 1 [firewall] => [deny_unknown_domains] => [lastconn] => 1544645254 [lastaddress] => 50.116.20.23 [datecreated] => 1511884238 [plan] => grandfathered_coach [available_runtimes] => Array ( [0] => php5.4 [1] => php5.5 [2] => php5.6 [3] => php7.0 [4] => php7.1 [5] => php7.2 [6] => php7.3 ) ) ) )
I want get value called id from it and want use it in my php. I have tried json decode method like below
$decoded_data = json_decode($servers,TRUE);
$myid = $decoded_data['data']['id'];
its giving me error like below
Warning: json_decode() expects parameter 1 to be string, object given in C:\xampp\htdocs\code\new2.php on line 11
I have searched many questions and answer here but I am unable to get it resolved. Let me know if someone can help me. Thanks a lot.
$servers is an object with key data which is an array. You can access the first element in the array using index 0 then you can get the id.
Json decode is not needed as $servers is already an object. You use json decode to go from json string to object. You can learn more about PHP JSON decode
$servers = $sp->server_list();
echo $myId = $servers->data[0]->id;
I have a multi-dimensional array and php seems to be returning an array instead of a value when I attempt to access the values directly. What am I doing to cause this?
The array looks like (via print_r):
Array (
[12] => Array ( [2016] => 93083.00 [2015] => 85367.00 [2014] => 69726.00 )
[11] => Array ( [2016] => 66730.00 [2015] => 65548.00 [2014] => 77936.00 )
[10] => Array ( [2016] => 84602.00 [2015] => 112070.00 [2014] => 102104.00 )
)
I'm trying to access values using $arrayname[12][2016] but it is returning Array[2016] instead of 93083.
Is this a simple syntax mistake? Or am I missing part of the concept here? I've been trying to work this problem for hours so maybe I'm missing a simple explanation.
EDIT: the syntax above is actually correct, the issue was in the data entry: I was trying to access a key that didn't exist. I tried to delete the post, but can't since it has been answered.
$arrayname[12] = [2016=>93083.00, 2015=> 85367.00 ]
...
...
echo $arrayname[12][2015] ; // prints 85367
i think your array has one more level. try $arrayname[12][2016][2016] .
I have a PHP Array which looks like this:
[0] => Array
(
[label] => Standard Stop
[code] => 5
[excludeIF] => [miniTrack, isTriangleHanger, tubeTrack, boxTrack]
)
[1] => Array
(
[label] => Soft Stop With Lag
[code] => 7
[excludeIF] => [miniTrack, isTriangleHanger, tubeTrack, boxTrack]
)
When I do a json_encode it generates something like this:
{"label":"Standard Stop","code":"5","excludeIF":"[miniTrack, isTriangleHanger, tubeTrack, boxTrack]"}
The problem with that encode is that it's making the code a string and it's also making the excludeIF a string.
code needs to be a float
excludeIF needs to be an array
Is something like that possible with JSON Encode?
I'm querying an API and getting a response back with various countries. Here is the relevant array I'm working with and what it prints out.
print_r($apiResponse['response']['data'][0]['countries']);
prints this:
Array ( [US] => Array ( [id] => 840 [code] => US [name] => United States [regions] => Array ( ) ) [CA] => Array ( [id] => 124 [code] => CA [name] => Canada [regions] => Array ( ) ) )
I am looking to save an array of only the two character country codes from that data. The only thing is the key is unknown to me when I query it so I don't know how to access the [code] section of it to save it to my new array.
I want to end up being able to take whatever amount of countries the API sends back and save the two character codes in a format like this:
'country_codes' => array('US','CA','UK','AU')
Thanks for your help!
Use the array_keys() function. Here you have the documentation.
I'm sorry if this is newbie question but I don't understand how to access the [ids] value in the JSON array via PHP.
Why is this not working?
$jsonResponse = json_decode($response,true);
print $jsonResponse[2]["ids"];
This is the JSON array:
Array
(
[0] => analytics#gaData
[1] => https://www.googleapis.com/analytics/v3/data/ga?ids=ga:123455&dimensions=ga:eventCategory,ga:eventAction,ga:eventLabel&metrics=ga:visits,ga:pageviews&start-date=2013-01-01&end-date=2020-01-01
[2] => Array
(
[start-date] => 2013-01-01
[end-date] => 2020-01-01
[ids] => ga:123455
[dimensions] => ga:eventCategory,ga:eventAction,ga:eventLabel
[metrics] => Array
(
[0] => ga:visits
[1] => ga:pageviews
)
[start-index] => 1
[max-results] => 1000
)
[3] => 1000
[4] => 9
There doesn't seem to be anything wrong with your code. Your code should be printing, what seems to me, the object ga:123455 if the code is executed. When printing this, this object will be converted to a string. PHP will do (string) ga:123455 (invoke the __toString() method on ga:123455) to convert an object to a string. If this method is absent, there must be a warning that ga:123455 cannot be converted to a string. __toString() might also return an empty string.
I would suggest debugging it by doing print var_dump( $jsonResponse[2]["ids"] ); and print var_dump( (string) $jsonResponse[2]["ids"] );.