php - 在数组中运行循环以获取动态值

We have created function to rotate banners by it's priority value.

We had run this to three banners only, But now we need multiple banners.

We used this array

$arr1 = array(
        array("url" => "1", "priority" => $AD_weight_1),
        array("url" => "2", "priority" => $AD_weight_2),
        array("url" => "3", "priority" => $AD_weight_3),
        );

But now we can't add number or ad weight manually, So we tried to use something like this but it's not working and show only last number of loop !!

$ads_limit = 7;
for ($xy = 0; $xy < $ads_limit; $xy++)
{   
    $arr1 = array(
    array("url" => $xy, "priority" => $ad_groups[$xy]['ad_before_post_weight']),
    );  
}
print_r ($arr1);

Can we use loop inside array or you have any idea for this ?

The issue is that you are assigning the entire array on each iteration of the loop, you need to add elements.

$ads_limit = 7;

// Initialize $arr1 as an array
$arr1 = [];
for ($xy = 0; $xy < $ads_limit; $xy++)
{   
    // Add ads as arrays
    $arr1[] = 
        array("url" => $xy, "priority" => $ad_groups[$xy]['ad_before_post_weight']);  
}
print_r ($arr1);