Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9126487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:03:19+00:00 2026-06-17T07:03:19+00:00

I am having a very strange issue with printing out element of an array.

  • 0

I am having a very strange issue with printing out element of an array.

I am attempting to print out some elements of an array inside of a php foreach Here is exactly what the array looks like

 [2] => Array
        (
            [id] => 3
            [body] => dsfgdfgd
            [has_subquestion] => 1
            [is_subquestion] => 0
            [ordering] => 2
            [is_manditory] => 0
            [created] => 2013-01-09 12:06:47
            [parent_id] => 0
            [sub] => Array
                (
                    [0] => Array
                        (
                            [id] => 4
                            [body] => dfgdfg
                            [has_subquestion] => 1
                            [is_subquestion] => 1
                            [ordering] => 0
                            [is_manditory] => 0
                            [created] => 2013-01-09 11:24:20
                            [parent_id] => 3
                        )

                    [1] => Array
                        (
                            [id] => 23
                            [body] => gsdgdf
                            [has_subquestion] => 1
                            [is_subquestion] => 1
                            [ordering] => 14
                            [is_manditory] => 0
                            [created] => 2013-01-09 12:56:33
                            [parent_id] => 3
                        )

                )

        )
     [3] => Array
            (
                [id] => 5
                [body] => dfgdfg
                [has_subquestion] => 1
                [is_subquestion] => 0
                [ordering] => 3
                [is_manditory] => 0
                [created] => 2013-01-09 12:06:47
                [parent_id] => 0
                [sub] => Array
                    (
                        [id] => 6
                        [body] => dfgdfg
                        [has_subquestion] => 0
                        [is_subquestion] => 1
                        [ordering] => 3
                        [is_manditory] => 0
                        [created] => 2013-01-08 13:37:07
                        [parent_id] => 5
                    )

            )

Notice that the first one has 2 [sub]’s and the second only has one. This is my code for printing them

echo count($question['sub']);
foreach($question['sub'] as $s):
    echo '<li>
    <input type="hidden" name="sub[id]" value="'. $s['id'] .'" />
    <input type="hidden" name="sub[parent]" value="'. $question['id'] .'" />
    '. $s['body'] .'</li>';
endforeach;

this is what it is printing

2
dfgdfg
gsdgdf

8 <–count (which should be 1 not 8)
6 <–each of the following are the first letter/number in the sub array
d
0
1
3
0
2

Can anyone see what I am doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T07:03:20+00:00Added an answer on June 17, 2026 at 7:03 am

    You will need to update your code to see if the [sub] array is multidimensional or not. I would do it by checking if sub has a key that will only match if there is only a single array:

    if (array_key_exists('id', $question['sub']) {
        // This is a single array so wrap it in an array so that the foreach logic works
        $question['sub'] = array($question['sub']);
    }
    ... continue as normal with your foreach loop.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having a very strange issue with a few creative banners. On some computers when
I am having a very strange issue with Socket.IO. For some reason, it appears
I'm having some troubles with displaying a graph. It's a very strange issue, because
I'm having a very strange issue with Python's subprocess.Popen. I'm using it to call
Hey internets. I am having a VERY strange issue in C. I am deriving
hello fellow java developers. I'm having a very strange issue. I'm trying to read
Having a very strange issue regarding postback in ASP.NET. I have page dynamically populating
I am having a very strange issue on the native android browser and blackberry
I am having a very strange issue with VS 2012. When in design time
I'm having a very strange issue with a space invaders game I'm working on.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.