PHP 数组 - 根据值获取所有相关的数组

发布于 2024-12-01 20:28:17 字数 2670 浏览 1 评论 0原文

我有如下的 PHP 数组数组,我想根据键“type”值提取数组。我的意思是,例如在下面的代码中想要提取基于

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

为此我将在 for 循环中的每个元素并组合相关的元素。但是有没有直接的函数可以做到这一点?

像 array.search(type value 2) 这样的东西给出了相关的两个条目..? (因为我有很多这样的类型)

谢谢你的帮助

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'  

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'  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陈独秀 2024-12-08 20:28:18

您可以在函数内制定条件,如果数组元素匹配则返回 true,如果不匹配则返回 false

然后,您可以将其用作 array_filterDocs

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

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

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

根据需要修改函数。输入将是单个元素。

或者,如果您希望使用指定的约束来调用阵列上的某些接口(演示):

<?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);
   }
}

但是一个更优雅的变体是在数组上使用 FilterIterator ,它可以更好地接受参数并且更可重用(演示):

<?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);
    }
}

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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文