在数组中显示数组?

发布于 2024-10-02 19:32:20 字数 1465 浏览 0 评论 0原文

我在数组中有一个数组,我想简单地打印出它的内容...问题是我正在尝试的不起作用...即

        foreach($retval as $k=>$v){

          if (is_array($v)){

            foreach($v as $l=>$w){

            echo $l . ' = ' . $w . '<br />';

            }

        } else {

          echo $k . ' = ' . $v . '<br />';

        }

这样做,但我最终得到:

id = 2

name=Speakersform_field

=hiddendisplay_order

=

0groups=Array

所以我没有在内部数组中得到foreach..我做错了什么?实际上,当我非常确定只有“组”是数组时,我的代码似乎认为所有返回的内容都是数组。


数组的结构如下所示:

array ( 0 => array ( 'id' => 2, 'name' => 'Speakers', 'form_field' => 'hidden', 'display_order' => '0', 'groups' => array ( 0 => array ( 'bit' => '1', 'name' => 'don', 'display_order' => '4', 'subscribers' => 8, ), 1 => array ( 'bit' => '2', 'name' => 'tyler', 'display_order' => '5', 'subscribers' => 0, ), 2 => array ( 'bit' => '4', 'name' => 'stephen', 'display_order' => '6', 'subscribers' => 0, ), 3 => array ( 'bit' => '8', 'name' => 'daniel', 'display_order' => '7', 'subscribers' => 0, ), 4 => array ( 'bit' => '16', 'name' => 'william', 'display_order' => '8', 'subscribers' => 0, ), 5 => array ( 'bit' => '32', 'name' => 'bobbys', 'display_order' => '9', 'subscribers' => 0, ), ), ), )

长话短说,我实际上只是想搜索整个事物以查找名称“bobby”,并获得一个简单的真或假,判断该值是否存在于其中的任何位置。

I have an array inside an array and I'd like to simply print out the contents of it... the problem is what I'm trying doesn't work... ie

        foreach($retval as $k=>$v){

          if (is_array($v)){

            foreach($v as $l=>$w){

            echo $l . ' = ' . $w . '<br />';

            }

        } else {

          echo $k . ' = ' . $v . '<br />';

        }

Doing that however I end up with:

id = 2

name = Speakers

form_field = hidden

display_order = 0

groups = Array

So I'm not getting a foreach in that inside array.. what am I doing wrong? It seems actually that my code believes everything return is an array when I'm pretty sure that only 'groups' is an array.


the structure of the array looks like so:

array ( 0 => array ( 'id' => 2, 'name' => 'Speakers', 'form_field' => 'hidden', 'display_order' => '0', 'groups' => array ( 0 => array ( 'bit' => '1', 'name' => 'don', 'display_order' => '4', 'subscribers' => 8, ), 1 => array ( 'bit' => '2', 'name' => 'tyler', 'display_order' => '5', 'subscribers' => 0, ), 2 => array ( 'bit' => '4', 'name' => 'stephen', 'display_order' => '6', 'subscribers' => 0, ), 3 => array ( 'bit' => '8', 'name' => 'daniel', 'display_order' => '7', 'subscribers' => 0, ), 4 => array ( 'bit' => '16', 'name' => 'william', 'display_order' => '8', 'subscribers' => 0, ), 5 => array ( 'bit' => '32', 'name' => 'bobbys', 'display_order' => '9', 'subscribers' => 0, ), ), ), )

Long story short, I'm actually just trying to search this whole thing for say the name 'bobby' and get a simple true or false on whether that value exists anywhere in there.

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

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

发布评论

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

评论(5

墨离汐 2024-10-09 19:32:20
print_r($myArray);

PHP 手册

<?php
    $a = array ('a' => 'apple', 'b' => 'banana', 'c' => array ('x', 'y', 'z'));
    print_r ($a);
?>

会给出:

Array
(
    [a] => apple
    [b] => banana
    [c] => Array
        (
            [0] => x
            [1] => y
            [2] => z
        )
)

不确定这是否是您正在寻找的内容...

print_r() 为您提供可视化表示,而 var_export() 返回有效的可执行 PHP 代码。 手册:var_export()

print_r($myArray);

PHP Manual

<?php
    $a = array ('a' => 'apple', 'b' => 'banana', 'c' => array ('x', 'y', 'z'));
    print_r ($a);
?>

would give:

Array
(
    [a] => apple
    [b] => banana
    [c] => Array
        (
            [0] => x
            [1] => y
            [2] => z
        )
)

Not sure if this is what you are looking for...

print_r() gives you a visual representation while var_export() returns valid, executable php code. Manual: var_export()

初见 2024-10-09 19:32:20

$retval[]['groups'] 也是一个数组时,您假设数组只是双重嵌套的。使用递归来处理这个问题,而不是仅仅在级别上添加级别。

You're assuming that arrays are only doubly-nested, when $retval[<something>]['groups'] is also an array. Use recursion to handle this instead of just adding levels upon levels.

岁月静好 2024-10-09 19:32:20

听起来你需要重新考虑你想要做什么。

如果您只是想查看数组中的内容,以便知道如何获取特定的数据,那么您可能会使用 print_r、var_export 或上面提到的类似方法。

print_r($retval);

如果您希望您的 php 脚本能够自行确定“bobby”是否在数组中,那么您可能会获得更多信息来帮助您进行搜索。例如,您知道数组的结构以及您正在搜索的名称吗?如果是这样,那么您只需要检查 $array[$foo]['name'] 中的字符串。您可以像这样调整您的代码:

foreach($retval as $k=>$v) {
    if ($v['name'] == 'bobby') {
        echo "Bobby is at index $k";
    }
}

PHP 包含 很多数组函数 ,尽管这些往往适用于一维数组。

PS 请编辑数组的结构以使其更易于阅读。

It sounds like you need to reconsider what you are trying to do.

If you just want to see what is in your array so that you know how to get at a particular piece of data, then you're probabaly after print_r, var_export or similar as mentioned above.

print_r($retval);

If you want your php script to be able to work out on it's own whether 'bobby' is in the array, then you might have some more information that will help your search. For example, do you know the structure of your array and that you're searching for a name? If so, then you only need to check the strings that are in $array[$foo]['name']. You could adapt your code like so:

foreach($retval as $k=>$v) {
    if ($v['name'] == 'bobby') {
        echo "Bobby is at index $k";
    }
}

PHP contains lots of functions for arrays, although these tend to be for single-dimension arrays.

P.S. Please edit the structure of your array to make it easier to read.

仅此而已 2024-10-09 19:32:20

如果您的目标是了解某个值是否位于任何嵌套数组中,您可以执行以下操作:

foreach($retval as $vec){
    if(in_array("bobby", $vec)
        do something

If your goal is to understand if a value is in any nested array you can do this:

foreach($retval as $vec){
    if(in_array("bobby", $vec)
        do something
千纸鹤带着心事 2024-10-09 19:32:20

使用包裹在 RecursiveIteratorIterator。语法将是:

$arr = array('this is your complicated multi-dimensional array');
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($arr));
foreach($iterator as $key => $value){
    echo '['.$key.'] => '.$value.'<br />';
}

您甚至可以使用 $iterator->depth() 来确定当前元素的深度。

Use a RecursiveArrayIterator wrapped in RecursiveIteratorIterator. The syntax will then be:

$arr = array('this is your complicated multi-dimensional array');
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($arr));
foreach($iterator as $key => $value){
    echo '['.$key.'] => '.$value.'<br />';
}

You can even use $iterator->depth() to determine the depth of a current element.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文