如何从查询reults的在PHP codeigniter数组一个逗号分隔的字符串 [英] how to create a comma separated string from an array of query reults in php codeigniter

查看:132
本文介绍了如何从查询reults的在PHP codeigniter数组一个逗号分隔的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个查询返回值的数组。我的任务是转换这个结果逗号分隔值,如:

I have a query which returns an array of values. My task is to convert this results as comma separated values like:

值1,值....值N

我试图使用爆炸和内爆..但它没有工作。可我不使用它在适当的way..Here是我的code:

I tried using explode and implode.. But it didnt worked. May be am not using it in the proper way..Here's my code:

查询

    $this->db->select("product_name")
    ->from('sale_items')
    ->where('sale_items.sale_id',4221);
    $q1 = $this->db->get();
    if ($q1->num_rows() > 0) {
     foreach (($q1->result()) as $row1) {
    $prdtarray[] = explode(",", $row1->product_name);
    $data1[] = $row1;
    }
    echo print_r($prdtarray);
    $product=implode(',',$prdtarray); 
    echo $product ;

的结果是:

$ prdtarray
    阵列([0] =>阵列([0] =>鞋垫premium邵氏30)[1] =>阵列([0] =G型糖尿病premium关闭凉鞋黑色尺寸09))1

$prdtarray Array ( [0] => Array ( [0] => Insole Premium Shore 30 ) [1] => Array ( [0] => G Diabetic Premium closed Sandal Black size 09 ) ) 1

$产品

Array,Array

我的预期的结果是:

My expected result is :

    (  Insole Premium Shore 30 ,G Diabetic Premium closed Sandal Black size 09 ) 

我怎样才能做到这一点?谁能帮我了..

How can I achieve this?? Can anyone help me out..

推荐答案

爆炸破灭是否按预期工作。第一个是用来分割字符串,并与片返回数组例如

explode and implode are working as expected. The first is used to split a string and return an array with the pieces e.g.

 $pizza  = "piece1 piece2 piece3 piece4 piece5 piece6";
 $pieces = explode(" ", $pizza);
 echo $pieces[0]; // piece1
 echo $pieces[1]; // piece2

这就是为什么你会得到这样的:阵列([0] =>阵列([0] =>鞋垫premium邵氏30)[1] =>阵列([0] =G型糖尿病premium封闭凉鞋黑色尺寸09))当您打印$ prdtarray(回声的print_r($ prdtarray); )。每次通话时间 $ prdtarray [] =爆炸(,$ row1-> PRODUCT_NAME); 您要创建一个新的数组与一个元素(只有一个元素,因为 $ row1-方式> PRODUCT_NAME 不包含任何),并将其添加到 $ prdtarray

That is why you get this: Array ( [0] => Array ( [0] => Insole Premium Shore 30 ) [1] => Array ( [0] => G Diabetic Premium closed Sandal Black size 09 ) ) when you print $prdtarray (echo print_r($prdtarray);). Each time you call $prdtarray[] = explode(",", $row1->product_name); you are creating a new array with one element (only one element because $row1->product_name doesn't contains any ",") and adding it to $prdtarray.

破灭用来加入一个字符串数组元素例如

implode is used to Join array elements with a string e.g.

$array = array('lastname', 'email', 'phone');
$comma_separated = implode(",", $array);

echo $comma_separated; // lastname,email,phone

在您的code,因为你有 $ prdtarray $产品的结果里面两个数组

In your code since you have two arrays inside $prdtarray the result of $product is

 Array,Array

所以,你可以保存 $ row1-> PRODUCT_NAME 在数组中值,然后使用来创建一个逗号分隔值造成的。在code是这样的:

So, you can save the $row1->product_name value in an array and then use implode to create a comma separated values result. The code looks like this:

$this->db->select("product_name")
->from('sale_items')
->where('sale_items.sale_id',4221);
$q1 = $this->db->get();
if ($q1->num_rows() > 0) {
 foreach (($q1->result()) as $row1) {
$prdtarray[] = $row1->product_name;
}
$product=implode(',',$prdtarray); 
echo $product ; // Insole Premium Shore 30,G Diabetic Premium closed Sandal Black size 09

这篇关于如何从查询reults的在PHP codeigniter数组一个逗号分隔的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆