0

Having an array like [1]

$arr = array(
        array(
            "ignoreMe" => "123",
            "checkMe" => "value",
        ),
        array(
            "ignoreMe" => "456",
            "checkMe" => "value",
        ),
 );

I'd like to check if special keys (here the key checkMe) of the inner array have the same value. If all keys have the same value, then I'd like to remove the key from the inner array. (from all arrays)

But when having an array like [2]

$arr = array(
        array(
            "ignoreMe" => "123",
            "checkMe" => "value",
        ),
        array(
            "ignoreMe" => "456",
            "checkMe" => "value",
        ),
        array(
            "ignoreMe" => "789",
            "checkMe" => "foo", 
        ),
 );

All keys should stay intact.

How would I do this with this complex validator? (Link https://github.com/Respect/Validation)

Expected result of [1] is

$arr = array(
        array(
            "ignoreMe" => "123",
        ),
        array(
            "ignoreMe" => "456",
        ),
 );

[2] should not be touched

Here is what has been tried:

$validator = v::arr()->each(v::key("check", v::equals('value')));
Sverri M. Olsen
  • 13,055
  • 3
  • 36
  • 52
paskl
  • 589
  • 4
  • 25
  • @Rikesh I've added the expected result to my question. – paskl Nov 11 '14 at 12:14
  • What have you tried? I do not know about everyone else, but I am not here to teach people how to use third-party libraries. You should teach yourself, or take some course. – Sverri M. Olsen Nov 11 '14 at 12:16
  • @SverriM.Olsen `$validator = v::arr()->each(v::key("check", v::equals('value')));` Would take a specified value to that key. Problem is, that value of the key is not always the same. Thats where I'm stuck. – paskl Nov 11 '14 at 12:24
  • Think I'll go the other way around of not using this lib with this specific problem. I know my way around this issue just fine, but it produces less fine code in my opinion. If I do find a way of solving this, I'll come back and report. – paskl Nov 11 '14 at 12:31
  • What version of PHP are you using? – Sverri M. Olsen Nov 11 '14 at 12:32
  • @SverriM.Olsen Up to PHP5.5. – paskl Nov 11 '14 at 12:34
  • @pquerner Eh, >=5.5 or only *up to* 5.5. (i.e. <5.5)? – Sverri M. Olsen Nov 11 '14 at 12:49

1 Answers1

2

Okay, if you are running PHP 5.5+ then you can use a combination of the array_column and array_unique functions to remove the items from the array, if they all have the same value:

I am not sure exactly what such a function would be called, so I just called it myFunc...

function myFunc(array $arr, $key)
{
    // Get all the values using a key
    $values = array_column($arr, $key);

    // Remove all duplicates
    $unique = array_unique($values);

    // If there only is one item left then it means
    // that all the values are the same, so proceed
    // with modifying it...
    if (count($unique) === 1) {

        // Go over each array...
        foreach ($arr as $x => & $value) {

            // And unset the key
            unset($value[$key]);
        }
    }
    // Return the array
    return $arr;
}

Example:

$arr1 = array(
    array("ignoreMe" => "123", "checkMe" => "value"),
    array("ignoreMe" => "456", "checkMe" => "value"),
);
$arr2 = array(
    array("ignoreMe" => "123", "checkMe" => "value"),
    array("ignoreMe" => "456", "checkMe" => "value"),
    array("ignoreMe" => "789", "checkMe" => "foo"),
);

// All the values in this array are the same, so they
// will be removed
var_dump($arr1);
var_dump(myFunc($arr1, 'checkMe'));
echo '<hr>';

// There is a value in this array that is not the same
// as the others, so this array will be left intact
var_dump($arr2);
var_dump(myFunc($arr2, 'checkMe'));
Sverri M. Olsen
  • 13,055
  • 3
  • 36
  • 52