在PHP中只从数组中获取数值


Get only Numeric values from Array in PHP

我有一个数组,看起来像这样:

Array ( 
[0] => Array ( [country_percentage] => 5 %North America ) 
[1] => Array ( [country_percentage] => 0 %Latin America )
)

我只想要上面数组中的数值。我想要我的最后一个阵列像这个

Array ( 
[0] => Array ( [country_percentage] => 5) 
[1] => Array ( [country_percentage] => 0)
)

我是如何使用PHP实现这一点的??提前感谢。。。

当数字位于第一个位置时,可以像这样进行int强制转换:

$newArray = [];
foreach($array => $value) {
   $newArray[] = (int)$value;
}

我想您可以循环二维数组并使用preg_replace,即:

for($i=0; $i < count($arrays); $i++){
    $arrays[$i]['country_percentage'] = preg_replace( '/[^'d]/', '', $arrays[$i]['country_percentage'] );
}

Ideone演示


根据您的评论更新:

for($i=0; $i < count($arrays); $i++){
    if( preg_match( '/North America/', $arrays[$i]['country_percentage'] )){
       echo preg_replace( '/[^'d]/', '', $arrays[$i]['country_percentage'] );
  }
}

试试这个:

$arr = array(array('country_percentage' => '5 %North America'),array("country_percentage"=>"0 %Latin America"));
$result = array();
foreach($arr as $array) {
  $int = filter_var($array['country_percentage'], FILTER_SANITIZE_NUMBER_INT);
  $result[] = array('country_percentage' => $int);
}

试试这个:-

$arr =[['country_percentage' => '5 %North America'],
       ['country_percentage' => '0 %Latin America']];
$res = [];
foreach ($arr as $key => $val) {
    $res[]['country_percentage'] = (int)$val['country_percentage'];
}
echo '<pre>'; print_r($res);

输出:-

Array
(
    [0] => Array
        (
            [country_percentage] => 5
        )
    [1] => Array
        (
            [country_percentage] => 0
        )
)

您可以使用array_walk_recursive来消除循环,
传递回调的第一个参数作为修改初始数组值的引用
然后应用filter_varintval,如前面提到的其他答案。

$array = [
    ["country_percentage" => "5 %North America"],
    ["country_percentage" => "0 %Latin America"]
];
array_walk_recursive($array, function(&$value,$key){
    $value = filter_var($value,FILTER_SANITIZE_NUMBER_INT);
    // or 
    $value = intval($value);
});
print_r($array);

将输出

Array
(
    [0] => Array
        (
            [country_percentage] => 5
        )
    [1] => Array
        (
            [country_percentage] => 0
        )
)

您可以通过循环遍历数组来获得所有nemeric值。然而,我不认为这是最有效和好看的答案,我无论如何都会发布它。

// Array to hold just the numbers
$newArray = array();
// Loop through array
foreach ($array as $key => $value) {
    // Check if the value is numeric
    if (is_numeric($value)) {
        $newArray[$key] = $value;
    }
}

我误解了你的问题。

$newArray = array();
foreach ($array as $key => $value) {
    foreach ($value as $subkey => $subvalue) {
        $subvalue = trim(current(explode('%', $subvalue)));
        $newArray[$key] = array($subkey => $subvalue);
    }
}

如果您想要除数值以外的所有值:

$array[] = array("country_percentage"=>"5 %North America");
$array[] = array("country_percentage"=>"3 %Latin America");
$newArray = [];
foreach ($array as $arr){
foreach($arr as $key1=>$arr1) { 
       $newArray[][$key1] = intval($arr1);
  }
}
echo "<pre>";
print_R($newArray);

这是一种简单的方法,因为我喜欢使用尽可能多的预制函数。但这应该对你有效:D

$array = array('jack', 2, 5, 'gday!');
$new = array();
foreach ($array as $item) {
  // IF Is numeric (each item from the array) will insert into new array called $new.
  if (is_numeric($item)) { array_push($new, $item); } 
}