NotionCommotion Posted December 5, 2014 Share Posted December 5, 2014 I have the following mapping array: array( array('name'=>'nothing','options'=>array()), array('name'=>'aaa','options'=>array('a')), array('name'=>'bbb','options'=>array('b')), array('name'=>'ccc','options'=>array('c')), array('name'=>'aaa and bbb','options'=>array('a','b')), array('name'=>'aaa and ccc','options'=>array('a','c')), array('name'=>'bbb and ccc','options'=>array('b','c')), array('name'=>'aaa, bbb, and ccc','options'=>array('a','b','c'))) Given array(), I would like to return 'nothing', given array('c'), I would like to return 'ccc', given array('b','c'), I would like to return 'bbb and ccc', given array('a','b','c'), I would like to return 'aaa, bbb, and ccc', etc. How is this best accomplished? As an alternate solution, instead of being given the above arrays, I could be given the following strings "", "c", "b~c", "a~b~c" and I can change the above mapping array to include these strings instead of the arrays I show. Please reference http://forums.phpfreaks.com/topic/292921-query-table-and-return-sub-arrays/ for history. Would this be a better approach? How would I ensure the order of the JOINed values were always in the correct order for my mapping array? Quote Link to comment Share on other sites More sharing options...
Adam Posted December 6, 2014 Share Posted December 6, 2014 Do your homework. Quote Link to comment Share on other sites More sharing options...
hansford Posted December 6, 2014 Share Posted December 6, 2014 I don't even fully understand the question of what you/they are wanting. You have a main array and within the array you have other arrays. So, array[0] = a key/value paired array. to access you would do: array[0]['name']; array['options'] = an empty array. array[0]['options'][0] = first element of empty array. You can determine how to access subsequent arrays and their values by determining if the array is an indexed array or a key/value array. Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted December 6, 2014 Author Share Posted December 6, 2014 Let me give a working example. Is this really the best way to accomplish this, or is there a better way? <?php function test($map,$array1) { $value=false; foreach($map as $elem) { if(array_diff($array1, $elem['options']) === array_diff($elem['options'], $array1)) { $value=$elem['name']; break; } } return $value; } $map=array( array('name'=>'nothing','options'=>array()), array('name'=>'aaa','options'=>array('a')), array('name'=>'bbb','options'=>array('b')), array('name'=>'ccc','options'=>array('c')), array('name'=>'aaa and bbb','options'=>array('a','b')), array('name'=>'aaa and ccc','options'=>array('a','c')), array('name'=>'bbb and ccc','options'=>array('b','c')), array('name'=>'aaa, bbb, and ccc','options'=>array('a','b','c')) ); echo(test($map,array()).'<br>'); echo(test($map,array('c')).'<br>'); echo(test($map,array('b','c')).'<br>'); echo(test($map,array('a','b','c')).'<br>'); ?> Quote Link to comment Share on other sites More sharing options...
Barand Posted December 6, 2014 Share Posted December 6, 2014 If your arrays and options are always sorted in the same order function test($map,$array1) { $value=false; foreach($map as $elem) { if($array1 == $elem['options']) { $value=$elem['name']; break; } } return $value; } Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted December 6, 2014 Author Share Posted December 6, 2014 If your arrays and options are always sorted in the same order I am currently not positive. I am sure you know, and while in hindsight it is obvious, array('a','b') does not have equality (and obviously not identity) to array('b','a'). Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.