-1

Every time I try to display the category I get the name Array instead of the category name. I was wondering how can I display the category names from my code below?

$list = array();
$cat = array();
$query = mysqli_query($dbc,"SELECT id, parent_id, category FROM categories ORDER BY parent_id, category LIKE category ASC");
while($row = mysqli_fetch_assoc($query)){
    $list[$row['id']] = array_merge($row, array('children' => array()));
}
mysqli_free_result($query);

foreach($list as $nodeId => &$node) {
    if(!$node['parent_id'] || !array_key_exists($node['parent_id'], $list)){
        $cat[] = &$node;
    } else {
        $list[$node['parent_id']]['children'][] = &$node;
    }
}
unset($node);
unset($list);

Var dump output example.

array
  0 => &
    array
      'id' => string '1' (length=1)
      'parent_id' => string '0' (length=1)
      'category' => string 'Cat-1' (length=5)
      'children' => 
        array
          0 => &
            array
              ...
          1 => &
            array
              ...
          2 => &
            array
              ...
7
  • 1
    I dont see, where you are trying to display anything. Additional: Are you sure you want to unset $list right after you created it? Oo Commented May 13, 2011 at 12:46
  • do var_dump on the "problematic" variable and see the answer. Commented May 13, 2011 at 12:46
  • I did a var dump but still couldn't get it to work:( Commented May 13, 2011 at 12:47
  • @bazmegakapa I didn't post my display output because it didn't work Commented May 13, 2011 at 12:48
  • 2
    There really is no need to emphasize your ambiguous question title with additional exclamation marks... Commented May 13, 2011 at 12:49

3 Answers 3

2

When you echo a variable, and the browser display's "Array", you need to go another level deeper to access the array elements. You may have tried this already, but var_dump() your category array to view the structure of your array and verify it is what you expected it to be. YOu will be able to analyze your apparent multi-dimensional array to judge how many loops you need to get to the category names.

Update:

to go a level deeper, you need nested loops. for example:

foreach($first_level as $first){
    foreach($first as $second_level){
        echo $second_level;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

how would I go a level deeper?
1

This might or might not be your problem:

    $cat[] = &$node;

If you just want the category names, then you'd likely need to use:

    $cat[] = $node["category"];

When you otherwise print the list out, the subarrays will be converted into "Array" in string context.

Comments

0

I don't get that LIKE category ASC at the end of your query what is it supposed to do? otherwise Itay Moav is right var_dump it :) there are loads of really cool function to display var_dump correctly on the php manual page.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.