/    Sign up×
Community /Pin to ProfileBookmark

ArraysMerge&Search

I have two dynamic multidimansional arrays:

[CODE]$arrayInstruments = array(
“instrument1″=>array(“ID”=>”value”,”name”=>”value”,”model”=>value),
“instrument2″=>array(“ID”=>”value”,”name”=>”value”,”model”=>value),
“instrument3″=>array(“ID”=>”value”,”name”=>”Kg”,”model”=>value),
“instrument4″=>array(“ID”=>”value”,”name”=>”value”,”model”=>value),
“instrument5″=>array(“ID”=>”value”,”name”=>”value”,”model”=>value)
);

$arrayStores = array(
“store1″=>array(“storeID”=>”value”,”name”=>”value”,”address”=>”value”),
“store2″=>array(“storeID”=>”value”,”name”=>”value”,”address”=>”value”),
“store3″=>array(“storeID”=>”value”,”name”=>”value”,”address”=>”value”),
“store4″=>array(“storeD”=>”value”,”name”=>”value”,”address”=>”value”),
“store5″=>array(“storeID”=>”value”,”name”=>”value”,”address”=>”value”)
);
[/CODE]

I need to make a program that would search through these arrays and return data based on user input(user enters instrument name). This output should be list of instruments and stores where they can be found. User input is stored in simple variable at the program’s start($instrumentName=”some value”).

I got suggestion to make third array, so there can be established relation between these two arrays(it tells program in what store can particular instrument can be found).
So I made this array:

[CODE]$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”),);

[/CODE]

As you can se, it stores pairs of ID’s (instrument’s ID and store’s ID).
I would appreciate any help.

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@chris0Sep 28.2011 — now lets say you are searching in
[code=php]
$arrayStores = array(
"store1"=>array("storeID"=>"value","name"=>"value","address"=>"value"),
"store2"=>array("storeID"=>"value","name"=>"value","address"=>"value"),
"store3"=>array("storeID"=>"value","name"=>"value","address"=>"value"),
"store4"=>array("storeD"=>"value","name"=>"value","address"=>"value"),
"store5"=>array("storeID"=>"value","name"=>"value","address"=>"value")
);
[/code]


[code=php]
$res = array();
foreach ($arrayStores as $key => $val)
{
// $key = "store1";
// $val = array("storeID"=>"value","name"=>"value","address"=>"value");
if ($val['name']=='searchstring') { $res[] = $val['storeID']; }
}
[/code]
Copy linkTweet thisAlerts:
@nera1981authorSep 29.2011 — now lets say you are searching in
[code=php]
$arrayStores = array(
"store1"=>array("storeID"=>"value","name"=>"value","address"=>"value"),
"store2"=>array("storeID"=>"value","name"=>"value","address"=>"value"),
"store3"=>array("storeID"=>"value","name"=>"value","address"=>"value"),
"store4"=>array("storeD"=>"value","name"=>"value","address"=>"value"),
"store5"=>array("storeID"=>"value","name"=>"value","address"=>"value")
);
[/code]


[code=php]
$res = array();
foreach ($arrayStores as $key => $val)
{
// $key = "store1";
// $val = array("storeID"=>"value","name"=>"value","address"=>"value");
if ($val['name']=='searchstring') { $res[] = $val['storeID']; }
}
[/code]
[/QUOTE]

This make no sense, because user input is instrument name, not store name(searchString).
Copy linkTweet thisAlerts:
@007JulienOct 01.2011 — Maybe something like that
[code=php]
<?php
// Searching $instrumentID with $instrumentName="somme value";
foreach ($arrayInstruments as $key => $val){
// $key = "instrument";
// $val = array("ID"=>"value","name"=>"value","model"=>value);
if ($val['name'] == $instrumentName) {$instrumentID = $val['ID'];break;}

// Searching all storeID's with $instrumentID
$storesID = array();
foreach ($arrayInstrumentsStores as $key => $val){
// $key = "relation";
// $val = array("instID"=>"value","storeID"=>"value")
if ($val['instID']== $instrumentID) {$storesID['storeID']=1;}

// Searching stores names and adress with their ID
$allStores = array();
foreach ($arrayStores as $key => $val){
// $key = "store";
// $val = array("storeID"=>"value","name"=>"value","address"=>"value");
if (isset($storesForInstrumentID[$val['streID']])) {$allStores[] = $val; }
?>
[/code]
Copy linkTweet thisAlerts:
@nera1981authorOct 01.2011 — I found solution, I will post code later. Thanks everyone who posted
×

Success!

Help @nera1981 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...