在数组PHP中访问数组 [英] Accesing Array in an array PHP

查看:62
本文介绍了在数组PHP中访问数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3列的数组,其中第二列是php中的数组.第二列是数组可以有2-6行.

I have an array of 3 columns in which I have the second column which is an array in php.The second column which is an array can have 2-6 rows.

我将具有以下代码的键的数据放入数组

I put the data in the array with key with the below code

$data_array = array(
                  'name' =>$name, 
                  'category_name'=>$category_name,
                  'url'=>$url

);

上方$ data_array中的第二列$ category_name是一个数组,其数据如下所示:

The second column $category_name in the above $data_array is an array which has data like below

$category_name = array("India", "USA", "UK");

我想以以下格式回显数据,并最终将其添加到mysql数据库中

I want to echo the the data in the following format and eventually add it the mysql database

'Jim'
'India'
'www.google.com'

'Jim'
'USA'
'www.google.com'

'Jim'
'UK'
'www.google.com'

'John'
'India'
'www.yahoo.com'

'John'
'USA'
'www.yahoo.com'

'John'
'UK'
'www.yahoo.com'   

如果您发现第一行和第三行保持不变,而第二行根据category_name数组中的数据进行了更改

If you notice the first and third row remain same and the second row changes according to the data in the category_name array

我正在尝试使用下面的代码来实现这一目标,但没有任何输出

I am trying to use the below code to achieve this but I am getting no output

for($i=0;$i<count($NPR);$i++){

for($j=0;$j<count($NPR[$i]['category_name'][j]);$j++){

echo "'".$NPR[$i]['name']."'<br>";
echo "'".$NPR[$i]['category_name'][j]."'<br>";
echo "'".$NPR[$i]['url']."'<br>";
}
}

推荐答案

应为 count($ NPR [$ i] ['category_name']),您添加了不必要的 [j] .

It should be count($NPR[$i]['category_name']), you added an unnecessary [j] at the end of this.

但是,如果您使用 foreach

foreach ($NPR as $item) {
    foreach ($item['category_name'] AS $cat) {
        echo "'{$item['name']}'<br>";
        echo "'$cat'<br>";
        echo "'{$item['url']}'<br>";
    }
}

在调试代码时,请确保已启用错误报告. [j] 应该已经发出警告.

Make sure you have error reporting enabled when you're debugging code. [j] should have produced a warning.

这篇关于在数组PHP中访问数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆