Hierarchical string into PHP array - php

I have a string a[b][c] and I need to convert it into multidimensional PHP array. Does anybody knows a quick solution for that without using eval()?

Ok, I found solution for that:
parse_str("a[b][c]", $result);
Resulted multidimensional array is written into $result variable.
Source

Related

How to get a value in a multidimensional array using a string as multidimensional index?

if I have a string like this , $index_string="[1][3]";
How to get value in below array using above string,
$value_I_want_to_get_using_above_string = $multidimensional_array[$index_string];
You can do something like this using eval
$index_string="[1][3]";
$index_string =' $value_I_want_to_get_using_above_string = $multidimensional_array'.$index_string;;
eval($index_string);
Although as we all know eval() is dangerous especially if you are getting that string from another place.

Getting value from string by index

I have a string of data formatted like so:
[{"pr_a_w":"10","pr_a_we":"10","pr_c_w":"10","pr_c_we":"10"},{"pr_a_w":"20","pr_a_we":"20","pr_c_w":"20","pr_c_we":"20"},{"pr_a_w":"111","pr_a_we":"11","pr_c_w":"111","pr_c_we":"111"}]
The string doesn't have any index/numbers like a regular array would and I'm finding it difficult to extract individual values e.g. with a regular array I could use:
$string[0]["pr_a_w"]
To get the first instance of "pr_a_w" and I could use:
$string[1]["pr_a_w"]
To get the second instance etc.
Is it possible to get single values from this string based on their number?
What you have there is valid JSON (serialized array of objects), so you could use json_decode to translate the serialized data into a native PHP array:
$array = json_decode('[{"pr_a_w":"10","pr_a_we":"10","pr_c_w":"10","pr_c_we":"10"},{"pr_a_w":"20","pr_a_we":"20","pr_c_w":"20","pr_c_we":"20"},{"pr_a_w":"111","pr_a_we":"11","pr_c_w":"111","pr_c_we":"111"}]',true);
$array will then allow you to do exactly what you stated you'd like to do above.
$array[0]["pr_a_w"]; // will give you 10
$array[1]["pr_a_w"]; // will give you 10
Try like this, No need to access with array index. You will get error if you access wrong index.
$json_arr= json_decode('[{"pr_a_w":"10","pr_a_we":"10","pr_c_w":"10","pr_c_we":"10"},{"pr_a_w":"20","pr_a_we":"20","pr_c_w":"20","pr_c_we":"20"},{"pr_a_w":"111","pr_a_we":"11","pr_c_w":"111","pr_c_we":"111"}]',true);
foreach($json_arr as $row){
echo $row['pr_a_w']."<br>";
}

how to convert a text into an associative array?

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()

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.

Help editing JSON to make an array rather than a 'dictionary'

I currently have json using json_encode from a mysql query which looks like this:
{"post_2":{"caption":"...","id":"...","accountID":"..","date":"07\/07\/2011 1:45:12 AM","title":"...","authorInfo":{"Email Address":"..."}}}, {"post_2":{"caption":"...","id":"...","accountID":"..","date":"07\/07\/2011 1:45:12 AM","title":"...","authorInfo":{"Email Address":"..."}}}
How can I have the json being an array of posts ('post_2', 'post_1') rather than it being a dictionary? The JSON will be decoded on an iPhone using SBJSON and the JSON will have to be made into an array in the backend.
Thanks in advanced.
Provide a non-associative array to json_encode(). The easiest way is usually to simply call array_values() on the (associative) array, and encode the result.
Take a look at PHP's json_decode function, specifically the 2nd parameter if you want an array.

Categories