Multidimensional array in txt file to php array - php

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!

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.

Large data insertion

I have a csv file uploaded and data read into an array .The valid data is stored in the array
as associative array indexed from 0-4000 records
Array
(
[0] => Array
(
[uname] => uname1
[name] => fullname1
[email] => uname1#email.com
)
[1] => Array
(
[uname] => uname2
[name] => fullname2
[email] => uname2
)
[2] => Array
(
[uname] => uname3
[name] => fullname3
[email] => uname3#email
)
[3] => Array
(
[uname] => uname3
[name] => fullname3
[email] => uname3#email
)
..
...
[3999] => Array
(
[uname] => uname3
[name] => fullname3
[email] => uname3#email
)
)
How can i insert so many records because the $array is stored in variable when i click the submit the $array is reset to null.
How can I approach this without using database, any solution available?
Are you trying to say you're overflowing the 64KB limit of the <FORM> when you submit it? Make sure you're using method="POST" and encoding="multipart/form-data".
It's also possibly, but highly unlikely with 4000 records, that you're exceeding the post_max_size and upload_max_filesize parameters in your php.ini file. If you're secretly trying to post 50 million email addresses instead then that's likely your problem.
If that doesn't help, then you need to clarify what you're trying to accomplish, because we're left guessing. ;)

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.

Manipulating arrays in php

I have an file uploading site, it has an option of uploading through urls, what I am trying to do is whenever a user uploads through url, I check my database if a file exists that was uploaded through same url it displays the download url directly instead of uploading it again.
The data sent to uploading script is in array form like:
Array (
[0] => http://i41.tinypic.com/3342r93.jpg
[1] => http://i41.tinypic.com/28cfub7.jpg
[2] => http://i41.tinypic.com/12dsa32.jpg
)
and the array used for outputing the results is in form like this:
Array
(
[0] => Array
(
[id] => 43
[name] => 3342r93.jpg
[size] => 362750
[descr] =>
[password] =>
[delete_id] => 75CE
[upload_id] => 75F45CAE1
)
[1] => Array
(
[id] => 44
[name] => 28cfub7.jpg
[size] => 105544
[descr] =>
[password] =>
[delete_id] => D392
[upload_id] => 6676FD881
)
[2] => Array
(
[id] => 45
[name] => 12dsa32.jpg
[size] => 49000
[descr] =>
[password] =>
[delete_id] => 54C9
[upload_id] => A58614C01
)
)
Now I want is that if the link http://i41.tinypic.com/28cfub7.jpg is already upload I just add it to output array but maintain it in a order (if the link added was 2nd in array the output result should also show it in 2nd)
So what function should be used to remove the matched urls from input array and a function to add it output array in the order no.
// edited
Yes unset will do the thing but I want to maintain the order:
For example after unsetting the array looks like this:
Array (
[0] => http://i41.tinypic.com/3342r93.jpg
// [1] was removed
[2] => http://i41.tinypic.com/12dsa32.jpg
)
but the output array would be
Array
(
[0] => Array
(
[id] => 43
[name] => 3342r93.jpg
[size] => 362750
[descr] =>
[password] =>
[delete_id] => 75CE
[upload_id] => 75F45CAE1
)
// this will become [1], so how can i add another output[1] and shift other
// items after it to [2], [3] and so on...
[1] => Array
(
[id] => 45
[name] => 12dsa32.jpg
[size] => 49000
[descr] =>
[password] =>
[delete_id] => 54C9
[upload_id] => A58614C01
)
)
Well, you can add it to the output array by doing something like:
$OutArray[2] = $element;
Where $element is another Array with the id, name, size (etc...) elements.
As for removing from the array:
unset($OutArray[2]);
You may want to read Array (PHP manual).
If you have an indexed array, you can remove a value by doing:
unset ($array[2]);
If you want to add an item to an array, use this shorthand of array_push (you don't need to specify an index!):
$array[] = "new object";
All documentation is on php.net/arrays
Why don't use an if statement and/or file_exists() to see if the file is there. If you already have an array with the values then it just won't be uploaded again.

Categories