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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:58:45+00:00 2026-06-18T10:58:45+00:00

There are 3 arrays in my PHP, $console, $model, and $game. Here is the

  • 0

There are 3 arrays in my PHP, $console, $model, and $game.

Here is the code by the way:

<?PHP
    $console = array();
    $model = array();
    $game = array();

    $gameQuery = "SELECT * FROM consoleGame";
    $gameResult = mysql_query($gameQuery) or die(mysql_error());

    while ($row = mysql_fetch_assoc($gameResult)) {
        if(!is_array($game[$row['modelId']])) {
            $game[$row['modelId']] = array();
        }

         $game[$row['modelId']][$row['gameId']] = array(
                                        'Game Name' => $row['gameName'],
                                        'Game ID' => $row['gameId']);
    }

    $modelQuery = "SELECT * FROM consoleModel";
    $modelResult = mysql_query($modelQuery) or die(mysql_error());

    while ($row = mysql_fetch_assoc($modelResult)) {
        if (!is_array($model[$row['consoleId']])) {
            $model[$row['consoleId']] = array();
        }

        $model[$row['consoleId']][$row['modelId']] = array(
                                        'Model Name' => $row['modelName'],
                                        'Model ID' => array_values($game[$row['modelId']])); //This is the warning by the way.
    }

    $consoleQuery = "SELECT * FROM consoleConsole";
    $consoleResult = mysql_query($consoleQuery) or die(mysql_error());

    while ($row = mysql_fetch_assoc($consoleResult)) {
        if (!is_array($console[$row['consoleId']])) {
            $console[$row['consoleId']] = array();
        }

        $console[$row['consoleId']] = array(
                                        'Console Name' => $row['consoleName'],
                                        'Console ID' => array_values($model[$row['consoleId']]));
    }

    $console = array_values($console);
    echo json_encode($console);
?>

As you can see in the code, I have added array_values to $console and $model without a hitch. I was lucky back then. When I added array_values to $game, it creates a warning. What is the possible fix to this?

Additional information
I declared $console, $model and $game as an array(). I have no idea why it’s not shown above $gameQuery.

  • 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-18T10:58:47+00:00Added an answer on June 18, 2026 at 10:58 am

    You’re using three different queries to get results from your database, and although you ‘check’ if a key is present for the ‘modelId’ in the $game array, it is possible that no game exists for a specific model;

    This line guarantees that a key is added to the $game array if that modelId was not yet present

    if(!is_array($game[$row['modelId']])) {
        $game[$row['modelId']] = array();
    }
    

    However, in the second loop, with results from SELECT * FROM consoleModel your loading all models, possibly some have no games in your database, so there won’t be a key for that modelId in the $game array.

    For example:

    consoleGame

    gameName | gameId | modelId
    GameA    | 1      | 1
    GameB    | 2      | 1
    

    consoleModel

    modelName| modelId
    ModelA   | 1
    ModelB   | 2
    

    This will have 2 games for ModelA, but none for ModelB.

    You may work around this issue by adding this inside the $modelResult loop too

    if (!is_array($model[$row['consoleId']])) {
        $game[$row['modelId']] = array();
    }
    

    Some ideas

    The way you’re collecting the data doesn’t really use the ‘power’ of the database. Databases are designed to retrieve data and related data (hence the name ‘relational database’)

    For example, to collect all consoleModels, including the games for that model (if any) use this;

    SELECT
       consoleModel.modelId,
       consoleModel.modelName,
       consoleGame.gameId,
       consoleGame.gameName
    FROM
        consoleModel
        -- Using 'LEFT' join so that consoleModels without games will also be returned
        LEFT JOIN consoleGame
          ON consoleGame.modelId = consoleModel.modelId
    

    Which will return;

    modelId | modelName | gameId | gameName
    1       | ModelA    | 1      | GameA
    1       | ModelA    | 1      | GameA
    2       | ModelB    | NULL   | NULL
    

    Although this will duplicate the ModelName rows, it may make things easier for you, as you won’t have to run 3 separate loops.

    Mode optimized options will be possible (e.g. loop through all consoleModels in PHP and collect the related games inside the loop), I will leave that up to you to try and learn

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

Sidebar

Related Questions

Given something like DB()->prepare(SELECT * FROM mysql.general_log WHERE user_host LIKE ?); $statement->execute( array('%console%') );
Is there a built-in function for PHP for me to check whether two arrays
Is there anyways to get first two element of each profile_id using php array(
There is an array of user states stored in the session. This works: <?php
I'm interested if there is any function like array_map or array_walk from php. Don't
I come from a php background and in php, there is an array_size() function
Is there any function available in PHP to check whether an array is empty
How can I add all of my array values together in PHP? Is there
Given the array, i need to find how many monotonically increasing Sub-arrays there are
Here is my markup <div class=code php> <div class=container> <div class=line-numbers>1 2 3 4

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.