简体   繁体   中英

Check each index of array if value of index is numeric print next index value - PHP

I want to check each index of array if its value is numeric print next key

print_r($expkey);

gives

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
)

when [2] => 0 print phrase when [3] => 1 print use_system_locale

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

HOW TO DO IT ? THANK YOU FOR HELP

Loop through it and check. if succeed then print next value -

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

You need to take care of the kay s also.

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

Just another answer:

<?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 />";
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM