1

I am stuck in this complex looping through embedded documents. One of my embedded documents is formatted like this:

"BUSCO" : [
    {
        "short_summary_data" : {
            "Complete BUSCOs (C)" : "1411",
            "Complete and single-copy BUSCOs (S)" : "1397",
            "Complete and duplicated BUSCOs (D)" : "14",
            "Fragmented BUSCOs (F)" : "7",
            "Missing BUSCOs (M)" : "22",
            "Total BUSCO groups searched" : "1440"
        }
    },
    {
        "full_table" : {
            "EOG09360002" : {
                "status" : "Complete",
                "contig" : "Chr3",
                "start" : "430821",
                "end" : "448546",
                "score" : "7092.4",
                "length" : "3931"
            },
            "EOG0936000A" : {
                "status" : "Missing",
                "contig" : "",
                "start" : "",
                "end" : "",
                "score" : "",
                "length" : ""
            },
            "EOG0936001N" : {
                "status" : "Missing",
                "contig" : "",
                "start" : "",
                "end" : "",
                "score" : "",
                "length" : ""
            }
        }
    }
],

In my Laravel 5.2 application (PHP 5.2) I want to loop through the BUSCO.short_summary_data. Once, the "full_table" is in the embedded document, I can't loop through it. The code which I have now is only limited to looping through the short_summary_data and makes a table with the information.

@foreach ($id_array as $char)

    <?php
    $cursor = $collection->find(array("_id" => new MongoId($char)));
    ++$i;
    ?>
    @foreach ($cursor as $document)
            @if (!empty($document['BUSCO']))
                <p>These BUSCO statistics correspond to project number {{$i}}</p>
                @if (is_array($document['BUSCO']))
                    @foreach ($document['BUSCO'] as $BUSCO)
                        @if (is_array($BUSCO["short_summary_data"]))
                            @foreach ($BUSCO["short_summary_data"] as $key => $value)
                                <tr>
                                    <td>{{$key}}</td>
                                    <td>{{$value}}</td>
                                </tr>
                            @endforeach
                        @endif
                    @endforeach
                @endif
            @else
            @endif
        @endforeach
@endforeach

This code gives the error: Illegal string offset 'short_summary_data'. I am super stuck and don't know what the problem is!

1 Answer 1

1

Your BUSCO key's value is an array of arrays; one of which contains short_summary_data and one that does not. Can be seen by looking at the JSON [ and { layout here:

"BUSCO" : [
{
    "short_summary_data" : {
        "Complete BUSCOs (C)" : "1411",
        "Complete and single-copy BUSCOs (S)" : "1397",
        "Complete and duplicated BUSCOs (D)" : "14",
        "Fragmented BUSCOs (F)" : "7",
        "Missing BUSCOs (M)" : "22",
        "Total BUSCO groups searched" : "1440"
    }
},
{
    "full_table" : {
...

What's happening is you are getting to the entry inside $BUSCO that doesn't have the short_summary_data field (the object containing full_table) and it's throwing that warning.

@foreach ($document['BUSCO'] as $BUSCO)
    @if (isset($BUSCO["short_summary_data"]) && is_array($BUSCO["short_summary_data"]))

Note the added isset for this new conditional. The format in your post doesn't have a short_summary_data key in each $BUSCO piece of data, you need to check if it's there and to not proceed if that key is not set.

Sign up to request clarification or add additional context in comments.

4 Comments

I have adjusted my code, now no error is shown but also no table :-(. Maybe you could help me with the formatting of my BUSCO embedded document, because I tried to get it like how you showed but it doesn't work for me. I use two separate queries to add the data $collection->update(array("project_id" => $project_id), array('$push' => array('BUSCO' => array('short_summary_data' => $data)))); and $collection->update(array("project_id" => $project_id), array('$push' => array('BUSCO' => array('full_table' => $data))));. Do you know how to syntax to get the outcome you proposed?
Check this updated answer, I conflated the solution and another thought. Sorry!
I have tried this, but now nothing is shown. When I remove the isset($BUSCO['short_summary_data"]) the undefined index error is thrown again.
I fixed it! Thank you for helping me!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.