0

I want to know all keys whose values are same.

I have below array:

[monday] => 10:00 - 11:00, 
[tuesday] => 11:00 - 12:00,
[wednesday] => 10:00 - 11:00,
[friday] => 10:00 - 11:00,
[saturday] => 11:00 - 12:00,
[sunday] => 14:00 - 15:00,

monday/wednesday/friday = 10:00 - 11:00.

tuesday/saturday = 11:00 - 12:00.

sunday = 14:00 - 15:00.

Please help me.

Thanks in advance.

1

1 Answer 1

4

You can do a simple foreach loop and join a new array depending on values:

<?php

$worktimes = array(
    'monday' => '10:00 - 11:00', 
    'tuesday' => '11:00 - 12:00',
    'wednesday' => '10:00 - 11:00',
    'friday' => '10:00 - 11:00',
    'saturday' => '11:00 - 12:00',
    'sunday' => '14:00 - 15:00'
);

$combined = array();
foreach ( $worktimes as $key => $value ) {
    $combined[$value][] = $key;
}

print_r( $combined );

?>

Now you have a new array that holds you key with all the values that match it.

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

3 Comments

And how does this not answer the question?
@bukfixart This does answer the question. Have you read the question?
@bukfixart The resultant array is Array ( [10:00 - 11:00] => Array ( [0] => monday [1] => wednesday [2] => friday ) [11:00 - 12:00] => Array ( [0] => tuesday [1] => saturday ) [14:00 - 15:00] => Array ( [0] => sunday ) ) which is in fact, the answer of the question.

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.