Array and loops

Make (in memory) database of musical instruments and database stores.

Base 1 must have:

instrument id
name of the instrument
instrument model
Base 2 must have:

shop id
stores name
stores address
*

input:
The user enters the name of the instrument in the variable at the beginning of the program.

output:
The program broadcasts a list of all the instruments of a similar name and the shops where it is located.

conditions:
Arrays and loops Must be used

This an assignment I’m having trouble with. When I asked how to make the connection between stores and instruments I was given this as an answer.
[php]
$instruments = array(
“1”=>“Guitar”,
“2”=>“Drums”
);
$stores = array(
“1”=>“Mitros”,
“2”=>“Mega”
);
$stores_instruments = array(
array(1,2),
array(1,1),
array(2,1)
);
[/php]

And this is my progress so far
[php]

<?php $instruments = array( array (100,"1"=>"guitar","gibson"), array (200,"2"=>"drums","hohner"), array (300,"3"=>"flute","abel"), ); $prodavnice = array ( array (4,"1"=>"brothers","pijacna_8"), array (5,"2"=>"musicshop","knezmihajlova_10"), array (6,"3"=>"drums", "kneza_milosa_5"), ); $prodavnice_instrumenti= array ( array (1,1), array (2,1), array (2,2) ); ?>

[/php]

I’m not the last array is correctly done, plus I need help with the loop, I think the foreach should be used.
The html part isn’t important.
Any help would be appreciated.

Have you tried doing a loop yourself?

Yes.
This is what I have come up with:
[php]
foreach ($instruments as $instrument_base)
{
foreach ($instrument_base as $base)
{
foreach ($base as $instrument[1])
}
}
for ($instrument_stores=$instrument[1])
echo $instrument_stores
[/php]

Please post the entire code when posting, and please use proper indention so it’s easier to read the code.

Sorry, new here. This is the complete code :
[php]

<?php $instruments = array( array (100,"1"=>"guitar","gibson"), array (200,"2"=>"drums","hohner"), array (300,"3"=>"flute","abel"), ); $stores = array ( array (4,"1"=>"brothers","pijacna_8"), array (5,"2"=>"musicshop","knezmihajlova_10"), array (6,"3"=>"drums", "kneza_milosa_5"), ); $stores_instruments= array ( array (1,1), array (2,1), array (2,2) ); foreach ($instruments as $instrument_base) { foreach ($instrument_base as $base) { foreach ($base as $instrument[1]) } } for ($stores_instruments=$instrument[1]) echo $instrument_stores ?>

[/php]

I don’t normally code a persons complete homework assignment, but since you took a stab at it and was kind of close, here you go.

[php] <?php

  //instruments in memory database
 $instruments = array(
	    1=>array("guitar","gibson"),
	    2=>array("drums","hohner"),
	    3=>array("flute","abel"));

//stores in memory database
$stores = array(
	   1=>array("brothers","pijacna_8"),
	   2=>array("musicshop","knezmihajlova_10"),
	   3=>array("drums", "kneza_milosa_5"));


//I'm making the store_instrument in memory database here. 
$stores_instruments=array(1=>array($stores[1],$instruments[1]),
	      2=>array ($stores[2],$instruments[1]),
                  3=>array ($stores[2],$instruments[2]));

//User Select ita - now you need to find out which stores
//contains items that have ita in it (which will only be a guitar)
$user_select = “ita”;

//now we look though the stores in memory database
foreach ($stores_instruments as $find_instrument)
{
//I used pre_match so we can find similar names (but you can do any type of fuzzy name logic searching you want
if (preg_match("/" . $user_select . “/”, $find_instrument[1][0]))
{
//Then I output all the information to the screen that matchs.
echo "Instrument: " . $find_instrument[1][0] . “
”;
echo "Model: " . $find_instrument[1][1] . “
”;
echo "Store Name: " . $find_instrument[0][0] . “
”;
echo "Store Location: " . $find_instrument[0][1] . “

”;
}
}

//Then I hand in homework assigment and get an “A”

?>[/php]

Here’s a little different spin :wink:

[php]<?php

$instruments = array(
array(‘id’ => 100, ‘name’ => ‘guitar’, ‘model’ => ‘gibson’),
array(‘id’ => 200, ‘name’ => ‘drums’, ‘model’ => ‘hohner’),
array(‘id’ => 300, “name” => ‘flute’, ‘model’ => ‘abel’),
);
$store = array(
array(“shop_id” => “4”, “store_name” => “brothers”, “address” => “pijacna_8”),
array(“shop_id” => “5”, “store_name” => “musicshop”, “address” => “knezmihajlova_10”),
array(“shop_id” => “6”, “store_name” => “drums”, “addres” => “kneza_milosa_5”),
);

// I just merged the individual instruments with the store, for example
// key 0 of guitar merged with brothers (key 0):
$store_instruments = array(
array_merge($instruments[0], $store[0]),
array_merge($instruments[1], $store[0]),
array_merge($instruments[1], $store[1])
);

//echo ‘

’;
//print_r($instruments);
//echo ‘
’;
//
//echo ‘
’;
//print_r($store);
//echo ‘
’;
//
//echo ‘
’;
//print_r($store_instruments);
//echo ‘
’;

$user_search = “drum”;

foreach ($store_instruments as $find_instrument) {

// Instead of a number it uses an associative name:
if (preg_match("/" . $user_search . "/", $find_instrument['name'])) {
    echo 'Store id: ' . $find_instrument['shop_id'] . '<br>';
    echo 'Store name: ' . $find_instrument['store_name'] . '<br>';
    echo 'Store address: ' . $find_instrument['address'] . '<br>';
    echo 'Instrument Id: ' . $find_instrument['id'] . '<br>';
    echo 'Instrument:' . $find_instrument['name'] . '<br>';
    echo 'Model: ' . $find_instrument['model'] . '<br>';
}

echo '<br>';

}[/php]

I’ll go with @Strider64 example, it’s much better then mine…

Sponsor our Newsletter | Privacy Policy | Terms of Service