PHP Restore an Array from a String - php

I have an array which is very complicated to generate.
Hence, would like to generate it once and then store as a string in a database.
Then, whenever it is needed, it can be simply retrieved from the DB as a string and declared as an array. Is there an easy way to do such a thing on PHP without using the eval() function.
So the string in the db might be something like
Array
(
[0] => Array
(
['none'] => Array
(
['none'] => Array
(
['Page'] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
)
)
)
[1] => Array
(
['Volume 1'] => Array
(
[] => Array
(
['Page'] => Array
(
[27] => 18
[28] => 19
[29] => 20
)
)
)
['Volume 2'] => Array
(
[] => Array
(
['Page'] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
)
)
)
)
)

You can use the json_encode and json_decode functions. They will serialize an array into a JSON string or deserialize a valid JSON string to an array respectively.
Using serialize/unserialize functions might not always be a good idea, because they come with security issues. Code might get executed upon deserialization which you had not intended. If no outside users have write access to the serialized objects, it should be fine though.

Yup, use serialize and unserialize functions.

Related

php: converting an array of objects to pure array

following array is received at server side :
[{"id":"2","foo":"bar","children":[{"id":"4","foo":"baz","children":[{"id":"6"}]},{"id":"5"}]},{"id":"7"},{"id":"3"}]
is there any way to convert it to a nested array like this?
[
['id' => 2, 'foo' => 'bar', 'children' =>[
'id'=> 4, 'foo' => 'baz' ....
P.S: i find out that without using any function i will have desired code format on server side. maybe this is because of sending data as post request (using ajax) that convert data to an array then serialize it and send , then on server side i have a nice array the same as what i'm looking for.Im not sure it is ralated to php or laravel or jquery !?
Array
(
[0] => Array
(
[id] => 2
[children] => Array
(
[0] => Array
(
[id] => 4
[children] => Array
(
[0] => Array
(
[id] => 6
)
)
)
[1] => Array
(
[id] => 5
)
)
)
[1] => Array
(
[id] => 7
)
[2] => Array
(
[id] => 3
)
)
To get the array in PHP, Yes. Use json_decode().
To get the "nested array like this" in JSON, No. This is not how arrays in JSON are supposed to look. See the specification at json.org, it is really easy to understand.

deseralize php Json in vb.net

I trying to get a array in VB.NET, but I have troubles for deserialize, I don't know if my format is bad or what, but first the data is a std object
Array
(
[0] => stdClass Object
(
[id] => 6797892
[marca] => xxx
[details] => yyy
[price] => rrr
[info] => Array
(
[0] => stdClass Object
(
[Items] => Array
(
[0] => stdClass Object
(
[Por] => 1
[$$hashKey] => 03F
)
)
[Tipo] => mouse
[price] => 1.65
[$$hashKey] => 03D
)
[1] => stdClass Object
(
[Items] => Array
(
[0] => stdClass Object
(
[Por] => o
[$$hashKey] => 03J
)
)
[Tipo] => teclado
[price] => 1.65
[$$hashKey] => 03H
)
)
[$$hashKey] => 03B
)
)
next i use json_encode(in php):result is:
[{"Id":"6797904","marca":"xxx","Pais":"yyy","Liga":"rrr","Jornada":"3","info":[{"Items":[{"Por":"1","Cuota":"2.25","$$hashKey":"03I"}],"Tipo":"mouse","price":2.25,"$$hashKey":"03G"}],"$$hashKey":"03E"}]
and using
....
file_put_contents($file, print_r($current, true) );
...
I save it in items.txt, and I load it in vb.net, but I don't know what is the correct way to convert into an array using:
Dim str As JArray = JArray.Parse(TextBox1.Text)
Dim results As Object = str("Id").ToString
To put in it an answer format.
The use of print_r is not correct here.
...
file_put_contents($file, print_r($current, true) );
...
The definition for print_r as per php.net:
print_r — Prints human-readable information about a variable.
This means extra characters are added to make it readable to humans, but not for machines. It even creates invalid JSON, causing VB.NET to generate an error.
Update your code to
...
file_put_contents($file,$current);
...
And it should work

json_encode does not work with strings as indexes

When I have an array like this :
Array (
[0] => Array ( [0] => 1 [1] => 12 [2] => Essener [3] => 1 )
[1] => Array ( [0] => 2 [1] => 12 [2] => Dinkel Spezial [3] => 0.2 )
[2] => Array ( [0] => 1 [1] => 1 [2] => Essener [3] => 1 )
)
and I use json_encode and echo it, I get this:
[["1","12","Essener","1"],["2","12","Dinkel Spezial","0.2"],["1","1","Essener","1"]]
which is good for me.
Now I have an array with stdClass Objects, which I wasn't able to transform into JSON with json_encode. When I echo it, it just doesn't show anything.
Then I transformed this array with objects to an array like this (with get_object_vars()):
Array (
[0] => Array (
[item_id] => 1
[item_name] => Essener
)
[1] => Array (
[item_id] => 2
[item_name] => Dinkel Spezial
)
[2] => Array (
[item_id] => 3
[item_name] => Saatenbrot
)
)
and when I use json_encode and echo it still doesn't show anything. Can anyone tell me what I am doing wrong or what I need to do to get a JSON array?
I need this json array to send data to an IOS App.
From the documentation:
Note:
In the event of a failure to encode, json_last_error() can be used to determine the exact nature of the error.
So you can try to detect the exact error by yourself. From your information I can't see any error.
Furthermore I don't think, it doesn't return anything. Try to var_dump() the result of json_encode(). I assume it returns false, which means an error occured.
So if anybody wondered what was wrong,
the problem was that i had "ä,ü,ö,ß" in my array and i needed to convert these to unicode and then everything worked just fine.

Mustache not iterating over array

I don't know what is going on (maybe it because it is the 12th consecutive hour working).
I always used this notation but now it just doesn't work (I already checked several time that the data is passed to this template I'm talking about).
Data is not iterated and the result is empty.
Mustache code:
{{#ad}}
setInput("{{key}}", "{{value}}");
{{/ad}}
I tried also:
{{#ad}}
setInput("{{key}}", "{{value}}");
{{/ad}}
The data passed is the following:
Array
(
[ad] => Array
(
[0] => Array
(
[key] => id
[value] => 1
)
[1] => Array
(
[key] => created_on
[value] => 1371464401
)
[2] => Array
(
[key] => updated_on
[value] =>
)
[3] => Array
(
[key] => dealer_id
[value] => 1
)
)
)
Solved: be careful to not pass hashes instead of "plain" arrays!! Even if it seemed to be a common array because of the indexing (0 => "a", 1 => "b") it was actually an hash! So just return the malicious data within an array_values($data) to fix it!

Redis with multidimensional array in php

I'm trying to use redis with multidimensional array by using HMSET.
My array looks like this.
Array
(
[t] => Hello
[a] => This
[c] => key
[b] => 23
[data] => Array
(
[1] => some value
[more] => value
)
)
Is there any way I can store data in this format in the redis using predis library.
A better way to do this is by json_encode your array in PHP and store it as set in Redis
$string = json_encode(Array
(
[t] => Hello
[a] => This
[c] => key
[b] => 23
[data] => Array
(
[1] => some value
[more] => value
)
));
Yes, Redis is able to store strings and PHP is able to serialize (multidimensional) array to strings.
The serialize function in the example can be used for that exact job, but is only exemplary, you can use any serialization method that serializes your data into a (binary) string.
For example you can make use of JSON (json_encode), XML or in some cases just implode might be fitting.
$string = serialize(Array
(
[t] => Hello
[a] => This
[c] => key
[b] => 23
[data] => Array
(
[1] => some value
[more] => value
)
));
$cmdSet = $redis->createCommand('set');
$cmdSet->setArgumentsArray(array('thisispredisdoingredis', $string));

Categories