How do I access this array using var_dump? PHP - 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.

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 printing a text variable from a curl function

I have a curl function which outputs the following text from an api.
number=1&id=731&name=test&value=6311
How could I make a variable which will store one of these values in the text such as I want to make a variable called $name which will have the 'test' value from the output of the api. I saw different things such as in json you can use $variable['name'] but it doesn't seem to work like this.. help
Check this:
Parse query string into an array
Use parse_str function, it looks like:
$get_string = "number=1&id=731&name=test&value=6311";
parse_str($get_string, $get_array);
print_r($get_array);

How to get first part of a URL

can anyone help me to piece together the puzzled I'm facing. Lets say I have url's
/some-work/
/store/bread/alloy/
and in both of these cases I wanna fetch the first part from it. i.e. some-work, store.
Now I've used parse_url(get_permalink()) to get the array of the url and then fetch the path index of the array to fetch the above string. Now I have also checked strstr PHP function, but I am unable to make it work. Can anyone help?
You can use explode, array_filter and current function like as
$url = "http://www.example.com/some-work/";
$extracted = array_filter(explode("/",parse_url($url,PHP_URL_PATH)));
echo current($extracted);//some-work
Demo

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.

Categories