循环多维数组中使用PHP [英] Looping in a multidimensional Array using PHP
问题描述
我有一个很难多维数组中循环。我不是某种类型的专家,当涉及到PHP。我希望发生的是寻找某一个领域。如果它击中右场,那么它会获取数据并存储在一个变量,如果它没有击中右外场,它会继续寻找合适的领域。
下面的阵列
[111] =>排列
(
[标签] =>乙:VALUE
[类型] = GT;关
[等级] => 7
)[112] =>排列
(
[标签] =>乙:KEYVALUEOFINTHEALTHAGENCYD9J3W_PIR
[类型] = GT;关
[等级] => 6
)[113] =>排列
(
[标签] =>答:AGENCIES
[类型] = GT;关
[等级] =>五
)[114] =>排列
(
[标签] =>答:TOKEN
[类型] = GT;完成
[等级] =>五
[值] => vy8BMS8nDIFdQWRTb6wyNDGGUMgBzHtOXU6mHqZgdxhRAbi0qkwluK9pjt03OQyf
)[115] =>排列
(
[标签] => LOGINCAREGIVERPORTALRESULT
[类型] = GT;关
[等级] => 4
)[116] =>排列
(
[标签] => LOGINCAREGIVERPORTALRESPONSE
[类型] = GT;关
[等级] => 3
)[117] =>排列
(
[标签] =>小号:BODY
[类型] = GT;关
[等级] => 2
)[118] =>排列
(
[标签] =>小号:信封
[类型] = GT;关
[等级] => 1
)
这是我的code,我想道歉,首先不能够完成它。 :D ......我有什么的地方完全不知道.....和搜索是让我更加迷惑......对不起......
这里的code
$最后=计数($丘壑) - 1;
的foreach($丘壑为$ I => $行){
如果(!$瓦尔斯=='114'){
下一个
}
其他{
$ sessiontoken =< ------这里存储的价值
}
}
您的实际问题,建议你正在寻找总是返回键'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
}
这篇关于循环多维数组中使用PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!