Convert a string array into php array object - php

I have a PHP string array that I would like to convert into a PHP array object.
string ->
"Array(
[0] => Array
(
[item_id] => 315428396
[title] => Luke Heith
[link] => somelinks
[rights] => Array
(
[0] => update
[1] => view
[2] => grant_view
[3] => delete
[4] => comment
[5] => add_task
[6] => subscribe
[7] => grant
[8] => add_file
[9] => add_conversation
[10] => rate
)"
Solution a PHP object array. Does someone know how I can achieve this?

That format is a debugging output format only. It is not possible to 100% convert this back into an actual array; you will always find edge cases where the input will represent ambiguous values. There's also no function to convert this back into an array; you'd have to write one yourself, which is something of an ambitious project.
In short: use a format which can actually be deserialised (like JSON or serialize), this one cannot.

Related

Convert form to json, submit as single form element, then convert back as multidimensional array

I am attempting to get around having to raise the max_input_vars parameter when submitting a large form. My thought was to jsonify the form, create a second form with a single field in it, put the jsonified string into that single field, submit, decode as an array, and then replace the POST array with the decoded data. I am plugging this into an existing system so for this to work I would need the new array to have the same structure as the raw post would. However, I am running into an issue where regardless of how I encode the data I am losing info and structure beyond the first depth of the array. I have tried both using this library to encode the form:
https://github.com/macek/jquery-serialize-object
As well as jQuery(form).serializeArray(), each of which delivers results that decode in their own unintended ways. This is what the raw post data looks like when I don't intercept the form and dump it with print_r($_POST):
Array
(
[closedpostboxesnonce] => 9d9dc8fa74
[meta-box-order-nonce] => 520ef5d263
[update-nav-menu-nonce] => cfea78920d
[_wp_http_referer] => /wp-admin/nav-menus.php
[action] => update
[menu] => 2
[menu-name] => Left Sidebar Menu
[save_menu] => Save Menu
[qtranslate-fields] => Array
(
[menu-item-title] => Array
(
[3871] => Array
(
[en] => Knowledge Center
[ja] => Knowledge Center
[ko] => Knowledge Center
[zh] => Knowledge Center
[qtranslate-separator] => [
)
The data when I use the jquery-serialize-object library above to encode the form data, then decode it with json_decode($_POST["allinputs"],true):
Array
(
[closedpostboxesnonce] => 9d9dc8fa74
[meta-box-order-nonce] => 520ef5d263
[update-nav-menu-nonce] => cfea78920d
[_wp_http_referer] => /wp-admin/nav-menus.php
[action] => update
[menu] => 2
[menu-name] => Left Sidebar Menu
[qtranslate-fields[menu-item-title][3871][en]] => Knowledge Center
[qtranslate-fields[menu-item-title][3871][ja]] => Knowledge Center
[qtranslate-fields[menu-item-title][3871][ko]] => Knowledge Center
[qtranslate-fields[menu-item-title][3871][zh]] => Knowledge Center
Using jQuery(form).serializeArray() to encode, and $allinputs = (array)(json_decode($_POST["allinputs"])); to decode:
Array
(
[0] => stdClass Object
(
[name] => closedpostboxesnonce
[value] => 9d9dc8fa74
)
[1] => stdClass Object
(
[name] => meta-box-order-nonce
[value] => 520ef5d263
)
[2] => stdClass Object
(
[name] => update-nav-menu-nonce
[value] => cfea78920d
)
Is there a way to get the data encoded and then decoded while retaining the original structure?
Edit: using #think-win-win 's suggestion of passing true as the second parameter of json_decode yields:
Array
(
[0] => Array
(
[name] => closedpostboxesnonce
[value] => 9d9dc8fa74
)
[1] => Array
(
[name] => meta-box-order-nonce
[value] => 520ef5d263
)
[2] => Array
(
[name] => update-nav-menu-nonce
[value] => cfea78920d
)
It is one of the ones that I tried, along with a couple of others, but I felt the post was getting too long to include them all. The above one loses the associativeness of the top level of the array, in addition to converting the deeper levels to:
[7] => Array
(
[name] => qtranslate-fields[menu-item-title][3871][en]
[value] => Knowledge Center
)
[8] => Array
(
[name] => qtranslate-fields[menu-item-title][3871][ja]
[value] => Knowledge Center
)

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.

Multidimensional array in txt file to php array

I'm developing a tool for a client, and I save the data in a txt file when using the tool offline, so that he can then upload this file onto server and save data in database.
The data in the text file are presented like below:
Array
(
[idcategorie] => 1
[idmasteruser] => 1
[societe] => Company
[marque] => Brand
[audit] => AUdit
[nom] => Baker
[prenom] => James
[phone] =>
[email] => some#some.com
[nboutils] => 3
[outil0] => Array
(
[id] => 20
[valeurs] => Array
(
[0] => 24
[1] => 4
[2] => 27
[3] => 16
)
[file] => /newbam/images-up/HopbV_chefs.jpg
[notes] => Array
(
[0] => 4
[1] => 5
[2] => 7
[3] => 6
)
)
)
How can I put this data in a PHP array that I can handle after using keys?
If you used var_dump to generate this data, restoring it back into an array is a non-trivial task. However, this is much easier if you export the data using var_export or serialize, which can be put back into an array.
You can serialize the array:
http://php.net/serialize
It writes the array in a format that can be saved in a file and parsed later as an object or an array with unserialize:
http://php.net/unserialize
Hope it helps!

Order a PHP array based on contents

I have an array in PHP which looks like this:
Array
(
[2] => post4.post
[3] => post7.post
[4] => post5.post
[5] => post3.post
[6] => post6.post
[7] => post1.post
[8] => post2.post
)
How could I arrange it so it could look like this?
Array
(
[0] => post7.post
[1] => post6.post
[2] => post5.post
[3] => post4.post
[4] => post3.post
[5] => post2.post
[6] => post1.post
)
The array has a list of the files contained in a folder. In my local server looks like the second example but on a standard server looks like the first one.
Thanks in advance :D
You are looking for rsort.
PHP has great documentation. I suggest you have a look at the array functions and the Sorting Arrays article (which can be easily find via Google ;))
rsort($array) will sort an array in reverse/descending order (from high to low).
For future reference use this page (http://php.net/manual/en/array.sorting.php) to identify the most suited array sorting method for your needs.
You don't want to use rsort because it will reassign array indexes. Instead use arsort().

cakephp save array

what would be the efficient way of saving the following array using php (cakephp)?
each value needs to go into a new row in the table?
Array
(
[0] => 6786754654
[1] => 5643564545
[2] => 344544545
[3] => 233245654654
[4] => 453454654654
[5] => 6546542323
[6] => 654654654
[7] => 645654654
etc....
)
thanks
2 choices:
Format the array as required by Model::saveAll()
Loop through the array calling Model::create(), then Model:save()
I'd recommend option 1 as you can use Model::saveAll($data, array('validate' => 'first')); to ensure that all values are valid before saving any of them.

Categories