0

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?

2
  • "Cleaner" is obviously subjective. I personally tend to favor more verbose code if it reads easier, but sometimes that breaks down at scale. You could use array_column to get the individual field values, and array_unique to filter, and then just a simple count: 3v4l.org/Ttsef Commented Nov 5, 2021 at 16:40
  • but how can I know which ones are the same so I can just use those arrays and output data Commented Nov 5, 2021 at 16:44

2 Answers 2

1

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"
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

1

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"
    }
  }
}

4 Comments

thanks, this function is nice, one more question that is similar, how can I check if the year is most recent ones? if array one for example had year 2011, how can I compare it to other arrays to see which one is most recent one and just return that array?
maybe you can submit another question for this?
yeah, writing another one now
I asked it here

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.