Forum Moderators: coopster

Message Too Old, No Replies

Array problem, merge array to array

need help please

         

hadingrh

12:05 pm on Jul 12, 2008 (gmt 0)

10+ Year Member



need urgent help from you guys, please..
I have two arrays
- Array a = array ( [0] => 214 [1] => 216 [2] => 217 [3] => 452 [4] => 218 [5] => 219 [6] => 390 [7] => 220 [8] => 221 [9] => 230 [10] => 231 )
and
- Array b = array ( [10] => 228 [11] => 229 )
Question is, how to push the array a to b so the result should be :
array ( [0] => 214 [1] => 216 [2] => 217 [3] => 452 [4] => 218 [5] => 219 [6] => 390 [7] => 220 [8] => 221 [9] => 230 [10] => 228 [11] => 229 [12] => 231 )

?
Thanks for your kind before ;)

henry0

1:13 pm on Jul 12, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



hadingrh, Welcome to WebmasterWorld!

Here is an example and a model from the
manual about array_merge
[php.net]

<?php
$arraya = array ( 0 => 214, 1 => 216, 2 => 217, 3 => 452);
$arrayb = array ( 4 => 228, 5 => 229 ) ;

/*
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
*/

$result=array_merge($arraya, $arrayb);
print_r($result);
?>

The result is:
Array ( [0] => 214 [1] => 216 [2] => 217 [3] => 452 [4] => 228 [5] => 229 )

hadingrh

1:56 pm on Jul 12, 2008 (gmt 0)

10+ Year Member



thank you for your respond,
but as my question before I need to retain the keys of array b.
if I merge those array as array_merge command, it results to :
Array ( [0] => 214 [1] => 216 [2] => 217 [3] => 452 [4] => 218 [5] => 219 [6] => 390 [7] => 220 [8] => 221 [9] => 230 [10] => 231 [11] => 228 [12] => 229 )
differ from what I need, i.e :
array ( [0] => 214 [1] => 216 [2] => 217 [3] => 452 [4] => 218 [5] => 219 [6] => 390 [7] => 220 [8] => 221 [9] => 230 [10] => 228 [11] => 229 [12] => 231 )
note that the value of 228 and 229 need to be placed at key 10 and 11. so that the value of 231 is shifted to key 12.

henry0

2:54 pm on Jul 12, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, so this will do it
simply use the sign +
to retain key values
$arraya = array ( 0=> 214, 1 => 216, 2 => 217, 3 => 452);
$arrayb = array ( 7 => 228, 8 => 29 ) ;

$new_array=($arraya+$arrayb);
print_r($new_array);

hadingrh

3:14 pm on Jul 12, 2008 (gmt 0)

10+ Year Member



the result is not as i'm expected, if I sum the array ($a+$b)
the result is :
Array ( [0] => 214 [1] => 216 [2] => 217 [3] => 452 [4] => 218 [5] => 219 [6] => 390 [7] => 220 [8] => 221 [9] => 230 [10] => 231 [11] => 229 )

array value 228 is missing, and the keys still not as I wanted. :(

henry0

4:13 pm on Jul 12, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Look at my syntax, there are no brackets;
but the brakets return when the new array is called
and the result is exactly what you expect.

maybe someone may help, but I do not figure how to do it with[]

hadingrh

4:27 pm on Jul 12, 2008 (gmt 0)

10+ Year Member



thanks for your help ... but now I wonder how to remove my brackets ? :(
it makes me dizzy, please help anybody :)

hadingrh

9:52 am on Jul 14, 2008 (gmt 0)

10+ Year Member



Yess.. glad I've finally found it.
I used function from php manual's contributor :
function insert_into_array($array,$ky,$val)
{
$n = $ky;
foreach($array as $key => $value)
{
$backup_array[$key] = $array[$key];
}
$upper_limit = count($array);
//echo $upper_limit;

while($n <= $upper_limit)
{
if($n == $ky)
{
$array[$n] = $val;
}
else
{
$i = $n - "1";
$array[$n] = $backup_array[$i];
}
$n++;
}
return $array;
}

and modify its input variable to accommodate array input
foreach ($b as $key=>$val) {
$z = insert_into_array(&$a,$key, $val);
}

henry0

10:48 am on Jul 14, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Glad you sorted it out!