爆炸php数组

时间:2014-09-01 01:27:31

标签: php arrays

我有以下数组结果:

Array ( [0] => company_1 [1] => company_10 [2] => company_15 ) 

我希望结果像我在mysql IN查询子句中使用的那样:

$result= "1, 10, 15";

我使用explode函数,但它再次分别返回两个数组值:

Array ( [0] => Array ( [0] => company [1] => 1 ) [1] => Array ( [0] => company [1] => 10 ) [2] => Array ( [0] => company [1] => 15 ) )   

但是想知道如何才能分别获取数字并存储在一个字符串中。

有任何帮助吗? '

2 个答案:

答案 0 :(得分:3)

如果您的初始数据是一个数组,只需将它们再次爆炸,然后将它们聚集在一起,最后使用implode。例如:

$result = array('company_1', 'company_10', 'company_15');
$result = array_map(function($piece){
    return explode('_', $piece)[1]; // note: for php 5.4 or greater
    // return str_replace('company_', '', $piece);
}, $result);
$result = implode(', ', $result);
echo $result; // 1, 10, 15

答案 1 :(得分:1)

爆炸后你必须选择第二部分。

$result = array_map(function ($val) {
  $tmp = explode('_', $val);
  return isset($tmp[1]) ? $tmp[1] : null;
}, $array);

$result = implode(',', $result); // "1,10,5"
相关问题