简体   繁体   English

如何获取数组根并添加到数组子级

[英]How to get array root and add to array child

How do I take the root of an array and then add into the value taken his child to become an array of array data 我该如何取数组的根,然后将其子取的值添加到数组数据数组中

I have an array data like : 我有一个像这样的数组数据:

array(10) {
  ["2016-07-06"]=>
  array(11) {
    ["nb_uniq_visitors"]=>
    int(795)
    ["nb_users"]=>
    int(0)
    ["nb_visits"]=>
    int(896)
    ["nb_actions"]=>
    int(3134)
  }
  ["2016-07-07"]=>
  array(11) {
    ["nb_uniq_visitors"]=>
    int(878)
    ["nb_users"]=>
    int(0)
    ["nb_visits"]=>
    int(987)
    ["nb_actions"]=>
    int(3975)
  }
}

From the data array above I would like to take in the array of arrays where I want to combine the date data into their array. 从上面的数据数组中,我想将数组中的日期数据合并到它们的数组中。

Let's just above my input array into a single variable. 让我们在我的输入数组上方,进入一个变量。 Call it $allData . 称之为$allData Then I loop 然后我循环

foreach ($allData as $data) {
    echo '<pre>';
    var_dump($data);
}

From the results above loop I get a new array data like 从上面的循环结果中,我得到了一个新的数组数据,例如

array(11) {
    ["nb_uniq_visitors"]=>
    int(795)
    ["nb_users"]=>
    int(0)
    ["nb_visits"]=>
    int(896)
    ["nb_actions"]=>
    int(3134)
}
array(11) {
    ["nb_uniq_visitors"]=>
    int(878)
    ["nb_users"]=>
    int(0)
    ["nb_visits"]=>
    int(987)
    ["nb_actions"]=>
    int(3975)
}

If I want to take the data that previously used the root array array_push() , how to take and put it to use array_push() ? 如果我想获取以前使用根数组array_push() ,如何获取并放入以使用array_push()

Formed later so that the end result as 稍后形成,以便最终结果为

array(11) {
    ["date_visitor"]=>
    string(20) "2016-07-06"
    ["nb_uniq_visitors"]=>
    int(795)
    ["nb_users"]=>
    int(0)
    ["nb_visits"]=>
    int(896)
    ["nb_actions"]=>
    int(3134)
}
array(11) {
    ["date_visitor"]=>
    string(20) "2016-07-07"
    ["nb_uniq_visitors"]=>
    int(878)
    ["nb_users"]=>
    int(0)
    ["nb_visits"]=>
    int(987)
    ["nb_actions"]=>
    int(3975)
}

Using what functions to take root array, then I will use array_push to add the data into an array of child? 使用什么函数获取根数组,那么我将使用array_push将数据添加到子数组中?

Thanks 谢谢

A simple foreach loop should do the job: 一个简单的foreach循环就可以完成这项工作:

$new=[];
foreach($allData as $key=>$value){
    $value['date_visitor']=$key;
    $new[]=$value;
}

var_dump($new);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM