Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-在while循环中获取唯一值_Php_While Loop - Fatal编程技术网

PHP-在while循环中获取唯一值

PHP-在while循环中获取唯一值,php,while-loop,Php,While Loop,我有以下代码:- if( $featured_query->have_posts() ): $property_increment = 0; while( $featured_query->have_posts() ) : $featured_query->the_post(); $town = get_field('house_town'); $a = array($town); $b = array_unique(

我有以下代码:-

if( $featured_query->have_posts() ): $property_increment = 0;
    while( $featured_query->have_posts() ) : $featured_query->the_post(); 

        $town = get_field('house_town');
        $a = array($town);
        $b = array_unique($a);
        sort($b);

        var_dump($b);

    $property_increment++; endwhile; ?>
<?php endif; wp_reset_query();

在上面的alpha中,如果您能提供任何帮助,我们将不胜感激。

您的数组需要先与
array\u列取消嵌套,然后再对其进行排序并使其唯一。因此,在您初始化$a后,继续如下操作:

$b = array_unique(array_column($a, 0));
sort($b);
然后制作HTML:

$html = "";
foreach($b as $town)  {
    $html .= "<option value='$town'>$town</option>";
}
echo  "<select>$html</select>";
#在等待时收集所有get_字段('house_town')
$collect[]=get_field(“house_town”);
#那就开始工作吧
$html=内爆(“”,
数组映射(
功能($a){
返回“{$a}”;
},
数组_唯一($collect)
)
);

以下是Chris G的评论和trincot生成HTML代码的代码片段的摘要

注意:出于测试目的,我在这里手动创建了$town数组。替换为您的语句$town=get_field('house_town')

<?php
$town = array(
    "Nottingham",
    "Leicester",
    "Leicester",
    "Mountsorrel",
    "Loughborough",
    "Loughborough"
);

// $town = get_field('house_town');

$html = "";
$town = array_unique($town);
sort($town);
foreach($town as $xtown) {
    $html .= "<option value='$xtown'>$xtown</option>";
}
echo  "<select>$html</select>";
?>  

那么到底是什么问题?如何创建唯一数组,或者如何使用数组生成
s?您是否尝试过$result=array\u unique($town);使用数组生成唯一选项您能给我们看一个var_dump($town)和一个var_dump($a)吗;生成具有唯一城镇的阵列。然后使用trincot的代码片段生成HTML代码。(如有必要,我可以将摘要作为aswer发布)。这是可行的,但由于某些原因,值不是唯一的,即有两个选项作为“Leicester”,啊,是的,您的数组是嵌套的。我修改了我的答案。我使用的是PHP版本5.3.29,所以array_column是未定义的函数,有其他选择吗?我在我的答案中添加了一个polyfill,也就是说,一个函数会做同样的事情。现在,当应该有3个uniqueThank@ob_start时,会显示16个结果-现在唯一的问题是选项中的值不是按字母顺序排列的
function array_column($arr, $column) {
    $res = array();
    foreach ($arr as $el) {
        $res[] = $el[$column];
    }
    return $res;
}
 #collect all get_field('house_town') in while
 $collect[] = get_field('house_town');

 #then do the work
 $html = implode('',
           array_map(
               function($a){
                    return "<option value='{$a}'>{$a}</option>";
               },
               array_unique($collect)
             )
         );
<?php
$town = array(
    "Nottingham",
    "Leicester",
    "Leicester",
    "Mountsorrel",
    "Loughborough",
    "Loughborough"
);

// $town = get_field('house_town');

$html = "";
$town = array_unique($town);
sort($town);
foreach($town as $xtown) {
    $html .= "<option value='$xtown'>$xtown</option>";
}
echo  "<select>$html</select>";
?>