Home > Mobile >  PHP check if certain value in different arrays are the same for all
PHP check if certain value in different arrays are the same for all

Time:11-06

I have couple of arrays like this

$arrayOne = array (
    "name" => 'john',
    "position" => 'instructor',
    "hired"  => '2010',
    "department" => 'math',
);

$arrayTwo = array (
    "name" => 'smith',
    "position" => 'instructor',
    "hired"  => '2010',
    "department" => 'math',
);  

$arrayThree = array (
    "name" => 'dave',
    "position" => 'instructor',
    "hired"  => '2009',
    "department" => 'math',
);  

how can I check if these arrays all have the same hired date?

one way would be compare each individual one:

if($arrayOne['hired'] === $arrayTwo['hired']) & if($arrayOne['hired'] === $arrayThree['hired']) & ...

but is there a cleaner way to do this?

CodePudding user response:

Your initial question and subsequent comment are similar but they are attempting to do different things with the data, with different outcomes. The initial as-asked was:

how can I check if these arrays all have the same hired date

And that can be done with the following:

var_dump(count(array_unique(array_column([$arrayOne, $arrayTwo, $arrayThree], 'hired'))));

// or

$combined = [$arrayOne, $arrayTwo, $arrayThree];
$hiredValues = array_column($combined, 'hired');
$hiredValuesUnique = array_unique($hiredValues);
$length = count($hiredValuesUnique);
var_dump($length);

If the count is 1, they are the same, otherwise they aren't.

But, your follow-up comment was

how can I know which ones are the same

To do that, I'd create a new array that is keyed by that value, and foreach over the source arrays, effectively grouping similar ones for you to further act up.

$final = [];

foreach([$arrayOne, $arrayTwo, $arrayThree] as $array){
    if(!array_key_exists($array['hired'], $final)){
        $final[$array['hired']] = [];
    }
    $final[$array['hired']][] = $array;
}

var_dump($final);

Which produces:

array(2) {
  [2010]=>
  array(2) {
    [0]=>
    array(4) {
      ["name"]=>
      string(4) "john"
      ["position"]=>
      string(10) "instructor"
      ["hired"]=>
      string(4) "2010"
      ["department"]=>
      string(4) "math"
    }
    [1]=>
    array(4) {
      ["name"]=>
      string(5) "smith"
      ["position"]=>
      string(10) "instructor"
      ["hired"]=>
      string(4) "2010"
      ["department"]=>
      string(4) "math"
    }
  }
  [2009]=>
  array(1) {
    [0]=>
    array(4) {
      ["name"]=>
      string(4) "dave"
      ["position"]=>
      string(10) "instructor"
      ["hired"]=>
      string(4) "2009"
      ["department"]=>
      string(4) "math"
    }
  }
}

CodePudding user response:

I have written the code below:

//Your data
$arrayOne = array (
    "name" => 'john',
    "position" => 'instructor',
    "hired"  => '2010',
    "department" => 'math',
);

$arrayTwo = array (
    "name" => 'smith',
    "position" => 'instructor',
    "hired"  => '2010',
    "department" => 'math',
);  

$arrayThree = array (
    "name" => 'dave',
    "position" => 'instructor',
    "hired"  => '2009',
    "department" => 'math',
);  

function hiredIsTheSameEverywhere(...$arrays) : bool  
{
    return count(array_count_values(array_column($arrays, "hired"))) === 1; 
}

function whereHiredIsTheSame(...$arrays) : array 
{
    $return = [];
    $count = array_count_values(array_column($arrays, "hired"));
    foreach($arrays as $array) {
        if($count[$array['hired']] > 1) {
            $return[$array['hired']][] = $array;
        }
    }
    return $return;
}

//The output 
var_dump(hiredIsTheSameEverywhere($arrayOne, $arrayTwo, $arrayThree));
var_dump(whereHiredIsTheSame($arrayOne, $arrayTwo, $arrayThree));

output:

bool(false)
array(1) {
  [2010]=>
  array(2) {
    [0]=>
    array(4) {
      ["name"]=>
      string(4) "john"
      ["position"]=>
      string(10) "instructor"
      ["hired"]=>
      string(4) "2010"
      ["department"]=>
      string(4) "math"
    }
    [1]=>
    array(4) {
      ["name"]=>
      string(5) "smith"
      ["position"]=>
      string(10) "instructor"
      ["hired"]=>
      string(4) "2010"
      ["department"]=>
      string(4) "math"
    }
  }
}
  • Related