PHP:访问数组变量

发布于 2024-09-19 17:25:38 字数 538 浏览 4 评论 0原文

有人可以帮我访问这个数组吗?我在索引方面遇到了问题。

array(10) {
    [0]=>array(2) {
        ["t"]=>array(1) {
            ["tag"]=>string(3) "php"
        }
        [0]=>array(1) {
            ["NumOccurrances"]=>string(1) "2"
        }
    }
    [1]=>array(2) {
        ["t"]=>array(1) {
            ["tag"]=>string(6) "Tomcat"
        }
        [0]=>array(1) {
            ["NumOccurrances"]=>string(1) "1"
        }
    }
}

我想在显示为“PHP x 2”的 foreach 循环中使用它,但索引遇到问题

谢谢

Jonesy

Can someone help me access this array please I'm having trouble with indexes.

array(10) {
    [0]=>array(2) {
        ["t"]=>array(1) {
            ["tag"]=>string(3) "php"
        }
        [0]=>array(1) {
            ["NumOccurrances"]=>string(1) "2"
        }
    }
    [1]=>array(2) {
        ["t"]=>array(1) {
            ["tag"]=>string(6) "Tomcat"
        }
        [0]=>array(1) {
            ["NumOccurrances"]=>string(1) "1"
        }
    }
}

I want to use it in a foreach loop displaying like "PHP x 2" but am having trouble with the indexes

Thanks

Jonesy

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

守不住的情 2024-09-26 17:25:39

这个有作用吗?

foreach ($tags as $t) {
    echo $t['t']['tag'].' x '.$t[0]['NumOccurrances'].'<br />';
}

结构看起来有点奇怪。如果这没有帮助,请提供数组的其余部分。

Does this do?

foreach ($tags as $t) {
    echo $t['t']['tag'].' x '.$t[0]['NumOccurrances'].'<br />';
}

The structure seems a bit weird. If this does not help, please provide the rest of array.

幼儿园老大 2024-09-26 17:25:39
foreach( $a as $item ) {
    echo $item['t']['tag'] . 'x' . $item[0]['NumOccurrances'] . '<br>';
}
foreach( $a as $item ) {
    echo $item['t']['tag'] . 'x' . $item[0]['NumOccurrances'] . '<br>';
}
一城柳絮吹成雪 2024-09-26 17:25:39

我不会在这里使用 foreach 循环。 foreach 创建数组的副本,因此不如 for 循环高效。由于您的第一个维度是数字索引的,我会执行以下操作:

$count = count($array);
for ($i = 0; $i < $count; ++$i){
  echo $array[$i]['t']['tag'] . " x " . $array[$i][0]['NumOccurrances'];
}

我同意 vassilis 的观点,即数组结构是奇怪的。

I wouldn't use a foreach loop here. foreach creates a copy of the array and therefore is not as efficient as a for loop. Since your first dimension is numerically indexed, I would do the following:

$count = count($array);
for ($i = 0; $i < $count; ++$i){
  echo $array[$i]['t']['tag'] . " x " . $array[$i][0]['NumOccurrances'];
}

I agree with vassilis that the array structure is odd.

陈甜 2024-09-26 17:25:38

类似的东西

foreach($array as $entity)
{
    echo $entity['t']['tag'] . ' x ' . $entity[0]['NumOccurrances']; 
}

会起作用。

something like

foreach($array as $entity)
{
    echo $entity['t']['tag'] . ' x ' . $entity[0]['NumOccurrances']; 
}

Would work.

酸甜透明夹心 2024-09-26 17:25:38
foreach ($array as $key => $value){
  echo $value['t']['tag'] . " x " . $value[0]['NumOccurrances'];
}
foreach ($array as $key => $value){
  echo $value['t']['tag'] . " x " . $value[0]['NumOccurrances'];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文