使用PHP在多维数组中循环



我很难在多维数组中循环。说到PHP,我不是什么专家。我想要的是寻找一个特定的领域。如果它命中正确的字段,那么它将抓取数据并存储在一个变量中,如果它没有命中正确的字段,它将继续搜索正确的字段。

这是数组

[111] => Array
    (
        [tag] => B:VALUE
        [type] => close
        [level] => 7
    )
[112] => Array
    (
        [tag] => B:KEYVALUEOFINTHEALTHAGENCYD9J3W_PIR
        [type] => close
        [level] => 6
    )
[113] => Array
    (
        [tag] => A:AGENCIES
        [type] => close
        [level] => 5
    )
[114] => Array
    (
        [tag] => A:TOKEN
        [type] => complete
        [level] => 5
        [value] => vy8BMS8nDIFdQWRTb6wyNDGGUMgBzHtOXU6mHqZgdxhRAbi0qkwluK9pjt03OQyf
    )
[115] => Array
    (
        [tag] => LOGINCAREGIVERPORTALRESULT
        [type] => close
        [level] => 4
    )
[116] => Array
    (
        [tag] => LOGINCAREGIVERPORTALRESPONSE
        [type] => close
        [level] => 3
    )
[117] => Array
    (
        [tag] => S:BODY
        [type] => close
        [level] => 2
    )
[118] => Array
    (
        [tag] => S:ENVELOPE
        [type] => close
        [level] => 1
    )

,这是我的代码,我想道歉,首先不能完成它。D:……我完全不知道该放什么.....搜索让我更困惑…对不起....

下面是代码

$last = count($vals) - 1;
foreach ($vals as $i => $row) {
    if (!$vals == '114') {
        next
    }
    else {
        $sessiontoken = <------store the value here
    }
}

尝试如下:

foreach ($values as $key => $value){
    if ($key == '114') {
            if($value['tag']==='A:TOKEN')
               $sessiontoken = $value['value'];
    }
}

您的实际问题表明您希望始终返回键'114'。在这种情况下,根本不需要循环,只需要引用那个键:

$sessiontoken = $vals['114']['value'];

然而,我认为你实际上想要的是找到具有'A:TOKEN''tag'的元素,它看起来像这样:

foreach ($vals as $i => $row) {
    // Here, $row is one item from the $vals array
    // You don't want to think about $vals in the condition, just $row
    if ( $row['tag'] != 'A:TOKEN' ) {
        continue; // there's no "next" keyword in PHP
    }
    else {
        $sessiontoken = $row['value'];
    }
}

我可能会像这样交换if:

foreach ($vals as $i => $row) {
    if ( $row['tag'] == 'A:TOKEN' ) { // Positive tests are easier to read
        $sessiontoken = $row['value'];
    }
    // you can look for other tags at the same time with elseif ( ... )
    else {
         continue; // only if none of your conditions are met
    }
    // if there's nothing after the end of the else, 
    //  you don't need the continue at all
}

假设您想要查找只包含在一个结果中的会话令牌。

foreach ($vals as $i => $array_value){
    if (isset($array_value[$i]['value']) {
        $sessiontoken = $array_value[$i]['value'];
        break; // stop looping after it is found
    }
}

这一行应该修改:

if (!$vals == '114') {

假设如果您在位置114找到所需的项目,则希望存储会话,您应该这样做。我假设你在位置114有一个数组,然后你有指向一个值的键?似乎从你的描述,每个键实际上是一个数组,指向一个值?假设它是一个常规的键值对,您可以尝试以下操作:

foreach ($vals as $key=>$value)
{
    if ($key !== '114')
    {
        continue;
    } else {
        // check if value exists
        if (!empty($value['value'])
        {    
             $sessionToken = $value['value'];  
        }
    }
}

如果它不是键值对,那么您需要将其调整为:

foreach ($vals as $key=>$value)
{
    if ($key !== '114')
    {
        continue;
    } else {
        foreach ($value as $keyArray => $val) 
        {
           if ($keyArray == 'value')
            {    
                 $sessionToken = $value['value'];  
            }
        }
    }
}

相关内容

  • 没有找到相关文章

最新更新