Outputting a multidimensional array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rubberjohn
Forum Contributor
Posts: 193
Joined: Fri Feb 25, 2005 4:03 am

Outputting a multidimensional array

Post by rubberjohn »

I have the following code inside a nested sql statment:

Code: Select all

$arr_all_tags[] = array($get_all['work_area'] => $get_all_tags['tag']);
The index is from the outer sql statement and the value array is from the inner sql statement.

I am trying to output the values using the following code:

Code: Select all

foreach( $arr_all_tags as $work_area=>$tags )
{
  echo "<strong>" . $work_area ."</strong><BR>";

  sort( $tags ); 
  foreach( $tags as $tag )
  {
    echo $tag . "<BR>";
  }
 
}
But this gives me the following output:

Code: Select all

0
Tag1
1
Tag2
2
Tag3
3
Tag4
If I take away the square brackets after $arr_all_tags I get the index but none of the values. All I want is to list the index once and then list all of the values under that index.

Can anyone tell me what I am doing wrong?

Thanks in advance

rj
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

$arr_all_tags[$get_all['work_area']] = $get_all_tags['tag'];
rubberjohn
Forum Contributor
Posts: 193
Joined: Fri Feb 25, 2005 4:03 am

Post by rubberjohn »

cheers for that feyd but im still getting the same result as I do when I take the square brackets away- it gives me this error warning as if the values aren't being put into an array

Code: Select all

Warning: sort() expects parameter 1 to be array, string given in XXX on line 335

Warning: Invalid argument supplied for foreach() in XXX on line 336
Do the nested foreach statements have to be changed too?

thanks

rj
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

is $get_all_tags['tag'] an array?

gettype()
rubberjohn
Forum Contributor
Posts: 193
Joined: Fri Feb 25, 2005 4:03 am

Post by rubberjohn »

yeah its the result of an sql statement, the preceding line is:

Code: Select all

while($get_all_tags = mysql_fetch_array($sql_get_all_tags, MYSQL_ASSOC)){
which is inside another sql while loop

rj
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

your error says otherwise...

post your new code.
rubberjohn
Forum Contributor
Posts: 193
Joined: Fri Feb 25, 2005 4:03 am

Post by rubberjohn »

ok here's everything...

Code: Select all

$sql_get_all = mysql_query("SELECT * FROM work_area");
while($get_all = mysql_fetch_array($sql_get_all, MYSQL_ASSOC)){
	

$sql_get_all_tags = mysql_query( "SELECT `tag`
FROM `f_tags`
INNER JOIN `f_tag_f_user`
ON `f_tag_f_user`.`tag_id` = `f_tags`.`tag_id`
WHERE `f_tag_f_user`.`work_area_id` = '$get_all[work_area_id]'")or die (mysql_error());

while($get_all_tags = mysql_fetch_array($sql_get_all_tags, MYSQL_ASSOC)){

echo  gettype($get_all_tags) ;

$arr_all_tags[$get_all['work_area']] = $get_all_tags['tag']; 
		
		
		
}


}

foreach( $arr_all_tags as $work_area=>$tags )
{
  echo "<strong>" . $work_area ."</strong><BR>";

  sort( $tags ); // sorts the list of songs
  foreach( $tags as $tag )
  {
    echo $tag . "<BR>";
  }
 
}
thanks

rj
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Through the highlighter...

Code: Select all

<?php
$arr_all_tags = array(); //Added this just to initialize the array

$sql_get_all = mysql_query("SELECT * FROM work_area");
while($get_all = mysql_fetch_array($sql_get_all, MYSQL_ASSOC)){
    $sql_get_all_tags = mysql_query( "SELECT `tag`
        FROM `f_tags`
        INNER JOIN `f_tag_f_user`
            ON `f_tag_f_user`.`tag_id` = `f_tags`.`tag_id`
        WHERE `f_tag_f_user`.`work_area_id` = '" . $get_all['work_area_id'] . "'")or die (mysql_error());

    while($get_all_tags = mysql_fetch_array($sql_get_all_tags, MYSQL_ASSOC)){
        // This fills the $arr_all_tags with index of $get_all['work_area'] and value of $get_all_tags['tag']
        // That means that $arr_all_tags is one-dimensional because neither the index nor value is an array
        $arr_all_tags[$get_all['work_area']] = $get_all_tags['tag'];
    }
}

foreach( $arr_all_tags as $work_area=>$tags )
{
    echo "<strong>" . $work_area ."</strong><BR>";
    sort( $tags ); // sorts the list of songs

    // $tags here is not an array... This will error ($tags = $get_all_tags['tag'])
    foreach( $tags as $tag )
    {
        echo $tag . "<BR>";
    }
}
?>
$arr_all_tags is a one dimensional array. $tags is not an array at all. This could be your error.
rubberjohn
Forum Contributor
Posts: 193
Joined: Fri Feb 25, 2005 4:03 am

Post by rubberjohn »

why does

Code: Select all

gettype($get_all_tags)
say its an array?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

rubberjohn wrote:why does

Code: Select all

gettype($get_all_tags)
say its an array?
Because $get_all_tags is an array. $get_all_tags['tag'] is a string value in the array.
Post Reply