Skip to content
Advertisement

add field and value to array while looping through?

I have the folowing 2 arrays, and I need to add the value of the first one depending on the ID value of the second one as Key so I can order the 2nd array DESC:

$views:

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

And I have the folowing array object:

$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 
        )
)

with the next foreach Im going through it and I want to change the [0]..[1]..[3] key with the value I get from another array:

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

Expected output:

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 
        )
)

Advertisement

Answer

I assume you already sorted the $views array. So you will need to process each of the array of $most_seen_list objects to find the right one using an inner loop

$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);

RESULTS

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
        )

)
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement