Jump to content

Needles, haystacks, and arrays


NotionCommotion

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>');

?>
Link to comment
Share on other sites

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;
}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.