在循环php中从多维数组调用键
我正在我的多维数组中循环$阵列1在循环php中从多维数组调用键,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,我正在我的多维数组中循环$阵列1 for($index=0; $index < count($anotherArray); $index++){ '"data-example "' = . $array1[$index]["Number"]. 我试图将所有“Number”字段作为javascript数据元素传递。 我犯了一个错误 Notice: Undefined index: Number 正如其他人所指出的,您不应该对计数使用不同的数组。您如何确定$anotherArray和
for($index=0; $index < count($anotherArray); $index++){
'"data-example "' = . $array1[$index]["Number"].
我试图将所有“Number”字段作为javascript数据元素传递。
我犯了一个错误
Notice: Undefined index: Number
正如其他人所指出的,您不应该对计数使用不同的数组。您如何确定
$anotherArray
和$array1
始终包含相同数量的项目
有几种方法可以修复代码
选项1-使用isset
确保两个数组中的索引匹配:
for($index=0; $index < count($anotherArray); $index++){
if (isset($array1[$index])) {
'"data-example "' = . $array1[$index]["Number"].
}
选项3-切换到aforeach
,这样您就不必担心索引:
foreach ($array1 as $index => $data) {
'"data-example "' = . $data["Number"].
由于您没有包含完整的代码,我将由您决定最佳的实现方式。确保您应该使用什么:
$anotherArray
或$array1
$anotherArray与问题无关,它只是进行计数。请提供完整的代码。为什么要将其标记为JS?在这个问题中,我在JS中看不到任何内容。count($anotherArray)
的值是多少?。如果高于count($array1)代码>那么你就有问题了。通过每次回显$index
的值,您可以轻松检查错误发生在循环的哪个迭代上。$anotherArray和$array1的计数相同。
for($index=0; $index < count($array1); $index++){
'"data-example "' = . $array1[$index]["Number"].
foreach ($array1 as $index => $data) {
'"data-example "' = . $data["Number"].