Add tems to array with keys in PHP - php

How to add array items to an existing array with key => value ? actually i want to create an array of mysql rowset i.e.
$n =0;
while($row = mysql_fetch_array($rowset))
{
$array[$n] = array('name' => $row['name'], 'city' = $row['city']);
$n += 1;
}
Thanks.

Just try with:
$existingArray['newKey'] = 'new value';
Or use array_merge function:
$newArray = array_merge($existingArray, $additionalData);

http://php.net/manual/en/function.array-merge.php
That what you're looking for?
-edit-
Just to note, if conflicting results are found, the last most array entry will be used. If you array merge three arrays with id fields, only the final arrays id will be stored in the result.

You may want to look into this:
http://php.net/array_push
Should be simple enough.

For One:
$array['key'] = $value;
Merge:
$mergedArray = array_merge($array1, $array2);
(http://php.net/manual/en/function.array-merge.php)

Related

How to change key array php

How to change key array php from
array(
[0]=>Joni
[1]=>Jono
[2]=>Riki
[3]=>Budi
);
Change index to:
array(
[nominal]=>Joni
[nominal]=>Jono
[nominal]=>Riki
[nominal]=>Budi
);
you can make a multi-dimensional array for this purpose
$arr = array('a','b','c','d');
for($i=0;$i<count($arr);$i++){
$newArr['nominal'][$i] = $arr[$i];
}
print_r($newArr);
The expected outcome you want is not possible at all, because same indexes will be over-written in single-dimensional array.
Check this to understand what i said above:- https://eval.in/954556
Now there are 2 closer possiblities of outcomes, which i am going to mention:-
$possibility1 = [];
$possibility2 =[];
foreach($array as $arr){
$possibility1[] = ['nominal'=>$arr];
$possibility2['nominal'][] = $arr;
}
print_r($possibility1);//first closer possibility
print_r($possibility2);//second closer possibility
Output:- https://eval.in/954559

Can I merge arrays which names are kept as variable?

I have an array that contains names of arrays
$names_array[] = ('$array1', '$array2', $array3'....)
The $names_array[] is dynamically updated so it may contain 2 or more different names.
When the script is executed the values of listed arrays in $names_array[] need to be merged.
I think in case of merging it is not a problem
u can merge $result = array_merge($array1, $array2);
http://php.net/manual/en/function.array-merge.php
I think it can be done with variable variables.
$arraymerge = array();
foreach ($names_array as $arrayname)
{
$arraymerge = array_merge($arraymerge, ${$arrayname});
}
Thanks for help... I have went around the problem: if anyone needs to merge dynamically generated arrays, in my case I have six arrays that exist or not, so I need to merge the existing ones. What I did is:
if(!is_array($array1[$i])) $array1[$i]=array();
if(!is_array($array2[$i])) $array2[$i]=array();
if(!is_array($array3[$i])) $array3[$i]=array();
if(!is_array($array4[$i])) $array4[$i]=array();
if(!is_array($array5[$i])) $array5[$i]=array();
if(!is_array($array6[$i])) $array5[$i]=array();
$combineddata[$i]=array_merge($array1[$i], $array2[$i],$array3[$i],$array4[$i], $array5[$i], $array6[$i]);
In case 'array_x[$i]' doesn't exists array_merge doesn't break the script just merges empty array.
Thanks
$names_array = array ('array1', 'array2', 'array3');
$array1 = array ('a','b','c');
$array2 = array ('d','e','f');
$array3 = array ('g','h','i');
$result = array ();
foreach ($names_array as $x) {
$result = array_merge ($result, $$x);
}
print_r ($result);

php - Adding an element form one array into another

I have 2 arrays like so:
$array1 = array(
array("foo"=>"bar","count"=>"3"),
array("foo2"=>"bar2","count"=>"4"),
array("foo3"=>"bar3","count"=>"2")
);
$array2 = array(
array("foo4"=>"bar","count"=>"3"),
array("foo5"=>"bar2","count"=>"4"),
array("foo6"=>"bar3","count"=>"2")
);
how can i add the 3rd element of array2 into array1 so it can become like this:
$array1 = array(
array("foo"=>"bar","count"=>"3"),
array("foo2"=>"bar2","count"=>"4"),
array("foo3"=>"bar3","count"=>"2"),
array("foo6"=>"bar3","count"=>"2")
);
i have tried doing $array1 += $array2[2]; but it doesn't work. it just adds the keys from array("foo6"=>"bar3","count"=>"2") to array1 instead of adding it as an array in $array1
Could you help me out?
The [] operator appends an element to the end of an array, like this
$array1[] = $array2[2];
Just do like this:
$array1[] = $array2[2];
If you want the exact 3rd item, then you could do something like:
$array1[] = $array2[2];
If you want the last item of the array, you can use:
$array1[] = $array2[count($array2)];
try this
$array1[] = $array2[2];
array_merge() is a function in which you can copy one array to another in PHP.
http://php.net/manual/en/function.array-merge.php

"Classic" array in php

So, I'm starting a new project and working with php for the first time.
I get that the average definition and functioning of arrays in php is actually pretty much a namevalue combo.
Is there some syntax, API, or other terminology for just a simple list of items?
I.e. inserting something like ['example','example2','example3','example4'] that I can just call based off their index position of the array, without having to go in and modify the syntax to include 0 => 'example', etc...
This is a very shortlived array so im not worried about long term accessibility
php arrays are simple to use. You can insert into an array like:
$array=array('a','b','c'.....);
Or
$array[]="a";
$array[]="b";
$array[]="c";
or
array_push($array, "a");
array_push($array, "b");
array_push($array, "c");
array_push($array, "d");
and call them by their index values:
$array[0];
this will give you a
$yourArray = array('a','b','c');
or
$yourArray[] = 'a';
$yourArray[] = 'b';
$yourArray[] = 'c';
will get you an array with integer index values instead of an associative one..
You still can use array as "classic" arrays in php, just the way you think.
For example :
<?php
$array = array("First", "Second", "Third");
echo $array[1];
?>
You can then add different values <?php $array[] = "Forth"; ?> and it will be indexed in the order you specified it.
Notice that you can still use it as an associative array :
<?php
$array["newValue"] = "Fifth";
$array[1] = "ReplaceTheSecond";
$array[10] = "";
?>
Arrays in PHP can either be based on a key, like 0 or "key" => "value", or values can just be "appended" to the array by using $array[] = 'value'; .
So:
$mine = array();
$mine[] = 'test';
$mine[] = 'test2';
echo $mine[0];
Would produce 'test';
Haven't tested the code.

Combine Two Arrays with numerical keys without overwriting the old keys

I don't want to use array_merge() as it results in i misunderstood that all values with the same keys would be overwritten. i have two arrays
$array1 = array(0=>'foo', 1=>'bar');
$array2 = array(0=>'bar', 1=>'foo');
and would like to combine them resulting like this
array(0=>'foo', 1=>'bar',2=>'bar', 3=>'foo');
array_merge() appends the values of the second array to the first. It does not overwrite keys.
Your example, results in:
Array (
[0] => foo
[1] => bar
[2] => bar
[3] => foo )
However, If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.
Unless this was just an example to another problem you were having?
Does this answer your question? I'm not sure exactly what you're trying to accomplish, but from your description it sounds like this will work:
$array1 = array(0=>'foo', 1=>'bar');
$array2 = array(0=>'bar', 1=>'foo');
foreach ($array2 as $i) {
$array1[] = $i;
}
echo var_dump($array1);
If someone stumbles upon this, this is a way to do it nowadays:
var_dump(array_merge_recursive($array1, $array2));
There are probably much better ways but what about:
$newarray= array();
$array1 = array(0=>'foo', 1=>'bar');
$array2 = array(0=>'bar', 1=>'foo');
$dataarrays = array($array1, $array2);
foreach($dataarrays as $dataarray) {
foreach($dataarray as $data) {
$newarray[] = $data;
}
}
print_r($newarray);
$result = array_keys(array_merge(array_flip($array1), array_flip($array2)));
var_dump($result);

Categories