Answer the question
In order to leave comments, you need to log in
How to get keys in a multidimensional array that have different values but the same keys?
There is an array like this
Array
(
[0] => Array
(
[title] => Чтение
[value] => да
)
[1] => Array
(
[title] => Тип
[value] => общий
)
[3] => Array
(
[title] => Чтение
[value] => нет
)
[4] => Array
(
[title] => Тип
[value] => общий
)
)
Answer the question
In order to leave comments, you need to log in
In your array, the keys are title and value and the values are Read, Type, yes, no, generic. Call a spade a spade.
Well, the problem is solved by bypassing the array and compiling another array, where the keys will already be Reading and Type, and the values \u200b\u200bare yes, no, common. You just check at each step if the new array does not yet have such a key, add a key/value pair, if there is a key and the value does not match, output.
$input = [
[
'title' => 'Чтение',
'value' => 'да',
],
[
'title' => 'Тип',
'value' => 'общий',
],
[
'title' => 'Чтение',
'value' => 'нет',
],
[
'title' => 'Тип',
'value' => 'общий',
],
];
function get_unique_titles(array $input)
{
$tmp = [];
$result = [];
foreach ($input as $row) {
$tmp[$row['title']][] = $row['value'];
}
foreach ($tmp as $title => $values) {
if ($values == array_unique($values)) {
$result[] = $title;
}
}
return $result;
}
var_dump(get_unique_titles($input));
array(1) {
[0] =>
string(12) "Чтение"
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question