how to convert a text into an associative array? - php

Hi this is the code i am following in my project.
$reports = $this->curl->simple_get('url');
echo is_array($reports)?"Is Array":"Not Array";exit;
It's giving Not Array as output.
I want to convert that into associative array.

The data you are getting is probably not an array, but a string containing an array structure, e.g. output by print_r(). This kind of data will not automatically be converted back into a PHP array.
To use this you can use a similar solution as brought out here:
Create variable from print_r output
it describes the print_r_reverse function that's brought out in php.net page.
how ever - this is kind of an ugly hack. I would suggest to change the page content and use json_encode() in the "url" page, and parse the content using json_decode()

Related

how can i get back an array written in a text file with print_r [duplicate]

When I print an object using print_r, is there any specific utility function in php to convert the output to an object?
There seems to be enough output to reconstruct the object, but I couldn't find the exact functions to accomplish this.
There is no automatic method to change print_r value back to an object. You are also missing important information like what type is stored at a key (string or number or ..)
You could use var_dump to get more detailed output that can be changed back to an object. But still this does not mean there is an automatic function for it.
Last you could use var_export ( http://php.net/manual/en/function.var-export.php ) to get valid PHP output you can use.
You could also use http://php.net/manual/en/function.serialize.php to pass objects around.

php json_encode() output changes based on the array indices

I was trying to get some values from MySQL database and when converting into JSON array using json_encode() I got a JSON object , after a while I found out that the indices was the root cause of the problem
here's the example I mentioned
<?php
$array = array(0=>"zero",2=>"two");
$another_array=array(0=>"zero",1=>"one");
print_r(json_encode($array)); // output: {"0":"zero","2":"two"}
print_r(json_encode($another_array)); //output: ["zero","one"]
?>
so what's the reason for that ?
Because array(0=>"zero",1=>"one") is the same as array("zero", "one") and the JSON encoder assumes the latter is a list (since that's how PHP does lists) and has no way to tell the former from the latter.
If you want to force json_encode to treat all arrays as objects, pass JSON_FORCE_OBJECT as the second argument:
json_encode(array(0=>"zero",1=>"one"), JSON_FORCE_OBJECT)
// {"0":"zero","1":"one"}
If you always want a json list instead, get rid of the keys (e.g. using array_values()) before encoding.

How do I access this array using var_dump? PHP

I would like to display the text 'hamsters repeat one'.
My array:
a:1:{i:0;s:19:"hamsters repeat one";}
Should I use var_dump for this? In PHP. I know how to get the array data out of the database (I am using get_post_meta a wordpress feature for this) .
Should it be something like this? :
var_dump $variablewhicharrayhasbeenstored;
Thank-you
Use unserialize($myvar) to decode that string into an array.

Parsing serialized Array (Wordpress)

I'm using wordpress and one of my meta key values is stored like this:
a:1:{i:0;s:8:"Religion";}
I'm trying to figure out the easiest way in PHP to parse this so I can extract "Religion" or really any of the elements in a clean manner.
Hope this makes sense - thanks!!!
Loren
that is a serialized array, use unserialize()
$array = unserialize('a:1:{i:0;s:8:"Religion";}');
echo $array[0];
This works in PHP 5.2 and above:
list($thatWord) = unserialize($metaKeyValue);
You then have the string "Religion" in $thatWord.
The string you have:
a:1:{i:0;s:8:"Religion";}
Is a serialized array. If you retrieve it from within wordpress, you would get an Array instead of a string. I assume you pull it from the database directly, so you need to unserializeDocs it your own.

pass multidimentional array from javascript to php

Im running a javascript code which reads values from different XML files and it generates a multidimentional array based on those values. Now I need to pass this array to a PHP page. I tried different but it always pass the arrray as string not as an array.
Anyone has an idea :( ... and thank you very much
What Caleb said.
Use this and JSON encode your JS array to a string, send it over to PHP and use json_decode to decode it into a PHP array.
You need a JSON encoder/decoder to do that. Prototype has it implemented by default and with jQuery you can use jQuery-JSON
For example if you use Prototype as your JS library then you can convert your array into a string like that:
var example_multi_dim_arr = {"a":[1,2,3], "b": [4,5,6]};
var string_to_be_sent_to_server = Object.toJSON(example_multi_dim_arr);
And in the PHP side (assuming that the JSON string is passed to the script as a POST variable)
$multi_dim_arr = json_decode($_POST["variable_with_json"], true);
The last true field in json_decode indicates that the output should be in the form of an array ($multi_dim_arr["a"]) and not as an object ($multi_dim_arr->a).
NB! the function json_decode is not natively available in PHP 4, you should find a corresponding JSON class if you are using older versions of PHP. In PHP 5 everything should work fine.

Categories