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;
?>
Related
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);
This is my $part_array:
Array (
[0] => 1015789
[1] => 1029402
[2] => 1031345
[3] => 1036476
[4] => 1061512
[5] => 1065031
[6] => 1069892
[7] => 1070721
[8] => 1073222
[9] => 1074811
)
$next_index = $next_index + 1;
$next_part = ($part_array[$next_index]);
Retrieve part# information using MySQL
Display part# and related information on page. Click on button to display next part# and related information and repeat until end, or user exits.
Each time I increment the $next_index and get the $next_part, the $part_array has the next part # added to the end of the array, so if I have read 3 records, my array now looks like this:
Array (
[0] => 1015789
[1] => 1029402
[2] => 1031345
[3] => 1036476
[4] => 1061512
[5] => 1065031
[6] => 1069892
[7] => 1070721
[8] => 1073222
[9] => 1074811
[10] => 1015789
[11] => 1029402
[12] => 1031345
)
How else could I do this without adding to the array? Does anyone have any suggestions? I am new to PHP and would greatly appreciate any advice.
I have an array of sample user string inputs which may or may not have smarty variables in them which id like to escape with {literal}{/literal} tags.
Array
(
[0] => {$PLEASE}
[1] => {PLEASE}
[2] => {{PLEASE}}
[3] => {{{PLEASE}}}
[4] => {a{PLEASE}}
[5] => {a{$PLEASE}}
[6] => {{$PLEASE}a}
[7] => {{PLEASE}a}
[8] => {{{$PLEASE}}}
[9] => {{{{PLEASE}}}}
)
Here is what I hope to achieve.
Array
(
[0] => {$PLEASE}
[1] => {literal}{PLEASE}{/literal}
[2] => {literal}{{PLEASE}}{/literal}
[3] => {literal}{{{PLEASE}}}{/literal}
[4] => {literal}{a{PLEASE}{/literal}
[5] => {literal}{a{/literal}{$PLEASE}{literal}}{/literal}
[6] => {literal}{{/literal}{$PLEASE}{literal}a}{/literal}
[7] => {literal}{PLEASE}a}{/literal}
[8] => {literal}{{{/literal}{$PLEASE}{literal}}}{/literal}
[9] => {literal}{{{{PLEASE}}}}{/literal}
)
Right now I have this
$data = preg_replace('/{+([^\$])([a-z0-9]*)}+/si', '{literal}{\1\2}{/literal}', $data);
Which gives me
Array
(
[0] => {$PLEASE}
[1] => {literal}{PLEASE}{/literal}
[2] => {literal}{PLEASE}{/literal}
[3] => {literal}{PLEASE}{/literal}
[4] => {a{literal}{PLEASE}{/literal}
[5] => {a{$PLEASE}}
[6] => {{$PLEASE}a}
[7] => {literal}{PLEASE}{/literal}a}
[8] => {{{$PLEASE}}}
[9] => {literal}{PLEASE}{/literal}
)
Been stuck for quite sometime now, was wondering if anyone could help me figure it out or if its even possible to do so.
ok, I'm sure there's a more elegant way, perhaps one-liner, but whatever, it works with the following:
//Step 1: Replace 'real' smarty variables with an intermediate string
$data1 = preg_replace('/{(\$\w+)}/', "!!!$1!!!", $arr);
//replace start and end curly braces with {literal}:
$data2 = preg_replace('/{(.*)}/', '{literal}{$1}{/literal}', $data);
//Replace all inner smarty variables with their original string:
$data3 = preg_replace('/.!!!(.*)!!!/', '{/literal}$1{literal}', $data2);
//Replace standalone variables with their original string:
$data4 = preg_replace('/^!!!(.*)!!!$/', '{$1}', $data3);
You can merge steps 3&4 in one command
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.