Mam tablicę, która wyglądała tak

Array
(
    [child-safety-lock] => Array
        (
            [0] => Array
                (
                    [id] => 652
                    [name] => CHILD LOCK (YES/NO)
                )

            [1] => Array
                (
                    [id] => 336
                    [name] => CHILD LOCK (YES/NO)
                )

            [2] => Array
                (
                    [id] => 19
                    [name] => Child Lock
                )

            [3] => Array
                (
                    [id] => 398
                    [name] => CHILD LOCK (YES/NO)
                )

            [4] => Array
                (
                    [id] => 155
                    [name] => CHILD SAFETY LOCK
                )

            [5] => Array
                (
                    [id] => 329
                    [name] => CHILD LOCK
                )

            [6] => Array
                (
                    [id] => 96
                    [name] => CHILD LOCK
                )

            [7] => Array
                (
                    [id] => 184
                    [name] => CHILD SAFETY LOCK
                )

            [8] => Array
                (
                    [id] => 875
                    [name] => CHILD SAFETY LOCK (YES/NO)
                )

            [9] => Array
                (
                    [id] => 450
                    [name] => CHILD LOCK (YES/NO)
                )

            [10] => Array
                (
                    [id] => 149
                    [name] => CHILD SAFE LOCK
                )

            [11] => Array
                (
                    [id] => 374
                    [name] => CHILD LOCK (YES/NO)
                )

            [12] => Array
                (
                    [id] => 491
                    [name] => CHILD LOCK (YES/NO)
                )

            [13] => Array
                (
                    [id] => 622
                    [name] => CHILD LOCK (YES/NO)
                )

            [14] => Array
                (
                    [id] => 1309
                    [name] => CHILD LOCK (YES/NO)
                )

            [15] => Array
                (
                    [id] => 1336
                    [name] => CHILD LOCK (YES/NO)
                )

            [16] => Array
                (
                    [id] => 1466
                    [name] => CHILD LOCK (YES/NO)
                )

            [17] => Array
                (
                    [id] => 1577
                    [name] => CHILD SAFETY LOCK (YES/NO)
                )

            [18] => Array
                (
                    [id] => 1715
                    [name] => CHILD SAFETY LOCK (YES/NO)
                )

            [19] => Array
                (
                    [id] => 1908
                    [name] => CHILD LOCK (YES/NO)
                )

        )

    [controls] => Array
        (
            [id] => 84
            [name] => CONTROLS (E.G. TOUCH, DIAL ) 
        )

    [turntable-mm] => Array
        (
            [id] => 77
            [name] => TURNTABLE SIZE (MM)
        )

    [display-type] => Array
        (
            [id] => 78
            [name] => DISPLAY (E.G. LED, LCD, TFT ETC) 
        )

    [smart-inverter-technology] => Array
        (
            [id] => 83
            [name] => SMART INVERTER TECHNOLOGY
        )

    [power-w] => Array
        (
            [id] => 80
            [name] => POWER LEVEL (W)
        )

    [capacity-l] => Array
        (
            [id] => 79
            [name] => CAPACITY  (L)
        )

    [steam] => Array
        (
            [id] => 86
            [name] => STEAM FUNCTION
        )

    [grill-function] => Array
        (
            [id] => 89
            [name] => GRILL FUNCTION
        )

    [sensor-cook] => Array
        (
            [id] => 91
            [name] => SENSOR COOK
        )

    [quick-start] => Array
        (
            [id] => 92
            [name] => QUICK START
        )

    [defrost-function] => Array
        (
            [id] => 93
            [name] => DEFROST
        )

    [keep-warm-function] => Array
        (
            [id] => 97
            [name] => KEEP WARM
        )

    [time-remaining-display] => Array
        (
            [id] => 98
            [name] => TIME REMAINING DISPLAY
        )

    [reheat-function] => Array
        (
            [id] => 95
            [name] => REHEAT
        )

    [optional-trim-kit] => Array
        (
            [id] => 99
            [name] => TRIM KIT
        )

)

Jak mogę zwrócić klucz „zabezpieczenie przed dziećmi”, na przykład wpisując 652 lub 336? Innym przykładem jest zwracanie klucza tablicy „controls”, jeśli wpiszę identyfikator „84”? Próbowałem użyć w tym celu array_filter (), ale nie mogę tego zrobić, czy ktoś może wskazać mi właściwy kierunek?

Mają również tę funkcję, ale to nie działa

function in_array_r($needle, $haystack, $strict = false) {
    foreach ($haystack as $item) {
        if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict)!==FALSE)) {
            return key($haystack);
        }
    }

    return false;
}
0
Elland 20 grudzień 2019, 08:28

3 odpowiedzi

Powinno działać -

// Reconstruct array to simplify
$temp = [];
foreach ($arr as $key => $val) {
    $temp[$key] = $val[0];
}
// extract key & required values to a temporary array
$temp = array_combine(array_keys($temp), array_column($temp, 'id'));
// look for the values and extract the key
$key = array_search(84, $temp);

Bibliografia -

https://www.php.net/manual/en/function.array-search.php https://www.php.net/manual/en/function.array-combine.php. https://www.php.net/manual/en/function.array-column.php

0
Sougata Bose 20 grudzień 2019, 09:44
Dodał jeszcze jakieś pytanie.
 – 
Elland
20 grudzień 2019, 10:02
Cześć, działa tylko wtedy, gdy istnieje wiele tablic podrzędnych
 – 
Elland
2 styczeń 2020, 05:55

Utwórz oddzielne tablice i uzyskaj do nich dostęp poprzez indeks:

$data = [
    'child-safety-lock' => [
        ['id' => 652, 'name' => 'CHILD LOCK (YES/NO)'],
        ['id' => 336, 'name' => 'CHILD LOCK (YES/NO)'],
        ['id' => 19, 'name' => 'Child Lock'],
    ],
    'controls' => [
        'id' => 84, 'name' => 'CONTROLS (E.G. TOUCH, DIAL )'
    ]
];

$childSafetyLock = array_column($data['child-safety-lock'], 'name', 'id');
$controls = array_column([$data['controls']], 'name', 'id');

var_dump(
    $childSafetyLock[652],
    $childSafetyLock[336],
    $controls[84],
);

Lub bardziej uniwersalne rozwiązanie:

$data = [
    'child-safety-lock' => [
        ['id' => 652, 'name' => 'CHILD LOCK (YES/NO)'],
        ['id' => 336, 'name' => 'CHILD LOCK (YES/NO)'],
        ['id' => 19, 'name' => 'Child Lock'],
    ],
    'controls' => [
        'id' => 84, 'name' => 'CONTROLS (E.G. TOUCH, DIAL )'
    ]
];

$preparedData = [];
foreach ($data as $key => $value) {
     $preparedData[$key] = (count($value) === count($value, COUNT_RECURSIVE)) 
        ? [$value] 
        : $value;
}

$groupedData = [];
foreach ($preparedData as $key => $value) {
    $groupedData[$key] = array_column($value, 'name', 'id');
}

var_dump(
    $groupedData['child-safety-lock'][652],
    $groupedData['child-safety-lock'][336],
    $groupedData['controls'][84],
);
0
cetver 20 grudzień 2019, 11:06

Jeśli masz tylko jeden klucz, który ma wiele elementów tablicy podrzędnej, a wszystkie te identyfikatory są unikalne, możesz po prostu zamapować je na klucze:

<?php

$data =
[
    'biscuits' => [
        [
            'id' => 3,
            'name' => 'Digestive'
        ],
        [
            'id' => 4,
            'name' => 'Rich tea'
        ]
    ],
    'cake' =>
    [
        'id' => 5,
        'name' => 'Victoria'
    ]
];

foreach($data as $k => $item) {
    if($k == 'biscuits') {
        foreach($item as $biscuit) {
            $id_key[$biscuit['id']] = $k;
        }
    }
    else {
        $id_key[$item['id']] = $k;
    }
}
var_export($id_key);

Wynik:

array (
    3 => 'biscuits',
    4 => 'biscuits',
    5 => 'cake',
  )
0
Progrock 20 grudzień 2019, 14:05