doucheng4094 2017-07-19 10:11
浏览 186
已采纳

PHP array_push将一个数组转换成另一个数组

I need to add one array inside of another using array_push or something similar.

What I would like to achieve will look like this:

$years = array(
    "2017" => array("Value01", "Value02"),
    "2016" => array("Value03")
);

What would be the best way to create an array within another? I have tried the following:

array_push($years, "2016" => array());

This just gives a

500 error.

And:

array_push($years, "2016");

This does add a new array within $yearshowever it is called "2018" and the "2016" value is place inside that 2018 array. I have manually created the 2017 array already, so I assume it is just incrementing on that number for some reason.

  • 写回答

3条回答 默认 最新

  • dops57958 2017-07-19 10:15
    关注

    Correct way to do with array_push():-

    array_push($years, array("2016" => array()));
    

    But what you want, for that do:-

    $years["2016"]= array();
    

    Output:- https://eval.in/834452

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题