Arrays problem : how to display a value found in 2 arrays (foreach and so on)

Hi all
I got a question about arrays and how to display a value found in 2 arrays.

My table1 contains various projects which belongs to a category (a type) . This type is stored with a integrer.

Then my table2 contains the categories list, with their ids == type of table1, and their ‘names’ as the name to display as a “title”.

$set = array();
$cset = array();

$result = mysql_query("SELECT id,project,type FROM table1 ORDER BY type ASC");

while($record = mysql_fetch_object($result)){
    $creq = mysql_query("SELECT id,type FROM table2 WHERE id = $record->type");
	while ($cres = mysql_fetch_object($creq)){
             // Below the hole i'm in...
             // How do I tell 'for each "type" found in table1, apply the name corresponding from table2 ??
                foreach ($cset[$cres->type] as $set[$record->type]) {
				$cset[$cres->type][] = $record;

//Then, I'll retrieve the stored values : the first 'foreach' displays the 'types' and the 2nd foreach displays the contents relative to its own type.	

foreach ($cset as $theme => $records) {

    echo {$theme};
    foreach ($records as $record) {
	echo {$record->title};

Eventually it won’t work but I don’t know how to fix it. Although I know how to use 1 foreach instance, I don’t know how to deal with 2 arrays simultaneously.

Thanks anyone with an efficient hint or answer :slight_smile:


it would help make things clearer for me if you could post the table structures for each table and a sample of the desired output.

Hi Kalon,

Actually it would look like this:

table1 (the projects table) :

id     |    project       |   type

1 | some text | 2
2 | other text | 3
3 | another one | 1

table2 (the categories table)

id     |     types        |   

1 | typeA |
2 | typeB |
3 | typeC |

What I would like to output is a loop containing the category (as a string) followed by all the projects belonging to it.
With these tables as example the output would look like this :

  • typeA
  • project : another one
  • project : some text
  • project : other text

and so on…

As I said I know how to deal to display infos from 1 table and cheat as I know the type names, but here, I have to display categories’ real name which are on the 2nd table…

I’m still struggling for a tip - thanks in advance if anyone here could led me on the right path