Redis with multidimensional array in php - 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));

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.

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.

PHP Restore an Array from a String

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.

Complicated PHP transpose / pivot

I have the following array :
Array
(
[0] => Array
(
[Name] => first_data
[building] => A
[apt] => 16
)
[1] => Array
(
[Name] => first_data
[building] => B
[apt] => 16
)
[2] => Array
(
[Name] => second_data
[building] => A
[apt] => 17
)
[3] => Array
(
[Name] => second_data
[building] => B
[apt] => 18
)
and I need it to be returned as :
Array
(
[0] => Array
(
[Name] => first_data
[A] => 16
[B] => 16
)
[1] => Array
(
[Name] => second_data
[A] => 17
[B] => 18
)
Any ideas?
BTW the first array has hundreds of entries (not only first_data, but second and etc...) plus it has more than A and B.
Thanks in advance.
Not exactly what you want, but if you instead index the new array by the name, you can do this very easily. If the index number is some kind of ID, you can just create a field for it
foreach ( $oldarray as $index => $piece )
{
$newarray[$piece['Name']] = array($piece['building'] => $piece['apt'])
}
This will give you
Array
(
['first_data'] => Array
(
['A'] => 16,
['B'] => 16
)
['second_data'] => Array
(
['A'] => 17,
['B'] => 18
)
)
Since you have two entries with the same new, when you hit the 2nd loop, it will simply add the other building name. If you can work with this layout, then your solution is very easy, it will take more steps to do it exactly as you showed. If you absolutely have to do it the way you showed, you need extra code to loop through the new array, find the building name, add the key in the correct place, but this will be slower if you have a large amount of data.
In my opinion, the way I presented it is a far easier way to look around the array too. If you wanted to know the apt value for A in "second_data" you can just do
$newarray['second_data']['A']
with your array layout, it would require a loop to search the array for "second_data" because you have no idea where it is.

How to store double array data into string in php?

I have data in double array in php. Now I need to store that array data into string. How can I do that in php? I am new to php. Please help me in this regard.
Array is as follows:
Array ( [0] => Array ( [id] => 100 [type] => test1 [count] => 8 [count2] => 8)
[1] => Array ( [id] => 103 [type2] => test2[count2] => 3 [count3] => 3 ))
You can use implode() to convert an array into a string: http://php.net/manual/en/function.implode.php

Categories