简体   繁体   English

打印嵌套数组的内容

[英]Printing contents of a nested array

I am trying to print the contents of a nested array, but it simply returns "Array" as a string yet will not iterate with a foreach loop. 我正在尝试打印嵌套数组的内容,但它只是以字符串形式返回“ Array”,但不会使用foreach循环进行迭代。 The arrays are coming from a mongodb find(). 数组来自mongodb find()。 The dataset looks like this: 数据集如下所示:

User
Post_Title
Post_Content[content1,content2,content3]

I am trying to get at the content1,2,3. 我试图获得内容1,2,3。

My current code looks like this: 我当前的代码如下所示:

$results = $collection->find($query);

foreach ($results as $doc)
    {
    echo $doc['title']; //this works
    $content[] = $doc['content'];           
    print_r($content); //this prints "Array ( [0] => Array )"
    foreach ($content as $item)
        {
        echo $item;
        }
    }   

All this code does is print the Title, followed by Array ( [0] => Array ), followed by Array. 这些代码所做的全部工作是打印Title,然后打印Array([0] => Array),然后打印Array。

I feel quite stupid to not figure out something that seems so basic. 我很愚蠢,没有弄清楚看起来如此基本的东西。 Most posts on stack overflow refer to multidimensional associative arrays - in this case, the top level array is associative, but the content array is indexed. 堆栈溢出中的大多数帖子都引用多维关联数组-在这种情况下,顶级数组是关联数组,但内容数组已建立索引。

I have also tried 我也尝试过

foreach ($doc['content'] as $item)

But that gives 但这给

Warning: Invalid argument supplied for foreach() 

I even tried iterating over the returned array again using a nested foreach, like the following: 我什至尝试使用嵌套的foreach再次遍历返回的数组,如下所示:

foreach ($results as $doc)
    {
    echo $doc['title']; //this works
    $content[] = $doc['content'];           
    print_r($content); //this prints "Array ( [0] => Array )"
    foreach ($content as $item)
        {
            foreach ($item as $next_item)
                {
                  echo $next_item;
                }
        echo $item;
        }
    }

The second foreach failed with an invalid argument.. Any thoughts would be greatly appreciated. 第二个foreach失败,并带有一个无效的参数。任何想法将不胜感激。

edit: perhaps it has something to do with how I am inserting the data to the DB. 编辑:也许与我将数据插入数据库的方式有关。 That code looks like this 该代码看起来像这样

$title = $_POST['list_title'];
$content[] = $_POST['list_content1'];
$content[] = $_POST['list_content2'];
$content[] = $_POST['list_content3'];

$object = new Creation();
$object->owner = "$username";
$object->title = "$title";
$object->content = "$content";
$object->insert();

Is this not the proper way to add an array as a property to a class? 这不是将数组作为属性添加到类的正确方法吗?

The issue is some of array content is an array while other parts are strings. 问题是一些数组内容是一个数组,而其他部分是字符串。 You should test for the sub content $doc being an array using is_array() . 您应该使用is_array()测试子内容$doc是否为数组。 If it is, loop through the $doc like you would any other array, and if it isn't, you can echo the content (may need to test the content type before doing this if you're uncertain as to what content it can be) 如果是的话,就像在其他数组中那样循环遍历$doc ,如果不是,则可以回显内容(如果不确定可以做什么,则可能需要先测试内容类型,然后再执行此操作)是)

The problem is in the code that you are saving data. 问题出在代码中,您正在保存数据。 You should replace the following line 您应该替换以下行

$object->content = "$content"; $ object-> content =“ $ content”;

with

$object->content = $content; $ object-> content = $ content;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM