How to convert array first value as key and second value as value

前端 未结 2 653
被撕碎了的回忆
被撕碎了的回忆 2020-12-19 03:41

Hi I am working on some array operations.

I need to convert first value of array as key and second value of array as value.

I have one variable $test

相关标签:
2条回答
  • 2020-12-19 04:08

    Assuming that structure will always be the same, you could do this:

    $output = array();
    foreach($testArray as $v){
        $output[] = array($v[0] => $v[1]);
    }
    

    See it in action here.

    0 讨论(0)
  • 2020-12-19 04:26

    Simple solution using array_map function:

    $result = array_map(function($v){
        return [$v[0] => $v[1]];
    }, $testArray);
    
    0 讨论(0)
提交回复
热议问题