douchen4547 2016-03-28 07:31
浏览 25

在php中放一个数组末尾的键

I have this array:

Array
(
[0] => Array
    (
        [date] => 2016-03-08
        [value] => Array
            (
                [key_1] => Array
                    (
                        [test_1] => 1
                        [test_2] => 10
                        [test_3] => 1000
                        [test_4] => 200
                    )

                [key_2] => Array
                    (
                        [test_1] => 1
                        [test_2] => 15
                        [test_3] => 1500
                        [test_4] => 100
                    )

             )
)

Now I have another array :

Array
(
  [key_3] => Array
    (
        [test_1] =>
        [test_2] =>
        [test_3] =>
        [test_4] => 1
    )

) I want to add this last array in the first array. I try like this : array_push($ymlParsedData[]['value'], $a_big_gift); but not work. Can you help me please ?

  • 写回答

3条回答 默认 最新

  • dongshi3061 2016-03-28 07:38
    关注

    You can't use $ymlParsedData[] for accessing specific element, it is a shorthand for pushing data to array.

    You can use either

    // NB! array_push() just adds the values, key 'key_3' is removed
    array_push($ymlParsedData[0]['value'], $a_big_gift);
    

    or

    // will keep key 'key_3'
    $ymlParsedData[0]['value']['key_3'] = $a_big_gift['key_3'];
    

    or

    // use array_merge() instead
    $ymlParsedData[0]['value'] = array_merge($ymlParsedData[0]['value'], $a_big_gift);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探