-1

I want to ask about how to replace inner key in a multidimensional array. I have an multidimensional array :

$array1=
array(array(5000, 6,  325,  3,  3,  517000000),
      array( 20000,  5,  217,  5,  3,  1692000000)
      );

The second array is

$array2=array(1,2,3,4,5,6);

I expected the new array is

Array(
[0] => Array
    (
        [1] => 5000
        [2] => 6
        [3] => 325
        [4] => 3
        [5] => 3
        [6] => 517000000
    )

[1] => Array
    (
        [1] => 20000
        [2] => 5
        [3] => 217
        [4] => 5
        [5] => 3
        [6] => 1692000000
    ))

I have tried this code below by another post PHP Replace multidimensional array keys, but I can't assign the value of my array1

foreach($array2 as $array2 ){
    for($k=0;$k<sizeof($array2);$k++){
        for($l=0;$l<$count;$l++){
        $last[$l][$array2] = $array1[$k][$l];
    }
    $i += $count;
    }

}

Thank you

1
  • 1
    Look at array_map() and array_combine() Commented Mar 19, 2016 at 1:31

2 Answers 2

0

As @Rizier has suggested in his comment you can do this using array_map() and array_combine().

<?php
$array1=
array(array(5000, 6,  325,  3,  3,  517000000),
      array( 20000,  5,  217,  5,  3,  1692000000)
      );
$array2 = array(1, 2, 3, 4, 5, 6);

foreach($array1 as $arr1){
    $array3[] = array_combine($array2, $arr1);
}
var_dump($array3);

output

  array (size=2)
      0 => 
        array (size=6)
          1 => int 5000
          2 => int 6
          3 => int 325
          4 => int 3
          5 => int 3
          6 => int 517000000
      1 => 
        array (size=6)
          1 => int 20000
          2 => int 5
          3 => int 217
          4 => int 5
          5 => int 3
          6 => int 1692000000
Sign up to request clarification or add additional context in comments.

Comments

0

try

<?php
$array1=
array(array(5000, 6,  325,  3,  3,  517000000),
      array( 20000,  5,  217,  5,  3,  1692000000)
      );
$newArray = array();
foreach($array1 as $arr){
  array_unshift($arr,'');
  unset($arr[0]);
  $newArray[] = $arr;   
}

print_r($newArray);

this will have same output you require. hope it helps :)

Comments

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.