如何添加一个空字符串作为现有数组的第一个元素?

How to add an empty string as the first element of an existing array?

我正在合并数组值。这是我的代码,它工作正常。

function photomag_image_size() {
    $image_sizes = get_intermediate_image_sizes();
    return array_merge($image_sizes, array('full',));   
}

现在我想在数组的第一个位置添加另一个包含空字符串的元素,并将 full 保留为最后一个元素。我希望第一个下拉选项为空。

尝试 array_unshift() php 函数检查 here 像这样

   $queue=array_merge($image_sizes, array(
        'full',
       ) );
    array_unshift($queue, " ");
    return $queue;

您可以使用 array_unshift 或像使用 full 那样合并:

function photomag_image_size() {
    $image_sizes = get_intermediate_image_sizes();
    $image_sizes = array_merge(array(""), $image_sizes);
    return array_merge($image_sizes, array('full') ); 
}

再次使用array_merge

$temp = array_merge($image_sizes, array('full'));
return array_merge(array("first") , $temp);