V
V
Vasya Surname2015-11-16 00:56:18
PHP
Vasya Surname, 2015-11-16 00:56:18

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] => общий
        )
)

I need to get keys that have different values, but the same names in this example, this is
reading it in the first case, yes , in the second , no , and ignore the type.
I broke my head how to do it

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rsa97, 2015-11-16
@Rsa97

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.

M
Mikhail Osher, 2015-11-16
@miraage

$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 question

Ask a Question

731 491 924 answers to any question