检查数组的每个索引,如果索引的值是数字打印下一个索引值 - PHP

时间:2015-05-22 05:19:23

标签: php arrays

我想检查数组的每个索引,如果它的值是数字打印下一个键

print_r($expkey);

给出

Array
(
    [0] => ew-language
    [1] => en
    [2] => 0
    [3] => phrase
    [4] => locale
)

Array
(
    [0] => ew-language
    [1] => en
    [2] => phrase
    [3] => 1
    [4] => use_system_locale
)

[2] => 0时 打印phrase 何时[3] => 1 打印use_system_locale

function numeric($key) {
$i=0;
if(is_numeric(key($key))){
 $i++;
 //if($i = ) incomplete code for function 
}
}
 numeric($expkey);

怎么做?谢谢你的帮助

3 个答案:

答案 0 :(得分:1)

循环并检查。如果成功则打印下一个值 -

foreach($your_array as $key=>$value) {
    foreach($value as $key => $check_val) {
      if(is_numeric($check_val)) {
        echo $value[$ley+1];
      }
    }
}

您还需要处理kay

答案 1 :(得分:1)

$i = 0;
foreach($expkey as $key->$value)
{
    if(is_numeric($value))
    {
        $next_val = $expkey[$i+1];
    }
    $i++;
}

答案 2 :(得分:1)

另一个答案:

<?php
function getValueNextToFirstNumeric($arr) {
    for($i=0; $i<count($arr); $i++) {
        if(is_numeric($arr[$i]) && isset($arr[$i+1]))
            return $arr[$i+1];
    }
    return null;
}

// Your array:
$arr = array
(
    ['ew-language', 'en', 0, 'phrase', 'locale'],
    ['ew-language', 'en', 'phrase', 1, 'use_system_locale'],
    ['ew-language', 'en', 2, 'phrase', 'decimal_point']
);
foreach($arr as $a)
    echo getValueNextToFirstNumeric($a) ."<br />";
?>