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.
Related
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.
I have two array as following
Array
(
[0] => 641
[1] => 622
[2] => 100
[3] => 6431
)
Array
(
[0] => 64.1
[1] => 62.2
[2] => 10
[3] => 643.1
)
How can I make it as following
Array
(
[0] => 641
[1] => 64.1
[2] => 622
[3] => 62.2
[4] => 100
[5] => 10
[6] => 6431
[7] => 643.1
)
It's as simple as
$result=array_merge($array1,$array2);
Note: Your values wont be in the order you presented though. If that is important then you need to loop through your arrays to build a new array accordingly.
Ummm ok here is that version as well
if(count($array1)==count($array2))
{
for($i=0;$i<count($array1);$i++)
{
$result[]=$array1[$i];
$result[]=$array2[$i];
}
}
print_r($result);
Fiddle
Manual
you can use array_merge() function merges one or more arrays into one array.
example:
array_merge(array1,array2,array3...)
I have array structure like this,
Array
(
[1] => Array
(
[1] =>
[2] =>
[3] =>
[4] => Product
[5] => Product Name
..
[59] => Color
)
[2] => Array
(
[1] =>
[2] =>
[3] => 1
[4] => 9155
....
[59] =>
)
[3] => Array
(
[1] =>
[2] =>
[3] => 1
[4] => 9165
...
[59] => Green
)
[4] => Array
(
[1] =>
[2] =>
[3] =>
[4] =>
...
[58] =>
[59] =>
)
)
Its reading data from Excel sheets , the issue is when i read data from excel sheet it reads empty rows too, I already tried to ignore empty rows from the excel sheet some how its working (when the excel is created from MSexcel ) but from Google Drive its reading empty rows. So I would like to remove those rows with 1- 59 are blanks. in the above example array with index 4 .
Note that some index have missing values in many sub index but I don't want to remove those, only all sub indexes from 1-59 are blank then that main index (here its 4) needs to remove.
Is there any smart way to remove those array index that have empty values. I not like to iterate all the array and store to another.
if you want to remove the index 4 that is an empty array :
array_filter(array_map('array_filter', $array));
Use array_map
$array = array_map('array_filter', $array);
let try with array_filter
$entry = array(
0 => 'foo',
1 => false,
2 => -1,
3 => null,
4 => ''
);
print_r(array_filter($entry));
Array
(
[0] => foo
[2] => -1
)
Use array_filter..It will remove all empty values..
array_filter($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!
I have an array as follows
Array ( [0] => application [1] => modules [2] => Ride [3] => externals [4] => images [5] => uploads [6] => profile [7] => 116 [8] => 13006678321287904362.jpg )
How can I insert an item to the existing array with out overwriting the existing element in specified index
Consider I would like to create as follows
Array ( [0] => application [1] => modules [2] => Ride [3] => externals [4] => images [5] => uploads [6] => profile [7] => 116 [8] => model [9] => 13006678321287904362.jpg )
Any help please
To elucidate on array_splice:
array_splice($array, $position, 0, $insert);
Where $array is the current array, $position is where you want to add the new item and $insert is the item to add (can be a new array)
You can use array_slice, array_push and array_merge, I don't know if a better solution does exist.
This should work for you if you want to add it to the end of the array
<?php
$array[] = $var;
?>