【问题标题】:Array in foreach shows only first arrayforeach 中的数组仅显示第一个数组
【发布时间】:2017-01-13 07:57:39
【问题描述】:

我正忙于一个 foreach 循环,但我找不到问题所在。 此代码仅显示第一个数组,然后停止。问题来自foreach循环中的数组。

这是我的代码:

<?php 
$catarray = array();
$catslug = array();

while ( have_posts() ) : the_post();
    global $post;
    $terms = get_the_terms( $post->ID, 'product_cat' );

    foreach ($terms as $term) {
        $array2 = array (  
            'name' => $term->name,  
            'slug' => $term->slug,
            'id' => $term->term_id
        );

        $product_cat = $array2;
        //$product_cat = $term->name;
        break;
    }
    array_push( $catarray, $product_cat );
endwhile;

$categorielijst = array_unique($catarray);

echo '<pre>';
print_r($categorielijst);
echo '</pre>';

?>

结果是:

    Array
(
    [0] => Array
        (
            [name] => Salades
            [slug] => salades
            [id] => 67
        )

)

如果我改变 $product_cat = $array2; 为了 $product_cat = $term->名称;

比是输出:

Array
(
    [0] => Salades
    [1] => Aardappels
    [3] => Diverse fruit
    [4] => Blad groentes
    [5] => Schulp sappen 100% fruit en groentes
    [8] => Groentes
    [11] => Uien
    [13] => Verse kruiden
    [19] => Dressings kiooms
    [25] => Appels
    [28] => Paddenstoelen
    [32] => Tomaten
    [34] => Bananen
    [35] => Citrus fruit
    [37] => Peren
    [49] => Verse sla
)

【问题讨论】:

  • 期望的结果是什么?
  • 我想要的是:Array ( [0] =&gt; Array ( [name] =&gt; Salades [slug] =&gt; salades [id] =&gt; 67 ) [1] =&gt; Array ( [name] =&gt; Aardappels [slug] =&gt; aardappels [id] =&gt; 23 ) [3] =&gt; Array ( [name] =&gt; Diverse fruit [slug] =&gt; diverse-fruit [id] =&gt; 11 )等等等等
  • 你为什么打电话给break;这会跳出循环?
  • 添加到@atoms $product_cat 的值如果删除break,将在每个循环中被覆盖。应该是$product_cat[] = value

标签: php arrays wordpress foreach


【解决方案1】:

尝试更改您的 foreach 以按以下方式工作:

foreach ($terms as $term) {

    $array2 = array (  
        'name' => $term->name,  
        'slug' => $term->slug,
        'id' => $term->term_id
    );





    $product_cat[] = $array2;
}

根据要求,输出应该是这样的:

Array ( [0] => Array ( [name] => Salades [slug] => salades [id] => 67 ) [1] => Array ( [name] => Aardappels [slug] => aardappels [id] => 23 ) [3] => Array ( [name] => Diverse fruit [slug] => diverse-fruit [id] => 11 )

Break 导致您在一次迭代后退出循环。正如 Sougata 所提到的,您将覆盖 product_cat,除非您将其附加到 product_cat[]

更新:假设您要对数组进行排序并且 'id' 是唯一的:

foreach ($terms as $term) {

    // check if its set, if not add it!
    if(!isset($product_cat[$term->term_id])){
        $product_cat[$term->term_id] = array('name'=>$term->name, 'slug' => $term->slug);
    }

}

【讨论】:

  • 输出结果为:Array ( [0] =&gt; Array ( [0] =&gt; Array ( [name] =&gt; Salades [slug] =&gt; salades [id] =&gt; 67 ) ) )
  • 那还只是第一次迭代吗?如果是这样,请检查以确保您的数据库调用返回您期望的数据
  • 你的权利结果很好,现在我想过滤重复的数组..如果我输入array_unique($product_cat);,那么我会再次得到一个数组..
  • 您不需要为此使用 array_unique。只需在添加之前检查它是否已经存在。注:除非您需要它们,否则您不应该从 DB 中获取这些行。这可能是数据库问题
  • ' [0] => 数组 ( [name] => Salades [slug] => Salades ) [1] => 数组 ( [name] => Aardappels [slug] => aardappels ) [ 2] => 数组([名称] => 沙拉 [slug] => 沙拉)'
【解决方案2】:

只需删除

break;

然后放

array_push( $catarray, $product_cat );

进入foreach循环

$catarray = array();
while ( have_posts() ) : the_post();
    global $post;
    $terms = get_the_terms( $post->ID, 'product_cat' );

    foreach ($terms as $term) {
        $array2 = array (  
            'name' => $term->name,  
            'slug' => $term->slug,
            'id' => $term->term_id
        );

        array_push( $catarray, $array2 );
    }

endwhile;
$categorielijst = array_unique($catarray);

echo '<pre>';
print_r($categorielijst);
echo '</pre>';

【讨论】:

  • 您好,解决方案很好。但它是一个分页页面,因此在第 1 页上您不会看到所有术语,而在第 2 页上您会看到不同的术语。但我希望我们得到所有条款。
猜你喜欢
  • 1970-01-01
  • 2012-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多