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 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图