2

I have following array result :

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

I want the result something like this as i can use in the mysql IN query clause:

$result= "1, 10, 15";

I use explode function but again it is returning both array values separately like this:

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

But wondering how can i only get numbers separately and stored in one string.

Any help ? '

RK.
  • 973
  • 5
  • 19
  • 45

2 Answers2

3

If your initial data is an array, just explode them again each them gather them inside then finally use implode. Example:

$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
Kevin
  • 41,694
  • 12
  • 53
  • 70
1

You have to pick the second part after you explode.

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

$result = implode(',', $result); // "1,10,5"
xdazz
  • 158,678
  • 38
  • 247
  • 274