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 973185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:14:29+00:00 2026-05-16T03:14:29+00:00

i want to make a new data structure using arrays for this query it

  • 0

i want to make a new data structure using arrays for this query it will be like :

will start with a query and a code example

query http://img85.imageshack.us/img85/170/query.png
i want to make a new data structure using arrays for this query it will be like :

//this is for the first headline
[46] => Array
        (
            [headline_name] => headline 1
            [score] => 50
            [questions] => Array
                (
                    [0] => Array
                        (
                            [question_id] => 136
                            [question_name] => question 3 , headline 1
                            [choice_0] => 0
                            [choice_1] => 0
                            [choice_2] => 0
                            [choice_3] => 0
                            [choice_4] => 0
                        ) ,
                    [1] => Array
                        (
                         ....
                        )
                )
        )

the code that i wrote wanting to achieve this result is :

foreach ($result as $row) {
  $data[$row->headline_id]= array( 
    'headline_name' => $row->headline_name ,
    'score' => $row->score,
  );
  $data[$row->headline_id]['questions'][] = array (
      'question_id'=>$row->question_id ,
      'question_name'=>$row->question_name ,
      'choice_0' => $row->choice_0 ,
      'choice_1' => $row->choice_1 ,
      'choice_2' => $row->choice_2 ,
      'choice_3' => $row->choice_3 ,
      'choice_4' => $row->choice_4             
  );  
}

the problem with it, it only shows the last question in each headline , in each loop in the questions array the array are not appended they overwrite each other


if i want to get it to work i have to delete i have to delete the lines for the score and headline_name , the code will be like this

foreach ($result as $row) {

  $data[$row->headline_id]['questions'][] = array (
      'question_id'=>$row->question_id ,
      'question_name'=>$row->question_name ,
      'choice_0' => $row->choice_0 ,
      'choice_1' => $row->choice_1 ,
      'choice_2' => $row->choice_2 ,
      'choice_3' => $row->choice_3 ,
      'choice_4' => $row->choice_4 
  );  

}

i want a solution to solve it with writing the headline_name and the score in the array

  • 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-05-16T03:14:29+00:00Added an answer on May 16, 2026 at 3:14 am

    the problem is that you reset the array in the second time when you enter to the loop
    by = ,

    to solve it you need to add a little if

        if(!isset($data[$row->headline_id])) {
          $data[$row->headline_id]= array( 
            'headline_name' => $row->headline_name ,
            'score' => $row->score,
          );
      }
    

    another issue :
    i recommend not to store this fields in this table
    is not normalize ,

    headline_name
    score
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.