-1

I want to implode my id values that I keep from database.

My data in database wp_badges

id    badges        value_id
----  ------        --------
1     Outloot       2719,3314 

My id values that I saved as value is actually become my product id

Following my code :PHP

 $result = $wpdb->get_results( "SELECT DISTINCT values_id FROM wp_badges");
    foreach($result as $value){

     $value->values_id;
    }
    $arr = array($value->values_id);
    echo '"'.implode('","',$arr).'"';

Here is the output:"2719,3314"

But the output I wanted was to get each element with double quotes

The output I want:"2719","3314"

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
JFeel
  • 189
  • 2
  • 10
  • 1
    Duplicate of https://stackoverflow.com/questions/5928599/equivalent-of-explode-to-work-with-strings-in-mysql ? – sianipard Nov 09 '21 at 10:20
  • 2
    If you have a comma separated string, putting it in an array and running `implode()` on it doesn't make sense since that would implode an array with that string as one single element. – M. Eriksson Nov 09 '21 at 10:22
  • https://stackoverflow.com/questions/3653462/is-storing-a-delimited-list-in-a-database-column-really-that-bad – RiggsFolly Nov 09 '21 at 10:24
  • 2
    Does this answer your question? [Equivalent of explode() to work with strings in MySQL](https://stackoverflow.com/questions/5928599/equivalent-of-explode-to-work-with-strings-in-mysql) – Utmost Creator Nov 10 '21 at 02:50

1 Answers1

2

You are doing things in the wrong place, the explode and echo has to be inside the loop or you will only ever do anything with the last occurance in the $result array.

$result = $wpdb->get_results( "SELECT DISTINCT values_id FROM wp_badges");
foreach($result as $value){
    $bits = explode(',',$value->values_id);
    echo '"' . implode('","', $bits) . '"<br>';
}

RESULT

"123","456"<br>
"923","956"<br>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149