循环时将字段和值添加到数组?

add field and value to array while looping through?

我有以下 2 个数组,我需要根据第二个数组的 ID 值添加第一个数组的值作为 Key,以便我可以对第二个数组进行排序 DESC:

$ 观看次数:

Array ( [1851] => 12 [14341] => 7 [17834] => 3 )

我有以下数组对象:

$most_seen_list :

Array ( 
    [0] => WP_Post Object ( 
            [ID] => 17834 
            [post_date] => 2021-10-20 16:01:50 
            [post_date_gmt] => 2021-10-20 21:01:50 
        )
    [1] => WP_Post Object ( 
            [ID] => 14341 
            [post_date] => 2021-06-01 17:57:00 
            [post_date_gmt] => 2021-06-01 22:57:00 
        )
    [2] => WP_Post Object ( 
            [ID] => 1851
            [post_date] => 2021-02-13 18:09:00 
            [post_date_gmt] => 2021-02-13 23:09:00 
        )
)

我正在检查下一个 foreach,我想用我从另一个数组获得的值更改 [0]..[1]..[3] 键:

foreach ($most_seen_list as $key => $value) {
    $newResult[$value->ID];
}

预期输出:

Array ( 
    [12] => WP_Post Object ( 
            [ID] => 1851
            [post_date] => 2021-02-13 18:09:00 
            [post_date_gmt] => 2021-02-13 23:09:00 
            )
    [7] => WP_Post Object ( 
            [ID] => 14341 
            [post_date] => 2021-06-01 17:57:00 
            [post_date_gmt] => 2021-06-01 22:57:00 
        )
    [3] => WP_Post Object ( 
            [ID] => 17834 
            [post_date] => 2021-10-20 16:01:50 
            [post_date_gmt] => 2021-10-20 21:01:50 
        )
)

我假设您已经对 $views 数组进行了排序。因此,您将需要处理每个 $most_seen_list 对象数组,以使用内部循环

找到正确的对象
$views = Array ( 1851 => 12, 14341 => 7, 17834 => 3 );
$most_seen_list = Array ( 
    (Object) [ 'ID' => 17834,'post_date' => '2021-10-20 16:01:50', 'post_date_gmt' => '2021-10-20 21:01:50',
    ],
    (Object) [ 'ID' => 14341, 'post_date' => '2021-06-01 17:57:00', 'post_date_gmt' => '2021-06-01 22:57:00'
    ],
    (Object) ['ID' => 1851, 'post_date' => '2021-02-13 18:09:00', 'post_date_gmt' => '2021-02-13 23:09:00' 
    ]
);

$new = [];
//get the ID'2 in the predefined order set in $views
foreach ( $views as $key=>$val) {
    // for each view find the correct object in $most_seen_list
    foreach( $most_seen_list as $obj) {
        if ( $obj->ID == $key ) {
            $new[$val] = $obj;
            break;  // terminate this iteration
        }
    }
}

print_r($new);

结果

Array
(
    [12] => stdClass Object
        (
            [ID] => 1851
            [post_date] => 2021-02-13 18:09:00
            [post_date_gmt] => 2021-02-13 23:09:00
        )

    [7] => stdClass Object
        (
            [ID] => 14341
            [post_date] => 2021-06-01 17:57:00
            [post_date_gmt] => 2021-06-01 22:57:00
        )

    [3] => stdClass Object
        (
            [ID] => 17834
            [post_date] => 2021-10-20 16:01:50
            [post_date_gmt] => 2021-10-20 21:01:50
        )

)

如果您不需要将观看次数作为键(如果某些观看次数是相同的,这可能会出现问题),您可以使用以下命令以正确的顺序对 post 进行排序。

// sort asc on views
usort($most_seen_list, function($postA, $postB) use ($views) {
    return $views[$postA->ID] <=> $views[$postB->ID];
});
$most_seen_list = array_reverse($most_seen_list); // reverse the order, so most viewed is first

在此之后,您仍然可以使用 post 获得观看次数:

$views[$oneOfThePosts->ID];