dtu1747 2017-01-13 07:57
浏览 141
已采纳

foreach中的数组仅显示第一个数组

I am busy with a foreach loop but i can't find the problem. This code show only first array, and stops. The problem comes from the array in the foreach loop.

Here is my code:

<?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>';

?>

The result is:

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

)

And if i change $product_cat = $array2; for $product_cat = $term->name;

Than is the output:

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
)
  • 写回答

2条回答 默认 最新

  • douxia3505 2017-01-13 08:19
    关注

    Try changing your foreach to work as followed:

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

    As requested, the output should be something like:

    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 was causing you to exit the loop after one iteration. And as mentioned by Sougata you would be overwriting product_cat unless you append it with product_cat[]

    UPDATE: Assuming that you want to sort arrays and 'id' is unique:

    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);
        }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)