Convert var_dump of array back to array variable
var_export
or serialize
is what you're looking for. var_export
will render a PHP parsable array syntax, and serialize
will render a non-human readable but reversible "array to string" conversion...
Edit Alright, for the challenge:
Basically, I convert the output into a serialized string (and then unserialize it). I don't claim this to be perfect, but it appears to work on some pretty complex structures that I've tried...
function unvar_dump($str) {
if (strpos($str, "\n") === false) {
//Add new lines:
$regex = array(
'#(\\[.*?\\]=>)#',
'#(string\\(|int\\(|float\\(|array\\(|NULL|object\\(|})#',
);
$str = preg_replace($regex, "\n\\1", $str);
$str = trim($str);
}
$regex = array(
'#^\\040*NULL\\040*$#m',
'#^\\s*array\\((.*?)\\)\\s*{\\s*$#m',
'#^\\s*string\\((.*?)\\)\\s*(.*?)$#m',
'#^\\s*int\\((.*?)\\)\\s*$#m',
'#^\\s*bool\\(true\\)\\s*$#m',
'#^\\s*bool\\(false\\)\\s*$#m',
'#^\\s*float\\((.*?)\\)\\s*$#m',
'#^\\s*\[(\\d+)\\]\\s*=>\\s*$#m',
'#\\s*?\\r?\\n\\s*#m',
);
$replace = array(
'N',
'a:\\1:{',
's:\\1:\\2',
'i:\\1',
'b:1',
'b:0',
'd:\\1',
'i:\\1',
';'
);
$serialized = preg_replace($regex, $replace, $str);
$func = create_function(
'$match',
'return "s:".strlen($match[1]).":\\"".$match[1]."\\"";'
);
$serialized = preg_replace_callback(
'#\\s*\\["(.*?)"\\]\\s*=>#',
$func,
$serialized
);
$func = create_function(
'$match',
'return "O:".strlen($match[1]).":\\"".$match[1]."\\":".$match[2].":{";'
);
$serialized = preg_replace_callback(
'#object\\((.*?)\\).*?\\((\\d+)\\)\\s*{\\s*;#',
$func,
$serialized
);
$serialized = preg_replace(
array('#};#', '#{;#'),
array('}', '{'),
$serialized
);
return unserialize($serialized);
}
I tested it on a complex structure such as:
array(4) {
["foo"]=>
string(8) "Foo"bar""
[0]=>
int(4)
[5]=>
float(43.2)
["af"]=>
array(3) {
[0]=>
string(3) "123"
[1]=>
object(stdClass)#2 (2) {
["bar"]=>
string(4) "bart"
["foo"]=>
array(1) {
[0]=>
string(2) "re"
}
}
[2]=>
NULL
}
}
How to create an array from output of var_dump in PHP?
Use var_export if you want a representation which is also valid PHP code
$a = array (1, 2, array ("a", "b", "c"));
$dump=var_export($a, true);
echo $dump;
will display
array (
0 => 1,
1 => 2,
2 =>
array (
0 => 'a',
1 => 'b',
2 => 'c',
),
)
To turn that back into an array, you can use eval, e.g.
eval("\$foo=$dump;");
var_dump($foo);
Not sure why you would want to do this though. If you want to store a PHP data structure somewhere and then recreate it later, check out serialize() and unserialize() which are more suited to this task.
How to convert output of var_dump to array or variable?
Simply you have to write following it will return array.
$data = $datetime1->diff($datetime2); //Return Object
$data = (array) $datetime1->diff($datetime2); //Return Array
Object Output
$data->y = 0 //year
$data->m = 11 //month
$data->d = 1 //day
$data->h = 1 //hour
$data->i = 8 //minute
$data->s = 33 //second
Array Output
$data['y'] = 0 //year
$data['m'] = 11 //month
$data['d'] = 1 //day
$data['h'] = 1 //hour
$data['i'] = 8 //minute
$data['s'] = 33 //second
Demo
Save var_dump output array objects into a variable
First, assign the result of getMarketSummary
to a variable. That way you'll be able to do other things with the result without calling that function again.
$result = $b->getMarketSummary($market);
Then, the result is just an object inside an array. Based on the method name, it looks like you would only expect one object in the array, so you shouldn't need to loop. Just use array and object notation to refer to the value you want.
$ask = $result[0]->Ask;
If I'm mistaken and it does return an array with more than one object, you can loop over it and do the same basic thing.
foreach ($result as $item) {
$ask = $item->Ask;
echo $ask; // or whatever you're doing with it
}
Convert string (which is array) back to array
I think I've got it, it's stored wrong into the database. It should be stored as json_encode($data)
, and retreived as json_decode($data, true)
.
But an other way is to store it as serialize($data)
and retreive it with unserialize($data)
var_dump array returning my string, double?
You need to save onlye the data you need in the Session
$_SESSION['id'] = $id['id'];
$_SESSION['naam'] = $naam['name'];
In your case you are saving the entire result from the function mysqli_fetch_assoc
, which is an array, as you can see from the docs:
http://php.net/manual/en/mysqli-result.fetch-assoc.php
Unexpected observation: var_dump() of an array is flagging referenced elements... since when?
Not sure if this answers your question, but you can use
debug_zval_dump($array);
to get the refcount:
array(4) refcount(2){
[0]=> long(1) refcount(1)
[1]=> &long(2) refcount(2)
[2]=> long(3) refcount(1)
[3]=> &long(4) refcount(2)
}
Also see this Article by Derick Rethans (PHP Core Dev) about Refcounting.
How to read var_dump in order to know how to load its variable contents in to an array with a foreach?
Before we can decipher it, we have to format it.
Tobias Kun's answer shows a very good way to format the var_dump
output so you can read it.
object(stdClass)#1 (2)
{
["noun"]=>
object(stdClass)#2 (1)
{
["syn"]=> array(24)
{
[0]=> string(12) "domestic dog"
[1]=> string(16) "Canis familiaris"
[2]=> string(5) "frump"
[3]=> string(3) "cad"
[4]=> string(7) "bounder"
[5]=> string(10) "blackguard"
[6]=> string(5) "hound"
[7]=> string(4) "heel"
[8]=> string(5) "frank"
[9]=> string(11) "frankfurter"
[10]=> string(6) "hotdog"
[11]=> string(7) "hot dog"
[12]=> string(6) "wiener"
[13]=> string(11) "wienerwurst"
[14]=> string(6) "weenie"
[15]=> string(4) "pawl"
[16]=> string(6) "detent"
[17]=> string(5) "click"
[18]=> string(7) "andiron"
[19]=> string(7) "firedog"
[20]=> string(8) "dog-iron"
[21]=> string(8) "blighter"
[22]=> string(5) "canid"
[23]=> string(6) "canine"
[24]=> string(5) "catch"
}
}
}
You have a stdClass object with a property called ,"noun". noun is an abject with a property called "syn", which is an array of strings.
Suppose we call the object $object
. Then we can access the array like:
echo $object->noun->syn[23];
which gives us canine
. So a loop might look like:
foreach($data->noun->syn as $value) {
echo $value . "<br>\n";
}
How can I capture the result of var_dump to a string?
Use output buffering:
<?php
ob_start();
var_dump($someVar);
$result = ob_get_clean();
?>
php var_dump() vs print_r()
The var_dump
function displays structured information about variables/expressions including its type and value. Arrays are explored recursively with values indented to show structure. It also shows which array values and object properties are references.
The print_r()
displays information about a variable in a way that's readable by humans. array values will be presented in a format that shows keys and elements. Similar notation is used for objects.
Example:
$obj = (object) array('qualitypoint', 'technologies', 'India');
var_dump($obj)
will display below output in the screen.
object(stdClass)#1 (3) {
[0]=> string(12) "qualitypoint"
[1]=> string(12) "technologies"
[2]=> string(5) "India"
}
And, print_r($obj)
will display below output in the screen.
stdClass Object (
[0] => qualitypoint
[1] => technologies
[2] => India
)
More Info
- var_dump
- print_r
Related Topics
Convert Var_Dump of Array Back to Array Variable
How to Convert Many Statement MySQL to Laravel Eloquent
Safely Catch a 'Allowed Memory Size Exhausted' Error in PHP
Upstream Sent Too Big Header While Reading Response Header from Upstream
Easiest Form Validation Library For PHP
Path of Assets in CSS Files in Symfony 2
Do I Have to Guard Against SQL Injection If I Used a Dropdown
Warning: Cannot Modify Header Information - Headers Already Sent by Error
Best Way to Check If MySQL_Query Returned Any Results
How to Get Current Route in Symfony 2
How to Include Wordpress Functions in Custom .PHP File
Upload Video Files Via PHP and Save Them in Appropriate Folder and Have a Database Entry
How to Get Everything After a Certain Character
Verify Receipt For in App Purchase