获取另一个元素已知的数组元素?

时间:2018-12-17 20:52:33

标签: php arrays

Array
(
    [0] => Array
        (
            [what] => b4
            [map] => 74,76,77,83
        )

    [1] => Array
        (
            [what] => b2
            [map] => 53,82
        )

    [2] => Array
        (
            [what] => b1
            [map] => 36
        )
 )

abc('b4');

function abc($what){
    $map = // element `map` where `what` = $what;
}

所以我需要得到map,其中what等于$what

例如-如果$whatb4,则结果应为74,76,77,83;等等。

我该怎么做?

6 个答案:

答案 0 :(得分:4)

如果您要定期访问数据并且what是唯一的,则将array_column()与第三个参数一起用作要用作键的列。然后,您可以使用what轻松访问数组,并且在此答案中不会损害任何循环...

$array = Array
(
    Array
    (
        "what" => "b4",
        "map" => "74,76,77,83"
        ),
    Array
    (
        "what" => "b2",
        "map" => "53,82"
        ),
    Array
    (
        "what" => "b1",
        "map" => "36"
        )
    );

$array = array_column($array, null, "what");

echo $array['b4']['map'];

给予...

74,76,77,83

答案 1 :(得分:3)

使用array_search()array_column(),您可以在一行中获得匹配的$map

<?php

$array = Array
(
    Array
        (
            "what" => "b4",
            "map" => "74,76,77,83"
        ),
   Array
        (
            "what" => "b2",
            "map" => "53,82"
        ),
    Array
        (
            "what" => "b1",
            "map" => "36"
        )
 );

function abc($array, $what) {
   return $array[array_search($what, array_column($array, 'what'))]['map'];
}   

echo abc($array, "b4");

该函数经过解构和解释:

function abc($array /* the complete input array */, $what /* the search string */) {
   // get the key of the sub-array that has $what in column 'what':
   $key = array_search($what, array_column($array, 'what'));
   // use that key to get 'map' on index $key
   return $array[$key]['map'];
}

在这里可以找到有效的小提琴:https://3v4l.org/0NpcX

答案 2 :(得分:2)

我认为“遍历”数组很容易阅读和理解:

<?php
$map = [
    [
        'what' => "b4",
        'map' => "74,76,77,83"
    ],
    [
            'what' => "b2",
            'map' => "53,82"
    ],
    [
            'what' => "b1",
            'map' => "36"
    ]
];

function lookupWhatInMap(&$map, $what) {
    array_walk($map, function($entry, $key) use ($what) {
        if ($entry['what'] == $what) {
            print_r($entry['map']);
        }
    });
}

lookupWhatInMap($map, "b4");

答案 3 :(得分:1)

您要做的就是遍历地图并比较值。

function abc($what){
    $map = [...];
    foreach($map as $item) {
        if (isset($item[$what]) ) {
            return $item["map"];
        }
    }
    return false;
}

答案 4 :(得分:1)

如果您只想从数组中获得1个值,则可以在存在匹配项的地方使用foreach和return语句:

$a = [
    [
        "what" => "b4",
        "map" => "74,76,77,83"
    ],
    [
        "what" => "b2",
        "map" => "53,82"
    ],
    [
        "what" => "b1",
        "map" => "36"
    ]
];
function abc($what, $arrays)
{
    foreach ($arrays as $array) {
        if ($array['what'] === $what) {
            return $array['map'];
        }
    }
    return false;
}

echo(abc('b4', $a)); // 74,76,77,83

Demo

答案 5 :(得分:0)

https://ideone.com/V9WNNx

$arr[] = [
            'what' => 'b4',
            'map' => '74,76,77,83'
        ];
$arr[] = [
            'what' => 'b2',
            'map' => '53,82'
        ];
$arr[] = [
            'what' => 'b1',
            'map' => '36'
        ];

echo abc('b4', $arr);

function abc($what, $arr){
    $map = null;
    $idx = array_search($what, array_column($arr, 'what'));
    if ($idx !== false) {
        $map = $arr[$idx]['map'];
    }
    return $map;
}