我很难在多维数组中循环。在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
}
}
答案 0 :(得分:0)
尝试以下方法:
foreach ($values as $key => $value){
if ($key == '114') {
if($value['tag']==='A:TOKEN')
$sessiontoken = $value['value'];
}
}
答案 1 :(得分:0)
您的实际问题表明您希望始终返回密钥'114'
。在这种情况下,您根本不需要循环,只需引用该键:
$sessiontoken = $vals['114']['value'];
但是,我认为你实际上想要的是找到'tag'
'A:TOKEN'
的任何元素,如下所示:
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
}
答案 2 :(得分:-1)
假设您要查找仅包含在您的一个结果中的会话令牌。执行以下操作:
foreach ($vals as $i => $array_value){
if (isset($array_value[$i]['value']) {
$sessiontoken = $array_value[$i]['value'];
break; // stop looping after it is found
}
}
答案 3 :(得分:-1)
这里的一个班轮应该是固定的:
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'];
}
}
}
}