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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:46:11+00:00 2026-06-05T17:46:11+00:00

Problem: I have two array where one produce a category and the second produce

  • 0

Problem:

I have two array where one produce a category and the second produce items for the category. I would like the items to be inside each category but I can’t make it work.

PHP code:

foreach ($current['Children'] as $key => $value)
{
    $query = "SELECT * FROM betyg_items WHERE CID = '{$key}' AND Status = '1' ORDER BY CID ASC";
    $result = mysql_query($query) or die ('Database Error (' . mysql_errno() . ') ' . mysql_error());

    $_SESSION['keys'][$key] = $value;

    while ($row = mysql_fetch_assoc($result))
    {
        $_SESSION['items'][$row['IID']] = array(
                                        'ID' => $row['IID'],
                                        'CID' => $row['CID'],
                                        'Description' => $row['Description']
                                    );
    }
}

$_SESSION[‘keys’] will contain:

Array
(
    [2] => Integration av källorna
    [3] => Belysning av egna resultat
    [4] => Referenser
)

$_SESSION[‘items’] will contain:

Array
(
    [1] => Array
        (
            [ID] => 1
            [CID] => 2
            [Description] => Källorna refereras separat
        )

    [2] => Array
        (
            [ID] => 2
            [CID] => 2
            [Description] => Vissa försök till sammanbindning
        )

    [3] => Array
        (
            [ID] => 6
            [CID] => 3
            [Description] => Inga jämförelser mellan egna och andras resultat
        )

    [4] => Array
        (
            [ID] => 7
            [CID] => 3
            [Description] => Kort redovisning av likheter och skillnader mellan egna och andras resultat
        )

    [5] => Array
        (
            [ID] => 11
            [CID] => 4
            [Description] => Många formella felaktigheter i referens- och litteraturförteckning; ej primärkällor
        )

    [6] => Array
        (
            [ID] => 12
            [CID] => 4
            [Description] => Vissa formella fel, vissa källor av mindre kvalitet
        )
)

Question:

How do I get each item into the array index it correspond by using the CID value?

Scenario:

For instance, all CID’s that contain the number 2 should go into “[2] => Integration av källorna”, and so forth.

Desired output:

Array
(
    [2] => Integration av källorna

            [1] => Array
                (
                    [ID] => 1
                    [CID] => 2
                    [Description] => Källorna refereras separat
                )

            [2] => Array
                (
                    [ID] => 2
                    [CID] => 2
                    [Description] => Vissa försök till sammanbindning
                )

    [3] => Belysning av egna resultat

            [3] => Array
                (
                    [ID] => 6
                    [CID] => 3
                    [Description] => Inga jämförelser mellan egna och andras resultat
                )

            [4] => Array
                (
                    [ID] => 7
                    [CID] => 3
                    [Description] => Kort redovisning av likheter och skillnader mellan egna och andras resultat
                )

    [4] => Referenser

            [5] => Array
                (
                    [ID] => 11
                    [CID] => 4
                    [Description] => Många formella felaktigheter i referens- och litteraturförteckning; ej primärkällor
                )

            [6] => Array
                (
                    [ID] => 12
                    [CID] => 4
                    [Description] => Vissa formella fel, vissa källor av mindre kvalitet
                )
)
  • 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-05T17:46:13+00:00Added an answer on June 5, 2026 at 5:46 pm
    $final_arr = array();
    $i = 0;
    
    foreach ($current['Children'] as $key => $value)
    {
        $query = "SELECT * FROM betyg_items WHERE CID = '{$key}' AND Status = '1' ORDER BY CID ASC";
        $result = mysql_query($query) or die ('Database Error (' . mysql_errno() . ') ' . mysql_error());
    
        $final_arr[$i]['key'] = $value;
    
        while ($row = mysql_fetch_assoc($result))
        {
             $final_arr[$i]['items'][] = array(
                                'ID' => $row['IID'],
                                'CID' => $row['CID'],
                                'Description' => $row['Description']
                              );
        }
       $i++;
    }
    

    FINAL OUTPUT WILL BE:
    $final_arr

    Array
    (
        [0] => 
                ['key']=> Integration av källorna
                ['items'] => 
                     [0] => Array
                     (
                        [ID] => 1
                        [CID] => 2
                        [Description] => Källorna refereras separat
                     )
    
                     [1] => Array
                     (
                        [ID] => 2
                        [CID] => 2
                        [Description] => Vissa försök till sammanbindning
                      )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with mutual constraints. I want to have two tables each
I have two Questions. One small and one greater problem. Why i have to
i have seen a few days ago such problem there is given two array
I have a sort problem. I have two array int a[] ={index1,index2,index3...indexI}; int b[]
Problem: I have two arrays that can possibly be different lengths. I need to
Here's my problem: I have two select fields in my web application. The second
Hi I have a problem to manipulate an array with another array like the
Once again, I have a problem for which I would like to shave off
I have two shell scripts. One script dynamically creates the call to the second
I would like to bring access to an internal array with a Property, but

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.