JSON Encode specific datatypes - php

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?

Related

CONVERT PHP ARRAY TO JSON (empty body)

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)

PHP search/filter array with given conditions, recursively

I am getting data from an api (that I cannot query agains, just get lump of data), and then I need to query against those data like I would do using database. Only It would be great if I could do it recursively.
Data example
[0] => Array
(
[id] => 1
[url] => https://domain.com/api/1.0/item/1/
[name] => some_item
[category] => some category
[created_by] => Array
(
[id] => 1
[screen_name] => tomino
)
[current_user_domain_access] => Array
(
[is_active] => 1
[is_administrator] => 1
)
[alerts_enabled] => 0
)
(much shortened version)
I receive an array of objects like that and then I need to select/filter/search by values.
Something like this
SomeModel::find(['category'=>'some category','current_user_domain_access' => ['is_administrator' => 1]]);
Is that something that would be possible in PHP? I was thinking about flattening the array, but then there might be key conflicts
1) select data : You can select data by (array_name->id),(array_name->url) and so on...
2)Filter : add conditions according to requirement
3)search : in_array(),array_search

PHP - Access JSON data value in array

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"] );.

How can I turn an array to csv file in php

One more beginner question:
how can I turn an array (actually it's just a text) like that:
Array
(
[0] => Array
(
[0] => nsor#cg.ukrtel.net
[1] => p2007#rambler.ru
[2] => pan20072009#yandex.ru
[3] => gf#ukr.net
[4] => tmkp#ma.odessa.ua
[5] => export#soh.by
[6] => advert#soh.by
)
)
into a .csv file by means of PHP? This file should be without things like this "[0] =>", only addresses.
Assuming your array is in a variable called $arr, you could use
echo implode("\r\n",$arr);
And you might want to prefix it with
header('Content-type: text-csv; charset="UTF-8"');
or similar.
Look at the first example at http://php.net/manual/en/function.fputcsv.php
Use your array instead of the array in the example.

Can I turn an array in memory into the code representation of that array?

I'm not even entirely sure how to ask the question, but assuming I have an array in memory like this:
Array (
[0] => Array
(
[0] => Array
(
[0] => 18451
[1] => MDX
)
[1] => Array
(
[0] => 18450
[1] => NSC
)
[2] => Array
(
[0] => 18446
[1] => RL
)
)
)
Is there some existing functionality to turn it into the code version of that array? I have a # of arrays I need to do this for, nested to various degrees. So I imagine I want output something like
$arrayname[] = array(array('18451','MDX'),array('18450','NSC'),array('18446','RL'));
I can write something to do it, but I'd rather not recreate the wheel if there's an existing way to do it.
This may be all you need:
http://us.php.net/var_export
manually loop through the array recursively and create a string like how you want.
var_export could help you there i think. The other option would be looping through it manually :(

Categories