PHP阵列 - 获取所有相关的人基于价值 [英] PHP Arrays - Getting all the related ones based on value

查看:115
本文介绍了PHP阵列 - 获取所有相关的人基于价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有数组如下的PHP数组,我想提取基于密钥型的价值阵列。我的意思是比如在下面的code要根据提取

 '类型'=> '1'
'型'=> '2'(有两个阵列此条件)
'型'=> '22'(有两个阵列此条件)

有关这个我要去for循环中的每个元素和梳理相关的。但有任何可用的直接功能做到这一点?

某些东西一样array.search(类型值2)给相关的两个条目..? (因为我有很多类型,如本)

感谢您的帮助。

 阵列
  0 =>
    排列
      '型'=>串1
      code'=>串1
      TOTAL_COUNT'=>串200
      car_count'=>串4
  1 =>
    排列
      '型'=>串'2'
      code'=>字符串'52160'
      TOTAL_COUNT'=>字符串'100'
      car_count'=>串'2'  2 =>
    排列
      '型'=>串'2'
      code'=>字符串'26'
      TOTAL_COUNT'=>串30
      car_count'=>字符串'15'  3 =>
    排列
      '型'=>字符串'20'
      code'=>字符串'6880'
      TOTAL_COUNT'=>串4
      car_count'=>串0
  4 =>
    排列
      '型'=>字符串'21'
      code'=>字符串'256'
      TOTAL_COUNT'=>串10
      car_count'=>字符串5
  5 =>
    排列
      '型'=>串22
      code'=>字符串'20'
      TOTAL_COUNT'=>字符串'100'
      car_count'=>字符串'8'  6 =>
    排列
      '型'=>串22
      code'=>字符串'25'
      TOTAL_COUNT'=>字符串'120'
      car_count'=>串'9'


解决方案

您可以制定一个返回真正如果一个数组元素比赛和<$ C函数里面你的病情$ C>假如果没有

您再使用它作为一个回调 array_filter 文件

示例(类型必须是整数2):

 函数myMatch($元素)
{
    返回$元素['类型'] === 2;
}$值= array_filter($阵列,'myMatch');

根据您的需要修改的功能。输入将是一个单一的元素。

或者,如果你preFER阵列上的一些接口与一个指定的约束(的演示):

 &LT; PHP$阵列=阵列(
    阵列('类型'=&GT;'1'),
    阵列(类型= GT;2),
    阵列('类型'=&GT;'22'),
);$选择compareValue ='类型';
$ startsWith ='2';$阵列=新OArray($数组);$选择compareValue =功能($ V)使用($选择compareValue)
{
    回报(字符串)$ V [$选择compareValue];
};$ startsWith =功能($值)使用($ startsWith)
{
    返回0 === strpos($价值$ startsWith);
};$约束=功能($元素)使用($选择compareValue,$ startsWith)
{
    返回$ startsWith($选择compareValue($元素));
};后续代码var_dump(
    $于阵列过滤器过滤($约束)
);类OArray
{
   / **
    * @var阵列
    * /
   私人$阵列;
   公共职能__construct($数组)
   {
       $这个 - &GT;数组= $阵列;
   }
   / **
    *基于函数过滤器
    * /
   公共职能过滤器($函数)
   {
       如果(!is_callable($函数))
           抛出新InvalidArgumentException('给予无效的功能。');
       返回array_filter($这个 - &GT;数组$功能);
   }
}

但是,一个更优雅的变体将使用FilterIterator,可以采取的论点远更好,更加可复用(的演示):

 &LT; PHP$阵列=阵列(
    阵列('类型'=&GT;'1'),
    阵列(类型= GT;2),
    阵列('类型'=&GT;'22'),
);$ FILTER =新ArrayElementStartsWithFilter($数组,'型','2');后续代码var_dump($过滤器 - 过滤器过滤());类ArrayElementStartsWithFilter扩展FilterIterator
{
    私人$什么;
    与私人$;
    公共职能__construct(数组$数组$什么,$用)
    {
        $这个 - &GT;什么= $什么;
        $这个 - &gt;在= $带;
        父:: __构造(新ArrayIterator($阵列));
    }
    公共职能接受()
    {
       $元= $这个 - &GT; getInnerIterator() - GT;当前();
       返回空($元素[$这个 - &GT;什么])!
               &功放;&安培; 0 === strpos($元素[$这个 - &GT;什么],$这个 - &gt;在)
       ;
    }
    公共职能过滤器(){
        返回iterator_to_array($本);
    }
}

I have PHP array of arrays as below and I want to extract the arrays based on key "type" value. I mean for example in the below code want to extract based on

'type' => '1'
'type' => '2'  ( There are two arrays for this condition)
'type' => '22'  ( There are two arrays for this condition)

For this I am going each element in the for loop and combing the related ones . But is there any direct function available to do this ?

some thing like array.search(type value 2) giving the related two entries ..? ( because I have lot of types like this )

Thanks for your help

array
  0 => 
    array
      'type' => string '1' 
      'code' => string '1'                                                       
      'total_count' => string '200'                                              
      'car_count' => string '4'                                                  
  1 => 
    array
      'type' => string '2'                                                       
      'code' => string '52160'                                                   
      'total_count' => string '100'                                              
      'car_count' => string '2'

  2 => 
    array
      'type' => string '2'                                                      
      'code' => string '26'                                                     
      'total_count' => string '30'                                               
      'car_count' => string '15'  

  3 => 
    array
      'type' => string '20'                                                      
      'code' => string '6880'                                                    
      'total_count' => string '4'                                                
      'car_count' => string '0'                                              
  4 => 
    array
      'type' => string '21'                                                      
      'code' => string '256'                                                     
      'total_count' => string '10'                                               
      'car_count' => string '5'                                              
  5 => 
    array
      'type' => string '22'                                                      
      'code' => string '20'                                                      
      'total_count' => string '100'                                              
      'car_count' => string '8'  

  6 => 
    array
      'type' => string '22'                                                      
      'code' => string '25'                                                      
      'total_count' => string '120'                                              
      'car_count' => string '9'  

解决方案

You can formulate your condition inside a function that returns true if an array element matches and false if not.

You then use it as a callback with array_filterDocs.

Example (type must be integer 2):

function myMatch($element)
{
    return $element['type'] === 2;
}

$values = array_filter($array, 'myMatch');

Modify the function according to your needs. The input will be a single element.

Or if you prefer some interface on your array to be called with a specified constraint (Demo):

<?php

$array = array(
    array('type' => '1'),
    array('type' => '2'),
    array('type' => '22'),
);

$compareValue = 'type';
$startsWith = '2';

$array = new OArray($array);

$compareValue = function($v) use ($compareValue)
{
    return (string) $v[$compareValue];
};

$startsWith = function($value) use ($startsWith)
{
    return 0 === strpos($value, $startsWith);
};

$constraint = function($element) use ($compareValue, $startsWith)
{
    return $startsWith($compareValue($element));
};

var_dump(
    $array->filter($constraint)
);

class OArray
{
   /**
    * @var Array
    */
   private $array;
   public function __construct($array)
   {
       $this->array = $array;
   }
   /**
    * function based filter
    */
   public function filter($function)
   {
       if (!is_callable($function))
           throw new InvalidArgumentException('Invalid function given.');
       return array_filter($this->array, $function);
   }
}

But a more elegant variant would be to use a FilterIterator on the array that can take the arguments far nicer and is much more re-useable (Demo):

<?php

$array = array(
    array('type' => '1'),
    array('type' => '2'),
    array('type' => '22'),
);

$filter = new ArrayElementStartsWithFilter($array, 'type', '2');

var_dump($filter->filter());

class ArrayElementStartsWithFilter extends FilterIterator
{
    private $what;
    private $with;
    public function __construct(array $array, $what, $with)
    {
        $this->what = $what;
        $this->with = $with;
        parent::__construct(new ArrayIterator($array));
    }
    public function accept()
    {
       $element = $this->getInnerIterator()->current();
       return !empty($element[$this->what])
               && 0 === strpos($element[$this->what], $this->with)
       ;
    }
    public function filter() {
        return iterator_to_array($this);
    }
}

这篇关于PHP阵列 - 获取所有相关的人基于价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆