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

  • Home
  • SEARCH
  • 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 6861149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:29:55+00:00 2026-05-27T02:29:55+00:00

The output of the function looks good but I’m getting extra [0] => Array

  • 0

The output of the function looks good but I’m getting extra [0] => Array ( [1] => 1 ) from nowhere everytime. The id’s are starting from 1 in my db table. Where is 0 appearing from?!

Array ( [1] => Array ( [name] => Sual [parent] => 0 ) **[0] => Array ( [1] => 1 )** ...

Here is the function

<?php

function treeGen($menu, $utype) {
    global $db;
    $tree = array();
    $stmt = $db->prepare("select id, parent, name FROM navigation WHERE menu=? AND user_type=?") or die($db->error);
    $stmt->bind_param("ii", $menu, $utype) or die($stmt->error);
    $stmt->execute() or die($stmt->error);
    $stmt->store_result();
    $meta = $stmt->result_metadata();
    $stmt->bind_result($id, $parent, $name);
    while ($stmt->fetch()) {
        $tree[$id] = array(
            'name' => $name, 
            'parent' => $parent
        );
        if (!array_key_exists($parent,$tree)) {
            $tree[$parent][$id] = $id;
        }
    }
    $stmt->close();
   print_r($tree);
}

?>
  • 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-27T02:29:56+00:00Added an answer on May 27, 2026 at 2:29 am

    I imagine the problem comes when you encounter a top-level row from the table (a row without a parent). When you process one of these rows, $parent is null and this conditional fires:

    if (!array_key_exists($parent,$tree)) {
        $tree[$parent][$id] = $id;
    }
    

    Here $parent is null, which is interpreted as 0, which isn’t a key that exists in $parent (at least, not the first time you encounter a row like this), so this results in $tree[0] being created. In your case, the first row where parent is null is the row with id = 1, hence $tree[0] is array(1 => 1).

    Change the above conditional to the following:

    if (!array_key_exists($parent,$tree) and !is_null($parent)) {
    

    or if your DB wrapper does not use the PHP null type to represent SQL NULL values, use something like this:

    if (!array_key_exists($parent,$tree) and $parent != 0) {
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to print the output of function only when it is true but
What's the most efficient way to convert the output of this function from a
I've been trying to troubleshoot this, but can't find a good function that will
I want to output the function name each time it is called, I can
What is the simplest way to suppress any output a function might produce? Say
I have been using PRETTY_FUNCTION to output the current function name, however I have
I am writing a function that takes in a output target file and a
Is there a pretty printing function in Clojure that would output data-structures like lists
I have created a function that takes a SQL command and produces output that
I've got some data (the output of a ddply function) that I want to

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.