1

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

value1,value2....valuen

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

Query

    $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 ;

The result is:

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

$product

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..

2
  • Can you print your result here? with pre tag? Commented Jan 14, 2016 at 11:23
  • $prdtarray[] = $row1->product_name; Commented Jan 14, 2016 at 11:25

3 Answers 3

2

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

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

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

 Array,Array

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
Sign up to request clarification or add additional context in comments.

1 Comment

I tried like $prdt.=$row1['product_name'].","; nd it worked.. I tried as u said nd it works too.. thanks
1

Something like this:

$result = "";
foreach ($prdtarray[0] as $prdtarray_key => $prdtarray_value) {
    $result .= $prdtarray_value . ", ";
}

Because everything is wrapped inside element 0 of $prdtarray we use that element directly. Next we loop over every element and build 1 string form it by concatenating .= and also adding , (comma space) after every element we add. This will result in trailing comma space, but you can figure it out from there, right?

Comments

0

Hope this helps you .

$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_array()) as $row1) {
    $prdtarray[] = $row1['product_name'];
    }
    $product=implode(',',$prdtarray); 
    echo $product ;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.