尝试访问数组值时PHP字符串偏移量非法
我可能在这里打得比我的体重稍微高一点,或者这是一个非常简单的问题,有一个简单的解决方案 我的问题 我无法访问foreach中的数组键值 在函数中创建数组时,数组似乎是重复的,当createdas时会进行双重迭代,如下图所示 步骤1:数组创建 第二步调用函数,使用数组;导致非法的字符串偏移 注意:您可以在上面发布的图像中看到数组值 问题 1为什么阵列看起来是重复的?请参见图 2如何修复非法的字符串偏移量,以及是什么原因导致了它 谢谢 更新 从$returnResults[]中删除[]后的新数组结构 在foreach循环结束后,matchOutput、matchScore等被追加到数组中。它不应该在循环中吗 如果需要多行,则需要通过如下方式维护其索引来创建数组:尝试访问数组值时PHP字符串偏移量非法,php,html,arrays,function,multidimensional-array,Php,Html,Arrays,Function,Multidimensional Array,我可能在这里打得比我的体重稍微高一点,或者这是一个非常简单的问题,有一个简单的解决方案 我的问题 我无法访问foreach中的数组键值 在函数中创建数组时,数组似乎是重复的,当createdas时会进行双重迭代,如下图所示 步骤1:数组创建 第二步调用函数,使用数组;导致非法的字符串偏移 注意:您可以在上面发布的图像中看到数组值 问题 1为什么阵列看起来是重复的?请参见图 2如何修复非法的字符串偏移量,以及是什么原因导致了它 谢谢 更新 从$returnResults[]中删除[]后的新数组结构
$returnResults = array(); // Initialize array
foreach ($results as $index => $result) {
$returnResults[$index] = array('picked' => $result['team'], 'homeScore' => $result['homeScore'],
'awayScore' => $result['awayScore'], 'homeTeam' => $result['homeTeam'],
'awayTeam' => $result['awayTeam'], 'score' => $result['score']);
//------> HERE ELEMENTS GETS APPENDED TO ARRAY
$pickedTeam = $result['team'];
if ($result['homeScore'] > $result['awayScore']) {
$matchOutcome = $result['homeTeam'];
$matchScore = $result['homeScore'];
$returnResults[$index]['matchOutcome'] = $matchOutcome;
$returnResults[$index]['matchScore'] = $matchScore;
}
.
.
elseif ($pickedTeam !== $matchOutcome) {
$margin = 'wrongPick';
$returnResults[$index]['margin'] = $margin;
}
}//end foreach
谢谢Samir,我会快速检查并让您知道否,不幸的是它没有解决问题仍然是非法字符串偏移和重复数组问题,但是数组结构已更改,请查看更新的问题。您是否希望查询返回多行?我希望多行是。确定。我现在明白这个问题了。检查我的最新答案。另外,似乎在代码中已经打印了两次数组,因此它可以显示两次。
<?php $picks = checkUserPicks('5');
foreach ($picks as $index => $pick){
?>
<tr>
<td>
<?php echo $pick['picked']; ?>
</td>
<td>
<?php echo $pick['matchOutcome']; ?>
</td>
<td>
<?php echo $pick['margin']; ?>
</td>
</tr>
<?php } ?>
$returnResults = array(); // Initialize array
foreach ($results as $index => $result) {
$returnResults[$index] = array('picked' => $result['team'], 'homeScore' => $result['homeScore'],
'awayScore' => $result['awayScore'], 'homeTeam' => $result['homeTeam'],
'awayTeam' => $result['awayTeam'], 'score' => $result['score']);
//------> HERE ELEMENTS GETS APPENDED TO ARRAY
$pickedTeam = $result['team'];
if ($result['homeScore'] > $result['awayScore']) {
$matchOutcome = $result['homeTeam'];
$matchScore = $result['homeScore'];
$returnResults[$index]['matchOutcome'] = $matchOutcome;
$returnResults[$index]['matchScore'] = $matchScore;
}
.
.
elseif ($pickedTeam !== $matchOutcome) {
$margin = 'wrongPick';
$returnResults[$index]['margin'] = $margin;
}
}//end foreach