首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP如何用array_search和array_column保存匹配项的密钥?

PHP如何用array_search和array_column保存匹配项的密钥?
EN

Stack Overflow用户
提问于 2016-09-12 11:52:04
回答 2查看 618关注 0票数 1

如何使用array_searcharray_column保存匹配项的密钥

代码语言:javascript
复制
$items = array(
    'meta-title' => [
        "code" => 'meta-title'
    ],

    'meta-keywords' => [
        "code" => 'meta-keywords'
    ],
);

$key = array_search('meta-title', array_column($items, 'code'));
var_dump($key); // 0

我想要的结果是:

代码语言:javascript
复制
'meta-title'

有什么想法吗?

EN

回答 2

Stack Overflow用户

发布于 2016-09-12 12:38:26

你的array_columns()调用返回一个数字索引的字符串数组(基于来自第一级的键),而不是你想要搜索的数组(即来自第二级的“代码”值的数组)。更好的做法是遍历$items,并基于正在遍历的数组进行搜索来构建数组(键/值对):

代码语言:javascript
复制
$items = array(
    'meta-title' => [
        'code' => 'meta-title'
    ],

    'meta-keywords' => [
        'code' => 'meta-keywords'
    ],
);

$results = array();
foreach ($items as $key => $value) {
    $result = array_search('meta-title', $value);
    if ($result !== false) {
        array_push($results, array($key => $result));
    }
}

http://sandbox.onlinephpfunctions.com/code/71934db55c67657f0336f84744e05097d00eda6d

票数 1
EN

Stack Overflow用户

发布于 2016-09-12 13:31:52

下面是一种面向对象的方法,它允许在运行时设置列和搜索值。作为一个类,它更具可重用性,并且具有一定的自文档化功能。

代码语言:javascript
复制
<?php
$items = array(
    'meta-title' => [
        "code" => 'meta-title'
    ],

    'meta-keywords' => [
        "code" => 'meta-keywords'
    ],
);


/** 
 * Search all records of a recordset style array for a column containing a value
 * Capture the row into matches member for later use.  
 */
class ColumnSearch {

    private $key; 
    private $search;

    public $matches=array(); 

    public function __construct( $key, $search ){
        $this->key = $key;
        $this->search = $search;
    }

    public function search( array $items ){
        // @todo validate $items is like a recordset
        $matches = array_filter( $items, array( $this, "_filter"), ARRAY_FILTER_USE_BOTH );
        $this->matches = $matches; 
        return count($matches); 
    }

    private function _filter( $row, $rowKey ){ 
        return ( $row[$this->key] == $this->search ); 
    }
}

$search = new ColumnSearch( 'code', 'meta-title' ); 

$occurances = $search->search( $items ); 

// return value indicates how many were found, in case of multiples...
echo $occurances ." ". PHP_EOL;

// the matched row will be in matches member.  
var_dump($search->matches); 

// there might be more than 1, not in your example but this is very generic code.
// grab just the keys, then get the current 
echo current( array_keys($search->matches) ) . PHP_EOL;

echo "New Search for value that doesn't exist.". PHP_EOL; 
$newSearch = new ColumnSearch( 'code', 'title' ); 
$count = $newSearch->search( $items ); 
if( 0 == $count ){
    echo "Nothing found.". PHP_EOL; 
}
echo current( array_keys( $newSearch->matches) ); 

http://sandbox.onlinephpfunctions.com/code/83b306bfc30ef2a055cf49501bdeb5cb2e5b5ed7

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39443123

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档