从 php 中的单个数组导出两个不同的数组

Derive two different arrays from a single array in php

有一个名为 $counts 的数组,输出为

array:3 [
0 => {
"applied_date": "10-10"
"count": 1
}
1 => {
"applied_date": "10-14"
"count": 1
}
2 => {
"applied_date": "10-15"
"count": 1
}
]

我想构建两个不同的数组,其中包含应用日期及其来自每个数组的值,并从 php 中的每个数组计数。它是在关联数组下还是其他什么?我是 php.

的新手

遍历 $counts 数组并将每个元素推送到新数组,如下所示:

$applied_dates_arr = [];
$count_arr = []

foreach ($counts as $el) {
    $applied_dates_arr[] = $el['applied_date'];
    $count_arr[] = $el['count'];
}

您可以尝试下面的代码来解决您的问题

<?php
// Array representing a possible record set returned from a database
$records = array(
    array(
        "applied_date"=> "10-10",
        "count"=> 1
    ),
    array(
        "applied_date"=> "10-10",
        "count"=> 1
    ),
    array(
        "applied_date"=> "10-10",
        "count"=> 1
    )
);

$applied_date = array_column($records, 'applied_date');
$count = array_column($records, 'count');
print_r($applied_date);
print_r($count);


?>

希望对您有所帮助!