Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在循环php中从多维数组调用键_Php_Arrays_Multidimensional Array - Fatal编程技术网

在循环php中从多维数组调用键

在循环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和

我正在我的多维数组中循环$阵列1

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-切换到a
foreach
,这样您就不必担心索引:

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"].