Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6374915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:32:51+00:00 2026-05-25T01:32:51+00:00

I have PHP array of arrays as below and I want to extract the

  • 0

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'  
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T01:32:52+00:00Added an answer on May 25, 2026 at 1:32 am

    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);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Tech used: PHP 5.3.10 Hi, I have an array (example below) I need to
I have an Array List that I want to output like my example below.
I have multiple arrays shown below. <?php $arrLayout = array( section1 => array( wXBMCLibrary
A PHP array can have arrays for its elements. And those arrays can have
I have two arrays in PHP. The first array ($author_array) is comprised of user_ids
I have an html document with multiple commented-out PHP arrays, e.g.: <!-- Array (
I have php array ($_FILE[fails]) I need to output only thous names where type
I have a PHP array which looks like this example: $array[0][0] = 'apples'; $array[0][1]
I have a PHP array, say, $array = Array(Name1,Name2,Name3,Name4,Name5); I want to find the
This is a php question. I have an array of arrays: Array ( [1]

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.