【发布时间】:2019-01-15 22:03:41
【问题描述】:
我正在尝试搜索关联数组的数组并获取满足所有搜索条件的子数组的键。
这是我的例子:
$list = [
['name' => 'this is items name',
'number' => 1,
'description' => 'this is description',
'id' => 'just some id',],
['name' => 'this is items name2',
'number' => 1,
'description' => 'this is description2',
'id' => 'just some id',],
['name' => 'this is items name3',
'number' => 1,
'description' => 'this is',
'id' => 'just some id',],
];
我想搜索“this description”并获取如下数组的键:
Array ( [0] => 0 [1] => 1 )
我试过这个:
$array_key = array_keys(array_column($list, 'description'), 'this description', false);
但它仅在搜索词与值完全匹配时才找到键。我该如何解决这个问题?
更准确地说,我如何不仅搜索列描述,还搜索整个数组?
【问题讨论】:
-
认为带有自定义搜索功能的array_filter可以帮你解决问题
-
您想要实现什么样的搜索?某种相似度百分比?
-
我认为您可以使用
strpos函数循环遍历数组和搜索。或者您可以使用array_search进行搜索,但它会搜索所有值,而不仅仅是描述。
标签: php arrays search multidimensional-array filtering