如何使Array Comma的值分开

时间:2014-08-14 10:00:05

标签: php

我需要什么:

  • 以(,)分隔的数字。

       like :  42166 ,42167 , 42168,42169 etc.
    

面临的问题:

  • 该号码未附加(,)。

  • 我正在使用内爆函数。

数组结构:

 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代码:

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

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

       output im getting:  412664127741288 etc.

3 个答案:

答案 0 :(得分:1)

foreach ($result as $key=>$value) {
    $company_id[] = $value['id'];
}
$company_id = implode (',', $company_id);

编辑:很抱歉我第一次误读了您的问题

答案 1 :(得分:1)

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

答案 2 :(得分:0)

如果要将Id值转换为逗号分隔,则尝试通过字符串操作并使用每个id值附加逗号(,)

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