Large data insertion - php

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. ;)

Related

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!

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.

Issue getting info out of array

I'm trying to get info out of this information:
Array (
[result] => success
[totalresults] => 1
[startnumber] => 0
[numreturned] => 1
[tickets] => Array (
[ticket] => Array (
[0] => Array (
[id] => 7
[tid] => 782755
[deptid] => 1
[userid] => 39
[name] => Mark Lønquist
[email] => mark.loenquist#outlook.com
[cc] =>
[c] => 79rzVBeJ
[date] => 2013-04-25 16:14:24
[subject] => test
[status] => Open
[priority] => Medium
[admin] =>
[attachment] =>
[lastreply] => 2013-04-25 16:14:24
[flag] => 0
[service] =>
)
)
)
)
The results are printed using:
print_r($results);
Usually, I've been able to do a simple:
$var = $results['something'];
To get it out, but it wont work with this :( Any help is appreciated.
After reformatting the array you pasted, it becomes clear that some elements are nested several levels deep. (It's a "multidimensional array"; see example #6 in the docs.) In those cases, you have to add additional brackets containing each successive key to reach the depth you want. For example, a sample from your $results array:
Array (
[result] => success
[totalresults] => 1
...
[tickets] => Array (
[ticket] => Array (
[0] => Array (
[id] => 7
[tid] => 782755
...
)
)
)
)
You simply need to do $results['totalresults'] to access "totalresults", but to get "tid" you would need to use $results['tickets']['ticket'][0]['tid'].
If you want to get "tid" from all of the tickets when there are multiple, you will have to iterate (loop) over the array of tickets. Probably something like this (untested, but should be close enough for you to figure out):
foreach ($results['tickets']['ticket'] as $ticket) {
echo $ticket['tid'];
}
To see what the problem is with your print_r() you may add error_reporting(E_ALL); to the top of your code.
Note that if you want to retrieve the value for a key such as 'totalresults' then $results['totalresults'] would be sufficient.
However, if you want to get a key from one of the nested arrays such as email then you would have to use $results['result']['tickets']['ticket'][0]['email'].

PDOStatement::getColumnMeta returns original table name instead of view name

I am using the CakePHP framework. When returning the results of a query, the framework calls the "experimental" PDOStatement::getColumnMeta to "arrayify" the data when it comes back from the database. However, there are mixed results depending on the query.
There are times when the array of data comes back as expected where all columns are associated to the name of the view. Other times, the data comes back mixed, where some of the data sits in an array associated with the original table that corresponds to the view.
// correct
Array(
[MyInstall] => Array
(
[id] => a6d1342a-7b4d-11e1-8397-60195b7d6275
[user_id] => dc038c9e-7b4b-11e1-8397-60195b7d6275
[script_id] => 057de1e0-7b48-11e1-8397-60195b7d6275
[path] =>
[url] =>
[created] => 2009-06-15 12:43:30
[version] => 3.2.1
[admin_url] => wp-admin
[name] => WordPress
[icon] => icon_WordPress.gif
)
)
//incorrect
Array(
[MyInstall] => Array
(
[id] => c71a2368-7b4d-11e1-8397-60195b7d6275
[user_id] => dc038c9e-7b4b-11e1-8397-60195b7d6275
[path] =>
[url] =>
[created] => 2011-11-07 22:26:38
[version] => 3.2.1
[admin_url] => wp-admin
)
[Script] => Array
(
[script_id] => 057de1e0-7b48-11e1-8397-60195b7d6275
[name] => WordPress
[icon] => icon_WordPress.gif
)
)
The way the results are built is from the results of the PDOStatment::getColumnMeta. Here is what a sample result of getColumnMeta looks like:
Array
(
[native_type] => STRING
[pdo_type] => 2
[flags] => Array
(
[0] => not_null
)
[table] => MyInstall
[name] => id
[len] => 108
[precision] => 0
)
Any suggestions on how I can get this same information using PDO for MySQL? Or is there another solution to this problem?
BTW: I already filed a bug with the PHP folks on this.
As it turns out, this is a now known bug in MySQL: http://bugs.mysql.com/bug.php?id=66794, still pending at the time of writing.

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