1

I need to figure out the best way to put together an array where multiple keys have the same value. For example,I need to return LARGE if any of the following values are provided:

"lrge", "lrg", "lg"

I think it should be in a form of multidimensional array. Something like:

$myArr= array (
    "color" = array (
       "RED"   => array("red", "rd", "r"),
       "BLUE"  => array("blue", "blu", "bl")
    ),
    "size" = array (
       "LARGE" => array("lrge", "lrg", "lg"),
       "SMALL" => array("smal", "sml", "sm")
    )
);

Having a blank moment on how to use it:

$cat = "size";    
$val = "lrg";

echo ... // need to return LARGE
6
  • Is it known whether your input value is a color or a size? Or is it unknown? Commented Mar 14, 2018 at 15:02
  • Actually, this is a very good question. I will know whether it's COLOR or SIZE. I'll update post... Commented Mar 14, 2018 at 15:04
  • Also, is it possible for you to use a database for this instead of hardcoded arrays? Commented Mar 14, 2018 at 15:05
  • foreach and in_array() are your best friends for this task. But for bonus points I would change the format of the list to express the mapping in the other direction. Commented Mar 14, 2018 at 15:10
  • 1
    I think it's better to use a lookup table than rolling out an array based solution Commented Mar 14, 2018 at 15:10

2 Answers 2

2

Personally, I'd look into storing these mappings in a database. Long-term, it will probably make your life easier. However, since you already know which attribute you want to be searching, this is actually rather trivial. Just loop through the sub-array specified by your $cat variable, and then use in_array() to see if $val is in that row.

$myArr= array (
    "color" => array (
       "RED"   => array("red", "rd", "r"),
       "BLUE"  => array("blue", "blu", "bl")
    ),
    "size" => array (
       "LARGE" => array("lrge", "lrg", "lg"),
       "SMALL" => array("smal", "sml", "sm")
    )
);

$cat = "size";    
$val = "lrg";

$match = "";

foreach($myArr[$cat] as $key => $row)
{
    if(in_array($val, $row))
    {
        $match = $key;
        break;
    }
}

echo $match;

DEMO

Sign up to request clarification or add additional context in comments.

Comments

2

If the only use of $myArr is to map the values you get from the API to some standard values then the best approach is to create a lookup table that maps the input values to the desired normalized values:

$myArr = array(
    'color' => array(
         'red'  => 'RED',  'rd'  => 'RED',  'r'  => 'RED',
         'blue' => 'BLUE', 'blu' => 'BLUE', 'bl' => 'BLUE',
    ),
    'size' => array(
         'lrge' => 'LARGE', 'lrg' => 'LARGE', 'lg' => 'LARGE',
         'smal' => 'SMALL', 'sml' => 'SMALL', 'sm' => 'SMALL',
    ),
);

This way, the normalization is as simple as:

$cat = 'size';
$val = 'lrg';

$normal = $myArr[$cat][$val];

1 Comment

Although I accepted the other answer, it's just because it was addressing exactly my question. However, after seeing this post I may change my approach to avoid loops. +1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.