[英]Check each index of array if value of index is numeric print next index value - PHP
我想檢查數組的每個索引,如果其值為數字,則打印下一個鍵
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);
怎么做 ? 謝謝你的幫助
循環瀏覽並檢查。 如果成功,則打印下一個值-
foreach($your_array as $key=>$value) {
foreach($value as $key => $check_val) {
if(is_numeric($check_val)) {
echo $value[$ley+1];
}
}
}
您還需要照顧kay
。
$i = 0;
foreach($expkey as $key->$value)
{
if(is_numeric($value))
{
$next_val = $expkey[$i+1];
}
$i++;
}
只是另一個答案:
<?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 />";
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.