Roby Firnando Yusuf
Roby Firnando Yusuf

Reputation: 449

PHP extract number only from result explode

I have explode here is the result of var_dump:

result of explode

and I want get the result from explode:

7 [remove other string]
0 [if contain "-"]
0 [if contain "-"]
0 [if contain "-"]

Here, I just used comma as delimiter:

var_dump (explode(",", $rowData[0][13]));
die();

Does anyone have the solution to solved this?

Thank You.

Upvotes: 2

Views: 103

Answers (2)

Maulik patel
Maulik patel

Reputation: 2436

<?php
$data_result = explode(",", $rowData[0][13])

for($data_count=0;$data_count<count($data_result);$data_count++)
{
    if(substr($data_result[$data_count], -1) == '-')    
    {
        echo $data_result[$data_count]
    }
}
?>

Upvotes: 0

Tamil Selvan C
Tamil Selvan C

Reputation: 20219

Use array_map() function and in it use filter_var() to sanitize number value.

Try

$rowData = ['JJ7', 'B-', 'S-', 'M-'];

$result = array_map(function($v) { 
    return abs((int) filter_var($v, FILTER_SANITIZE_NUMBER_INT)); 
}, $rowData );

var_dump( $result );

//output
// array(4) { [0]=> int(7) [1]=> int(0) [2]=> int(0) [3]=> int(0) } 

Upvotes: 1

Related Questions