【发布时间】: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 数组的位置?
【问题讨论】:
-
您是否尝试搜索数组?