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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?