Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 多维数组到单个数组_Php_Arrays_Post - Fatal编程技术网

Php 多维数组到单个数组

Php 多维数组到单个数组,php,arrays,post,Php,Arrays,Post,在使用ajax和PHP构建表单时,我正在努力处理多维数组。 我的ajax将多维数组发送到php。我想把这个数组转换成一维数组 因此,考虑到这一准则: $reasons = $_POST['reasons']; var_dump($reasons); 哪个输出: array(1) { [0]=> array(2) { ["id"]=> string(2) "37"

在使用ajax和PHP构建表单时,我正在努力处理多维数组。 我的ajax将多维数组发送到php。我想把这个数组转换成一维数组

因此,考虑到这一准则:

$reasons = $_POST['reasons'];
var_dump($reasons);
哪个输出:

    array(1) {
      [0]=>
        array(2) {
          ["id"]=>
            string(2) "37"
          ["reason"]=>
            string(9) "Something"
        }
   }
array(2) {
  ["id"]=>
    string(2) "37"
  ["reason"]=>
    string(9) "Parce que"
我现在希望它是这样的:

$reasons = {
  37 => "Something"
}
我该怎么做? 我已经搜索了几个小时,但没有找到正确的解决方案

我所尝试的:

(一)

  • 输出此项:

        array(1) {
          [0]=>
            array(2) {
              ["id"]=>
                string(2) "37"
              ["reason"]=>
                string(9) "Something"
            }
       }
    
    array(2) {
      ["id"]=>
        string(2) "37"
      ["reason"]=>
        string(9) "Parce que"
    
    }

    除了我的多维数组,哪种工作可以存储多个ID和原因。当这种情况发生时,上面的代码只返回一对键和值

    比如,如果我在多维空间中有两个id和两个原因,它只会将最后一个id和原因返回到单个数组中


    我想要的是:array(id=>reason,id=>reason,等等)

    不确定您的数据是否比这里显示的更复杂,但从提供的数据来看,这是非常简单的

    $result = [
        $reasons[0]['id'] => $reasons[0]['reason']
    ];
    
    像这样的事

     function array_flatten($data) { 
             $res=array();
             foreach ($data as $val) { 
                $res[$val['id']] =$val['reason'];
            }
            return $res;
        }
    
    可以使用提取列的线性数据,并将它们组合为键/值

    $reasons = [
        ['id' => 37, 'reason' => 'something'],
        ['id' => 38, 'reason' => 'something else'],
    ];
          
    $indexed = array_combine(array_column($reasons, 'id'), array_column($reasons, 'reason'));
    
    print_r($indexed);
    
    输出:

    Array
    (
        [37] => something
        [38] => something else
    )
    

    这回答了你的问题吗?我已经搜索了几个小时,但没有找到正确的解决方案。。。请发布你的代码,你尝试了什么?我用我尝试过的所有东西编辑了代码,并对我尝试做的事情进行了更多的解释。你这是什么意思?请在你的答案中添加一些解释,以便其他人可以从中学习