php: converting an array of objects to pure array - php

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.

Related

Pull a certain value from within a series of arrays

I'm pulling data from an XML file, converting it to json and then an array to be able to handle it.
The issue is because of the structure of the XML, it's like inception. Arrays inside of arrays inside of more arrays.
Array
(
[0] => ...
[3] => Array
(
[records] => Array
(
[record] => Array
(
[id] => 506
[sequenceNumber] => 1
[values] => Array
(
[value] => Array
(
[0] => Array
(
[picklistOptionId] => -1
[refId] => 230
[value] => **VALUE**
)
...
I want to be able to call the "**VALUE**" by the refId of 230. What would be the best way?
$arr[3]['records']['record']['values']['value'][0]['value']
There are downsides to having arrays of this many dimensions. e.g., they are less maintainable and (in some cases) slower.

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.

PHP merge array issues

I have an array as such
Array
(
[1] => Array
(
[0] => PrettyName1
[1] => PrettyName2
[2] => PrettyName3
)
[2] => Array
(
[0] => UglyURL1
[1] => UglyURL2
[2] => UglyURL3
)
)
I want to be able to put these into an array for a code-igniter template, well when I push and merge arrays I end up messing the whole thing up. Can someone show me the proper way to merge these arrays? I need something like
Array
(
PrettyName1 => UglyURL1
PrettyName2 => UglyURL2
PrettyName3 => UglyURL3
)
Maybe something like this array_combine($ar[1], $ar[2]) ?

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