0

I have two multidimensional arrays:

$instruments = array( array( ID => 'value', 
             Name => 'value', 
             Model => 'value '
            ),
          array( ID => 'value', 
             Name => 'value', 
          Model => ' value'            
		),
          array(ID => 'value', 
             Name => 'value', 
             Model => 'value '
            ),
		ID => 'value', 
             Name => 'value', 
             Model => 'value '
            ),


            ) 
         );

$stores= array( array( ID => 'value', 
             Name => 'value', 
             Adress => 'value '
            ),
          array( ID => 'value', 
             Name => 'value', 
          Adress => ' value'            
		),
          array(ID => 'value', 
             Name => 'value', 
             Adress => 'value '
            ),
		ID => 'value', 
             Name => 'value', 
             Adress => 'value '
            ),


            ) 
         );

I need to make a loop that will merge this arrays, and then get the requested data(instrument ID, name, model and information about stores where to get them) based on user input (instrument name).

2
Contributors
6
Replies
7
Views
5 Years
Discussion Span
Last Post by cereal
0

I made a third array:

$arrayInstrumentsStores = array(
"relation1" =>array("instID"=>"value","storeID"=>"value"),
"relation2" =>array("instID"=>"value","storeID"=>"value"),
"relation3" =>array("instID"=>"value","storeID"=>"value"),
"relation4" =>array("instID"=>"value","storeID"=>"value"),
"relation5" =>array("instID"=>"value","storeID"=>"value"),
"relation6" =>array("instID"=>"value","storeID"=>"value"),
"relation7" =>array("instID"=>"value","storeID"=>"value"),
"relation8" =>array("instID"=>"value","storeID"=>"value"),
"relation9" =>array("instID"=>"value","storeID"=>"value"),
"relation10" =>array("instID"=>"value","storeID"=>"value"),
"relation11" =>array("instID"=>"value","storeID"=>"value"),
"relation12" =>array("instID"=>"value","storeID"=>"value"),);

As you can see, this array stores pairs of ID's, instrument ID and store ID.
The only purpose of this array is to establish relations between first two arrays(so program knows in what store can particular instrument be found).
Just to mention, these are dynamic arrays.

Edited by nera1981: n/a

0

I can't think any way to merge the first two arrays, there is no data that can tell where an instrument is. Also, more than a store can have an instrument, so I came up with an example. Array $a is where you save instrument names as keys and store_id as values. Array $b is the store array.

<?php
$a = array(
    'viola' => array('1','2','12','13'),
    'guitar' => array('2','3','8'),
    'trumpet' => array('5'),
    'banjo' => array('6','10'),
    'flute' => array('4'),
    'oboe' => array('5','11'),
    'ukulele' => array('23'),
    'tuba' => array('1','5'),
    'cello' => array('7')
);

$var = 'guitar'; # $_POST or $_GET
$result = $a[$var]; # retrieve stores id

$stores = array(
    '1' => array('name' => 'Rock Store 1','address' => 'another street n1'),
    '2' => array('name' => 'Rock Wave','address' => 'that plaza n2'),
    '3' => array('name' => 'Jazz Store 2','address' => 'avenue n1115'),
    '4' => array('name' => 'Bohemian Rhapsody Store','address' => 'plaza n2'),
    '5' => array('name' => 'Indie Music Instruments','address' => '3rd street n123'),
    '6' => array('name' => 'Whatever Music Co.','address' => 'avenue n222'),
    '7' => array('name' => 'Blues Brothers','address' => '1060 West Addison')
);

echo $var . "\n\n";
# display store names and addresses
foreach($result as $key => $value)
{
    echo $stores[$value]['name'] . "\n";
    echo $stores[$value]['address'] . "\n\n";
}

?>

The above will output:

guitar

Rock Store 1
another street n1

Indie Music Instruments
3rd street n123

Blues Brothers
1060 West Addison

Hopefully someone else will help you to find a better solution.
I'm almost sure array_keys() can be used to make something better: http://www.php.net/manual/en/function.array-keys.php but with multidimensional arrays it's a problem, there's a function in the notes that half-works, finds the right values but the keys returned are "wrong", at least for me right now (too late! :D) I will try tomorrow.

0

I can't think any way to merge the first two arrays, there is no data that can tell where an instrument is. Also, more than a store can have an instrument, so I came up with an example. Array $a is where you save instrument names as keys and store_id as values. Array $b is the store array.

<?php
$a = array(
    'viola' => array('1','2','12','13'),
    'guitar' => array('2','3','8'),
    'trumpet' => array('5'),
    'banjo' => array('6','10'),
    'flute' => array('4'),
    'oboe' => array('5','11'),
    'ukulele' => array('23'),
    'tuba' => array('1','5'),
    'cello' => array('7')
);

$var = 'guitar'; # $_POST or $_GET
$result = $a[$var]; # retrieve stores id

$stores = array(
    '1' => array('name' => 'Rock Store 1','address' => 'another street n1'),
    '2' => array('name' => 'Rock Wave','address' => 'that plaza n2'),
    '3' => array('name' => 'Jazz Store 2','address' => 'avenue n1115'),
    '4' => array('name' => 'Bohemian Rhapsody Store','address' => 'plaza n2'),
    '5' => array('name' => 'Indie Music Instruments','address' => '3rd street n123'),
    '6' => array('name' => 'Whatever Music Co.','address' => 'avenue n222'),
    '7' => array('name' => 'Blues Brothers','address' => '1060 West Addison')
);

echo $var . "\n\n";
# display store names and addresses
foreach($result as $key => $value)
{
    echo $stores[$value]['name'] . "\n";
    echo $stores[$value]['address'] . "\n\n";
}

?>

The above will output:

guitar

Rock Store 1
another street n1

Indie Music Instruments
3rd street n123

Blues Brothers
1060 West Addison

Hopefully someone else will help you to find a better solution.
I'm almost sure array_keys() can be used to make something better: http://www.php.net/manual/en/function.array-keys.php but with multidimensional arrays it's a problem, there's a function in the notes that half-works, finds the right values but the keys returned are "wrong", at least for me right now (too late! :D) I will try tomorrow.

I don't think this is what I want, because structure of arrays is predetermined. But thanks anyway

0

Try this, is done with your arrays:

<?php
$instruments = array(
	array( 'ID' => '1', 'Name' => 'Guitar', 'Model' => 'Gibson Dark Fire'),
	array( 'ID' => '2', 'Name' => 'Guitar', 'Model' => 'Gibson Dusk Tiger'),
	array( 'ID' => '3', 'Name' => 'Banjo', 'Model' => 'Mad Moon'),
	array( 'ID' => '4', 'Name' => 'Ukulele', 'Model' => 'Koa'),
	array( 'ID' => '5', 'Name' => 'Guitar', 'Model' => 'Gibson Robot Guitar')
);
$stores= array(
	array('ID' => '1', 'Name' => 'Rock Store 1', 'Address' => 'another street n1'),
	array('ID' => '2', 'Name' => 'Bohemian Rhapsody Store', 'Address' => 'plaza n2'),
	array('ID' => '3', 'Name' => 'Indie Music Instruments', 'Address' => '3rd street n123'),
	array('ID' => '4', 'Name' => 'Blues Brothers', 'Address' => '1060 West Addison ')
);

$arrayInstrumentsStores = array(
	"relation1" =>array("instID"=>"1","storeID"=>"1"),
	"relation2" =>array("instID"=>"1","storeID"=>"2"),
	"relation3" =>array("instID"=>"4","storeID"=>"3"),
	"relation4" =>array("instID"=>"2","storeID"=>"1"),
	"relation5" =>array("instID"=>"3","storeID"=>"2"),
	"relation6" =>array("instID"=>"5","storeID"=>"4")
);

$string = 'Guitar';
$search = ucfirst(strtolower($string));
$result = '';

foreach($instruments as $key => $value)
{
	if($value['Name'] == $search)
	{
		$a = $value['ID'];
		foreach($arrayInstrumentsStores as $k => $v)
		{
			if($v['instID'] == $a)
			{
				$result[$v['storeID'].':'.$a]['storeID'] = $v['storeID'];
				$result[$v['storeID'].':'.$a]['instID'] = $a;
				$result[$v['storeID'].':'.$a]['instName'] = $value['Name'];
				$result[$v['storeID'].':'.$a]['instModel'] = $value['Model'];
			}
		}


	}
}
#print_r($result);
echo "\n";
$c = count($stores);
if(is_array($result))
{
	for($i = 0; $i < $c; $i++)
	{
		foreach($result as $key =>$value)
		{
			if($stores[$i]['ID'] == $value['storeID'])
			{
				echo $stores[$i]['Name'] . "\n";
				echo $stores[$i]['Address'] . "\n";
				echo $value['instName'] . "\n";
				echo $value['instModel'] . "\n\n";
			}
		}
	}
} else {
	echo 'not found' . "\n";
}

?>

The output will be:

Rock Store 1
another street n1
Guitar
Gibson Dark Fire

Rock Store 1
another street n1
Guitar
Gibson Dusk Tiger

Bohemian Rhapsody Store
plaza n2
Guitar
Gibson Dark Fire

Blues Brothers
1060 West Addison 
Guitar
Gibson Robot Guitar

So if you have more than a model of guitar you will get all the relations. hope is useful. Bye :)

Edited by cereal: added is_array() for empty results

0

Cereal, this works perfectly(although I changed output format). Thanks a lot.

Edited by nera1981: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.