简体   繁体   中英

How to Make values of Array Comma separated

What I need:

  • number separated by (,) .

      like : 42166 ,42167 , 42168,42169 etc. 

problem im facing :

  • the number is not appended by(,).

  • im using implode function.

Array Structure:

 Array
(
[id] => 42166
[Company_Website] => http://www.amphenol-highspeed.com/
[company_name] => Amphenol High Speed Interconnect
[city_name] => New York
[country_name] => USA
[comp_img] => 
 )

42166Array 
(
  [id] => 42167
[Company_Website] => http://www.clearfieldconnection.com/
[company_name] => Clearfield, Inc.
[city_name] => Plymouth
[country_name] => USA
[comp_img] => 
) 

Php Code:

    foreach ($result as $key=>$value) {

    echo $company_id= implode(",",(array)$value['id']);
      }

       output im getting:  412664127741288 etc.
foreach ($result as $key=>$value) {
    $company_id[] = $value['id'];
}
$company_id = implode (',', $company_id);

Edit : Sorry that I misread your question at first time

$i=0;
$company_id = "";
foreach ($result as $key=>$value) {
    $company_id .= ($i==0)?$value['id']:','.$value['id'];
    $i++;
}
echo $company_id;

If you want to convert Id values into comma separated then0 try by string manipulation and append comma (,) with each id value

$company_id = ""
foreach ($result as $key=>$value) {
    $company_id .= $value['id'].",";
}
echo rtrim($company_id, ",");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM