且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

循环多维数组中使用PHP

更新时间:2023-01-17 17:31:38

您的实际问题,建议你正在寻找总是返回键'114'。在这种情况下,你不需要一个循环所有,只是引用键:

  $ sessiontoken = $瓦尔斯['114'] ['值'];

不过,我觉得你的真正的希望是找到任何元素'A的标记 :TOKEN,这将是这样的:

 的foreach($丘壑为$ I => $行){
    //这里,$行从$丘壑数组一个项目
    //你不想去想丘壑$的条件下,只需$行
    如果($行['标签'] =!'A:令牌'){
        继续; //有PHP中没有下一个关键词
    }
    其他{
        $ sessiontoken = $行['值'];
    }
}

我可能会换的,如果像这样在一旁,虽然:

 的foreach($丘壑为$ I => $行){
    如果($行['标签'] =='A:令牌'){//正测试更易于阅读
        $ sessiontoken = $行['值'];
    }
    //你可以看看其他的标签,同时与ELSEIF(...)
    其他{
         继续; //只有在没有你的条件得到满足
    }
    //如果出现的其他结束后没什么的,
    //你不需要继续在所有
}

I'm having a hard time looping in a multidimensional array. I'm not an expert of some sort when it comes to PHP. What I want to happen is to search for a certain field. If it hits the right field, then it will grab the data and store in a variable and if it does not hit the right field, it will continue to search for the right field.

Here's the array

[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
    )

and here's my code and I would like to apologize first for not being able to complete it. :D ......i have totally no idea on what to place.....and searching is making me more confuse...sorry....

here's the code

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

Your actual question suggests you are looking to always return key '114'. In that case, you don't need a loop at all, just reference that key:

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

However, what I think you actually want is to find whichever element has a 'tag' of 'A:TOKEN', which would look like this:

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'];
    }
}

I'd probably swap the if around like this, though:

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
}