Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/282.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/7/arduino/2.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_Multidimensional Array - Fatal编程技术网

Php 将两个不同的数组数据合并到一个数组中

Php 将两个不同的数组数据合并到一个数组中,php,multidimensional-array,Php,Multidimensional Array,我想将两个不同的数组数据合并到一个数组中,但在这种情况下,我不知道如何使用array\u push 这是我的数据输入示例: ["author"]=> array(2) { [0]=> string(1) "John" [1]=> string(1) "Doe" } ["title"]=> array(2) { [0]=> str

我想将两个不同的数组数据合并到一个数组中,但在这种情况下,我不知道如何使用
array\u push

这是我的数据输入示例:

["author"]=>
  array(2) {
    [0]=>
    string(1) "John"
    [1]=>
    string(1) "Doe"
  }
["title"]=>
  array(2) {
    [0]=>
    string(1) "book a"
    [1]=>
    string(1) "book b"
  }
在一个数组中的结果,我的意思是,像这样:

["books"]=>
      array(2) {
        [0] =>
        array(2) {
          ["author"]=>
          string(1) "John"
          ["title"]=>
          string(1) "book a"
        }
        [1] =>
        array(2) {
          ["author"]=>
          string(1) "Doe"
          ["title"]=>
          string(1) "book b"
        }
      }
我已经尝试使用这种方法,但它仅从每个数组返回1:

$data['books'] = [];

      array_push($data['books'], [
        'author' => $data['author'],
        'title' => $data['title']
      ]);

if (isset($data['books'])) {
  foreach ($data['books'] as $k => $v) {
    $data['books'][$k]['author'] = (int)$v['author'];
    $data['books'][$k]['title'] = (int)$v['title'];
  }
}

result:

["books"]=>
  array(1) {
    [0]=>
    array(2) {
      ["author"]=>
      int(1)
      ["title"]=>
      int(1)
    }
  }

您必须在考虑键的情况下转置数组

function transpose(array $arr){
  $transArr = [];
  foreach($arr as $keyRow => $subArr) {
    foreach($subArr as $keyCol => $value) {
      $transArr[$keyCol][$keyRow] = $value;
    }
  }
  return $transArr;
}
此函数可普遍用于类似问题。该函数来自

如何使用:

$input = [
  "author"=> ["John","Doe"],
  "title" => ["book a","book b"],
];

$books = transpose($input);

echo '<pre>';
var_export($books);
输出:

$input = ['author' => $arrayAuthor, 'title' => $arrayTitle];
如果“author”和“title”作为两个数组存在,则必须首先按如下方式创建$input:

["books"]=>
      array(2) {
        [0] =>
        array(2) {
          ["author"]=>
          string(1) "John"
          ["title"]=>
          string(1) "book a"
        }
        [1] =>
        array(2) {
          ["author"]=>
          string(1) "Doe"
          ["title"]=>
          string(1) "book b"
        }
      }

“困惑”到底是什么意思?展示你的努力,并解释你在代码中遇到的困难。你在哪里遇到困难?你试过什么?你到底对什么感到困惑<代码>数组推送($arr,$val)=
$arr[]=$val。它们是一样的。两者都向数组中添加元素。如何构造第一个数组?这就是您想要更改以获得第二个变量的原因。感谢您回答我的问题:“)实际上,它只需要创建一个var数组为空,然后在循环每个数据HUHU时将其放入
$input = ['author' => $arrayAuthor, 'title' => $arrayTitle];