0

i am new to array. Hope you can help me. i have 2 array. Let say:

array 1 = [[50,60],[70,80]]

array 2 = ["Fire", "Marine"]

array 1 index : Array([0]=>Array ([0]=>50 [1]=>60) [1]=>Array ([0]=>70 [1]=>80))

array 2 index : Array ([0]=> Fire [1]=>Marine)

How i can insert array 2 into array 1 like output below :

Output that i want like below:

array 3 = [["Fire",50,60],["Marine",70,80]]

array 3 index = (Array([0]=>Array ([0]=>Fire [1]=>50 [2]=>60) [1]=>Array ([0]=>Marine [1]=>70 [2]=>80))

Thanks

4 Answers 4

2

array_unshift() Prepend one or more elements to the beginning of an array, that you want in your child array. http://codepad.org/ufRe2qiz

$arr1 = array( array(50, 60), array(70, 80) );
$arr2 = array("Fire", "Marine");
$arr3 = array();

$i = 0;
foreach( $arr1 as $child ){
   array_unshift( $child, $arr2[$i] );
   $arr3[] = $child;
   $i++;
}

print_r( $arr3 );

Result:

Array
(
[0] => Array
    (
        [0] => Fire
        [1] => 50
        [2] => 60
    )

[1] => Array
    (
        [0] => Marine
        [1] => 70
        [2] => 80
    )

)
Sign up to request clarification or add additional context in comments.

1 Comment

Hi jogesh_pi, now my google chart work as i need. thank you :)
1

Try this code

<?php
     //original array1
      $array1=array(
         0=>array(0=>50,1=>60),
         1=>array(0=>70,1=>80)
      );

      //original array2
  $array2=array(0=>'Fire',1=>'Marine');

  //new array     
  $array3=array();

  foreach($array1 as $key=>$val)
  {

    $array3[$key]=$val;
    array_push($array3[$key],$array2[$key]);

}

print_r($array3);
?>

Output : Array ( [0] => Array ( [0] => 50 [1] => 60 [2] => Fire ) [1] => Array ( [0] => 70 [1] => 80 [2] => Marine ) )

It should work give a try!!

3 Comments

array_push() prepend an element at the end of the array, but OP want add element on the beginning of array so you have to use array_unshift()
Yes jogesh if he need to have array2 to be as first element. :)
hi Praveen Reddy, by the way, i learn something new from you too. Thanks.
1

use array_map() function

codepad demo

$array1 = array(array(50,60),array(70,80));

$array2 = array("Fire", "Marine");

function mergebyindex($a, $b){
    $temp = array();
    $temp[] = $a;
    if(is_scalar($b)){
        $temp[] = $b;
    } else {
        foreach($b as $k => $v ){
            $temp[] = $v;
        }
    }
    return $temp;
}

echo '<pre>'; print_r(array_map('mergebyindex',$array2, $array1));

1 Comment

Hi Pragnesh Chauhan, your solution work for me too. thanks! :)
0

I would rethink your programming logic to be honest, juggling array's like that gets incredibly messy when you hit larger scale tasks (ESPECIALLY in mapping).

in PHP you can easily push things to the end of an array by:

$arr1[] = 'val1';
$arr1[] = 'val2';

same as:

$arr1 = array('val1','val2');

If you don't care about array positioning, you could do something like:

$arr2 = array(50,70); //original array
$arr2[] = 'marine';

would give:

$arr2 = array(50,70,'marine');

Otherwise look into PHP Array Shift, PHP Array Unshift and PHP Array Pop

2 Comments

i need to use this for google column chart. got solution from jogesh_pi and Pragnesh Chauhan. by the way thanks elzaer. :)
No worries - Dont forgot to vote up the helpful answers and mark something as correct!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.