0

I have used implode to separate the array. What I want is that my ids should be shown in brackets separated by commas, but after the last string there will be no comma. How can I show them in brackets with no comma after the last string?

<html>
   <head>
   </head>

   <body>
      <?php
         mysql_connect("localhost", "usename", "password") or die(mysql_error()); 
         mysql_select_db("dbname") or die(mysql_error()); 

         $ids=array();
         $data = mysql_query("SELECT id FROM information"); 
         while($row = mysql_fetch_assoc($data))  
         {
            $ids[]=$row["id"]; 
         } 
         echo implode(", ", $ids);
      ?>
   </body>
</html>
10
  • I think its better to show an example! Commented Jul 23, 2014 at 6:21
  • What is the output you get? Commented Jul 23, 2014 at 6:22
  • but using implode, no comma will get add after last string Commented Jul 23, 2014 at 6:22
  • Expected output just mention here. Commented Jul 23, 2014 at 6:23
  • 2
    implode doesn't add a glue after the last one array item. Commented Jul 23, 2014 at 6:24

2 Answers 2

7
$arr = [1,2,3,4,5,6,7,8,9,10,12,13]; // array(1,2,3,4,5,6,7,8,9,10,12,13); for versions below 5.4

$str = "(".implode("), (", $arr).")";

echo $str;

Results in

(1), (2), (3), (4), (5), (6), (7), (8), (9), (10), (12), (13)

If that's your expected behavior, you just add the opening and closing bracket as delimiters, which will separate the strings by it, but will result in lack of opening and closing bracket at the beginning and at the end (as it adds the glue AFTER each element, except the last), so you add it manually with concatenation.

It will take 1 and 2 for example and will add after 1 (because 2 is the last, it will only add AFTER 1) the delimiter ), ( will result in 1), (2, thus adding the concatenated ( and ) results in (1), (2)

Sign up to request clarification or add additional context in comments.

Comments

0

Provided I followed your question correctly. You'd use something like this

(We're passing variables by reference in this example):

foreach($ids as $i => &$id) {
    $id = "({$id})";
}

$string = implode(", ", $ids);

Which will end up returning something like this:

(56), (74), (88), (100), (91), (61), (43), (47), (77), (2), (78), (61), (44), (6), (74), (90), (14), (86), (80)

Here is an example (Note: It just uses randomly generated id's)


In response to your comment:

Even with strings, this function works. See this example

5 Comments

This will suit the OP needs, but it's an overkill where you are mutating the array for purpose of late presentation, which is also a bad practice according to me. Afterwards each time you try to do something with that array, you will have brackets around each element
@RoyalBg That is a good point. I wrote this answer assuming the sole purpose of the OP was to create the string that he is echo'ing out to the DOM, as seen in his example.
your code works in numbers,but whenever i wanted bring simple string for exapmle names of tv the last string i get is something like this - ' CNN ', 'AMC', '' there is a extra comma plus two apostrophe signs
@user3815328 Could you supply an example of that set of strings?
@user3815328 is that not what you wanted?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.