(PHP)初始化空的多维数组,然后填充它

时间:2019-11-07 12:33:23

标签: php arrays initialization array-push

我想用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] =>
)

我做错了什么?

3 个答案:

答案 0 :(得分:0)

您可以像这样简单地创建它:

$arrayMultiDim = [ 
    [
      'id' => 3,
      'name' => 'Carmen'
    ],
    [
      'id' => 4,
      'name' => 'Roberto'
    ]
];

然后再加上只是说:

$arrayMultiDim[] = ['id' => 5, 'name' => 'Juan'];

答案 1 :(得分:0)

非常简短的答案:

imsave

答案 2 :(得分:0)

请尝试以下代码:

$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;

Demo