duanboshi1472
duanboshi1472
2017-04-22 16:50
浏览 47
已采纳

在多维数组中搜索元素并在其位置返回值

I have this multidimensional array created through PHP:

 $values = array
 (
   array("Tom", "apple"),
   array("Mike", "banana"),
   array("Sam", "coconut")
 );

 $search = "Tom";

How could I search for a specific value in the array and find the next item within the same location?

Let's say the search value is "Tom", the value returned should be "apple" or "Mike" should return "banana".

I guess something like this $values[0][1] would be able to return "apple" from the array but I am not sure how to search for a specific value (which would be set through a variable) rather than having to do it manually.

Overall:

  • There is a multi-dimensional array

  • There is a variable containing a value (which will always be item [0] in its own location

  • The value next to what has been searched for should be returned (item [1] in its location)

图片转代码服务由CSDN问答提供 功能建议

我有通过PHP创建的这个多维数组:

  $  values = array 
(
 array(“Tom”,“apple”),
 array(“Mike”,“banana”),
 array(“Sam”,“coconut”)
); 
  
 $ search =“Tom”; 
   
 
 

我如何搜索数组中的特定值并找到同一位置中的下一个项目?

假设搜索值为“Tom”,返回的值应为“apple”或“Mike”应返回“banana”。

我猜是 像这样 $ values [0] [1] 将能够从数组中返回“apple”,但我不知道如何搜索特定值(通过变量设置) 而且必须手动完成。

整体:

  • 有一个多 -dimensional数组

  • 有一个包含值的变量(在自己的位置始终是item [0] < li>

    搜索到的内容旁边的值 应该返回(项目[1]在其位置)

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • duanlong4890
    duanlong4890 2017-04-22 17:01
    已采纳

    Would be easier, if you create an array with named keys. Instead of multidimensional array try something like this:

    $values = [
        "Tom" => "apple",
        "Mike" => "banana"
    ]
    

    or even

       $values = [
            "Tom" => [
                "fruit" => "apple",
                "car" => "Audi"
            ],
            "Mike" => [
                "fruit" => "banana",
                "car" => "VW"
            ]
        ]
    

    and search

    function getUserKeyValue($username, $key){
        return isset($values[$username][$key] ? $values[$username][$key] : null;
    }
    

    for example

    getUserKeyValue('Tom', 'fruit') or getUserKeyValue('Mike', 'car')
    
    点赞 评论
  • douzhao7634
    douzhao7634 2017-04-22 17:06

    the name to be searched must be in $values[0][i] where i iterates from 0 to c-1 //c is the no. of col. when the name is found we return $values[1][i] as the item next to it

    点赞 评论
  • douzao5487
    douzao5487 2017-04-22 17:36

    You should be able to do it like that:

     $find = $values[array_search($search, array_column($values, 0))][1];
    

    array_column (documentation) extracts your first "column" (think of your multidimensional array as a table with each smaller array as a row). The result is:

     array(
       "Tom", 
       "Mike", 
       "Sam"
     )
    

    array_search (documentation) then searches this array for the first occurrence of your $searchvalue. In the case of Tom, this would yield 0.
    With that index, you can get the value from your array.

    If you are not sure if the value exists in your array, check that first:

    $index = array_search($search, array_column($values, 0));
    $find = $index !== false ? $values[$index][1] : false;
    

    The A ? B : C construct is the ternary operator. It's a short form of if A then B else C.

    点赞 评论

相关推荐