douqin2108 2017-11-10 03:02
浏览 99
已采纳

在查询循环中构建一个具有相同ID的字符串Concat数组

i have a query inside a for loop that getting the product name of every array element. Now in every element of my array, i have an ID, where i want to concat all product names with the-same shipping_id.

Here i have my array with values like these:

Array name:id with values of:

Array
(
   [0] => Array
        (
            [product_id] => 1
            [shipping_id] => 1
        )
   [1] => Array
        (
            [product_id] => 2
            [shipping_id] => 1
        )

   [2] => Array
        (
            [product_id] => 1
            [shipping_id] => 2
        )
)

now i made this code with these:

   $first = true;
    $temp_ship_id = "";
    $product_list = "";
    foreach ($ids as $product) { 
        $productname = $this->getproductname($product[0][product_id]);
        // if($first) {
        //     $temp_ship_id = $product[0][shipping_id];                    
        //     $first = false;
        // }
        // if($product[0][shipping_id] == $temp_ship_id) {
        //     $product_list .= $productname.";
        // } else {
        //     $product_list .= $productname.";
        //     //$product_list = "";                    
        //     $temp_ship_id = $product[0]->shipping_id;
        // }
    }

    public function getproductname($product_id) {
            $product = DB::table('products')->select('product_name')
                ->where(['products.product_id'=>$product_id])
                ->first();


            return $product->product_name;    
    }

what am i doing is, i am getting the first shipping id and store it and i made a condition if they are thesame then i go concat the productname but, i see my logic is bad.

Please help me in other way. Something like This line of code to begin with:

foreach ($ids as $product) { 
    $productname = $this->getproductname($product[0][product_id]);
    //code for concat goes here
}

public function getproductname($product_id) {
        $product = DB::table('products')->select('product_name')
            ->where(['products.product_id'=>$product_id])
            ->first();


        return $product->product_name;    
}
  • 写回答

1条回答 默认 最新

  • douzong6649 2017-11-10 03:28
    关注

    Adjust below to your actual data, let me know if you have questions.

    <?php
        $concat = array();
        $array  = array( array( 'product_id'=>1, 'shipping_id'=>1, 'product_name' => 'a' ), array( 'product_id'=>2, 'shipping_id'=>1, 'product_name' => 'b' ), array( 'product_id'=>3, 'shipping_id'=>2, 'product_name' => 'c' ), array( 'product_id'=>4, 'shipping_id'=>1, 'product_name' => 'd' ) );
        foreach( $array as $row ) {
            if( isset( $concat[ $row['shipping_id'] ] ) ) {
                $concat[ $row['shipping_id'] ] .= ',' . $row['product_name'];
            } else {
                $concat[ $row['shipping_id'] ] .= $row['product_name'];
            }
        }
        var_dump( $concat );
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记