【问题标题】:PHP. How to get the position of an element inside an unidimensional and multidimensional array? [duplicate]php。如何获取一维和多维数组中元素的位置? [复制]
【发布时间】:2019-03-05 18:50:42
【问题描述】:

例如,在下面的代码中,如何获取给定元素在数组中的位置-order-:

<?php
$cars=array("Volvo","BMW","Toyota","Tesla","Volkswagen");
//for Volvo, an echo statement should return 1
//for BMW, an echo statement should return 2
//for Toyota, an echo statement should return 3 ... and so on ...
?>

更新:在收到有关search_array() 实现的一些有用贡献后,我想知道是否可以将相同的内容应用于另一个数组中包含的数组。多维数组的vardump() 显示以下内容:

array (size=3)
  'home' => 
    array (size=6)
      'label' => 
        object(Magento\Framework\Phrase)[5814]
          private 'text' => string 'Home' (length=4)
          private 'arguments' => 
            array (size=0)
              ...
      'title' => 
        object(Magento\Framework\Phrase)[5815]
          private 'text' => string 'Go to Home Page' (length=15)
          private 'arguments' => 
            array (size=0)
              ...
      'link' => string 'http://example.com/example.html' (length=23)
      'first' => boolean true
      'last' => null
      'readonly' => null
  'category4' => 
    array (size=6)
      'label' => string 'Transport' (length=9)
      'link' => string 'http://example.com/example.html' (length=23)
      'title' => null
      'first' => null
      'last' => null
      'readonly' => null
  'category686' => 
    array (size=6)
      'label' => string 'Transport' (length=15)
      'link' => string '' (length=0)
      'title' => null
      'first' => null
      'last' => boolean true
      'readonly' => null

在这种情况下,如何获取 category4 相对于size=3 数组的位置?

【问题讨论】:

  • 您是否尝试搜索数组?

标签: php arrays


【解决方案1】:

array_search() 将让您找到元素的位置,如果未找到该元素,则返回 FALSE。在http://php.net/manual/en/function.array-search.php了解更多信息

根据文档,这是可以从此函数返回的内容:

返回值

如果在数组中找到 needle,则返回它的键,否则返回 FALSE

这是一个例子:

<?php
$cars=array("Volvo","BMW","Toyota","Tesla","Volkswagen");
//for Volvo, an echo statement should return 1
//for BMW, an echo statement should return 2
//for Toyota, an echo statement should return 3 ... and so on ...

$volvoPosition = array_search("Volvo", $cars);

if ($volvoPosition !== false) {
  // Volvo position was found at index/position 0 of the array.
  print $volvoPosition; // This gives the value 0
} else {
  // "Volvo" was never found in the array.
}
?>

【讨论】:

  • 这不会返回 OP 请求的输出
  • 所以在通话中加 1
  • 除非返回为假或字符串
  • @asiby 但是这个函数永远不能返回字符串 - 听说过关联数组吗?
  • 好的。很抱歉当时的混乱。我应该首先添加一个示例。在这个例子中,我故意避免增加结果。因为如果他打算让你的那个结果从数组中抓取东西,那么它就行不通了。看看这个i.imgur.com/VRSkSGd.jpgLOL
【解决方案2】:

因为例子很简单,意思就是一个字符串数组,那么你可以使用array_flip,它会返回一个数组,用值来翻转键,所以我们可以这样做:

$cars = ["Volvo","BMW","Toyota","Tesla","Volkswagen"];
$flipped = array_flip($cars);
//for Volvo, => int(0)
var_dump($flipped['Volvo']);
//for BMW, => int(1)
var_dump($flipped['BMW']);

还要记住数组以0开头而不是1,那么“沃尔沃”的索引是0而不是1

【讨论】:

    【解决方案3】:

    是的,你可以使用 array_search(),但是 array_search() 返回从 0 开始的元素的索引,所以你可以像下面这样,

    这是你的数组

    $cars=array("Volvo","BMW","Toyota","Tesla","Volkswagen");
    echo (array_search("Volvo",$cars)) + 1; //you can getting 1 as output

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-02
      • 1970-01-01
      • 2018-10-16
      • 1970-01-01
      • 2020-12-20
      • 1970-01-01
      相关资源
      最近更新 更多