1

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?

5
  • 1
    why do you explode an array to make a string? surely you want to implode?
    – andrew
    Commented Sep 1, 2014 at 1:31
  • @andrew: yes. but first i have to use explode as i can separate the number from company.
    – RK.
    Commented Sep 1, 2014 at 1:33
  • stackoverflow.com/questions/907806/…
    – The Alpha
    Commented Sep 1, 2014 at 1:34
  • 1
    Come on guys, where's the map / reduce answer ;)
    – Phil
    Commented Sep 1, 2014 at 1:35
  • oh,ok. I see what you're trying to do now, sorry about that
    – andrew
    Commented Sep 1, 2014 at 1:39

3 Answers 3

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
1
  • 1
    if company_ is constant, dagons solution is probably for the best
    – Kevin
    Commented Sep 1, 2014 at 1:37
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"
4
  • I think you want _, not -
    – Phil
    Commented Sep 1, 2014 at 1:34
  • 3
    str_replace() is probably quicker if "company_" never changes
    – user557846
    Commented Sep 1, 2014 at 1:34
  • @Dagon's right. A simple implode and str_replace would be faster and simpler
    – Phil
    Commented Sep 1, 2014 at 1:36
  • 2
    I want that first sentence framed for my office.
    – user557846
    Commented Sep 1, 2014 at 1:37
-1

PHP has a native function for this sanitization task: filter_var_array().

Code: (Demo)

$array = [
    'company_1',
    'company_10',
    'company_15',
];
var_export(
    filter_var_array(
        $array,
        FILTER_SANITIZE_NUMBER_INT,
    )
);

Output:

array (
  0 => '1',
  1 => '10',
  2 => '15',
)

This array can then be passed into a prepared statement using advice found in: How can I bind an array of strings with a mysqli prepared statement?. There shouldbe no passing in of the value direct into an SQL string.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.