Page 1 of 1

Outputting a multidimensional array

Posted: Mon Mar 13, 2006 4:10 pm
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

Posted: Mon Mar 13, 2006 4:27 pm
by feyd

Code: Select all

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

Posted: Mon Mar 13, 2006 5:02 pm
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

Posted: Mon Mar 13, 2006 5:04 pm
by feyd
is $get_all_tags['tag'] an array?

gettype()

Posted: Mon Mar 13, 2006 5:11 pm
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

Posted: Mon Mar 13, 2006 5:13 pm
by feyd
your error says otherwise...

post your new code.

Posted: Mon Mar 13, 2006 5:44 pm
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

Posted: Mon Mar 13, 2006 5:54 pm
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.

Posted: Mon Mar 13, 2006 6:11 pm
by rubberjohn
why does

Code: Select all

gettype($get_all_tags)
say its an array?

Posted: Mon Mar 13, 2006 6:25 pm
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.