首页 > 解决方案 > (PHP) 初始化空多维数组然后填充

问题描述

我想创建一个包含 3 种信息类型的数组:名称、id 和工作。首先,我只想初始化它,以便以后可以用变量中包含的数据填充它。

我搜索了如何初始化一个多维数组,以及如何填充它,这就是我想出的:

$other_matches_info_array = array(array());

$other_matches_name = "carmen";
$other_matches_id = 3;
$other_matches_work = "SON";

array_push($other_matches_info_array['name'], $other_matches_name);
array_push($other_matches_info_array['id'], $other_matches_id);
array_push($other_matches_info_array['work'], $other_matches_work);

这是我print_r在数组时得到的:

Array
(
  [0] => Array
    (
    )
  [name] =>
)

我做错了什么?

标签: phparraysinitializationarray-push

解决方案


试试下面的代码:

$other_matches_info_array_main = [];

$other_matches_name = "carmen";
$other_matches_id = 3;
$other_matches_work = "SON";

$other_matches_info_array['name'] = $other_matches_name;
$other_matches_info_array['id'] = $other_matches_id;
$other_matches_info_array['work'] = $other_matches_work;


$other_matches_info_array_main[] = $other_matches_info_array;

演示


推荐阅读