Viewed   94 times

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'  

 Answers

3

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);
    }
}
Saturday, October 15, 2022
1

Actually, this can be done. Through a php extension.

File: config.m4

PHP_ARG_ENABLE(test, whether to enable test Extension support, [ --enable-test   Enable test ext support])

if test "$PHP_TEST" = "yes"; then
  AC_DEFINE(HAVE_TEST, 1, [Enable TEST Extension])
  PHP_NEW_EXTENSION(test, test.c, $ext_shared)
fi

File: php_test.h

#ifndef PHP_TEST_H
#define PHP_TEST_H 1

#define PHP_TEST_EXT_VERSION "1.0"
#define PHP_TEST_EXT_EXTNAME "test"

PHP_FUNCTION(getaddress4);
PHP_FUNCTION(getaddress);

extern zend_module_entry test_module_entry;
#define phpext_test_ptr &test_module_entry

#endif

File: test.c

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "php.h"
#include "php_test.h"

ZEND_BEGIN_ARG_INFO_EX(func_args, 1, 0, 0)
ZEND_END_ARG_INFO()

static function_entry test_functions[] = {
    PHP_FE(getaddress4, func_args)
    PHP_FE(getaddress, func_args)
    {NULL, NULL, NULL}
};

zend_module_entry test_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
    STANDARD_MODULE_HEADER,
#endif
    PHP_TEST_EXT_EXTNAME,
    test_functions,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
#if ZEND_MODULE_API_NO >= 20010901
    PHP_TEST_EXT_VERSION,
#endif
    STANDARD_MODULE_PROPERTIES
};

#ifdef COMPILE_DL_TEST
ZEND_GET_MODULE(test)
#endif

PHP_FUNCTION(getaddress4)
{
    zval *var1;
    zval *var2;
    zval *var3;
    zval *var4;
    char r[500];
    if( zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "aaaa", &var1, &var2, &var3, &var4) == FAILURE ) {
      RETURN_NULL();
    }
    sprintf(r, "n%p - %p - %p - %pn%p - %p - %p - %p", var1, var2, var3, var4, Z_ARRVAL_P(var1), Z_ARRVAL_P(var2), Z_ARRVAL_P(var3), Z_ARRVAL_P(var4) );
    RETURN_STRING(r, 1);
}

PHP_FUNCTION(getaddress)
{
    zval *var;
    char r[100];
    if( zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &var) == FAILURE ) {
      RETURN_NULL();
    }
    sprintf(r, "%p", Z_ARRVAL_P(var));
    RETURN_STRING(r, 1);
}

Then all you have to do is phpize it, config it, and make it. Add a "extension=/path/to/so/file/modules/test.so" to your php.ini file. And finally, restart the web server, just in case.

<?php
  $x = array("123"=>"123");
  $w = $x;
  $y = $x;
  $z = &$x;
  var_dump(getaddress4($w,$x,$y,$z));
  var_dump(getaddress($w));
  var_dump(getaddress($x));
  var_dump(getaddress($y));
  var_dump(getaddress($z));
?>

Returns(at least for me, your memory addresses will probably be different)

string '
0x9efeb0 - 0x9effe0 - 0x9ef8c0 - 0x9efeb0
0x9efee0 - 0x9f0010 - 0x9ed790 - 0x9efee0' (length=84)

string '0x9efee0' (length=8)

string '0x9f0010' (length=8)

string '0x9ed790' (length=8)

string '0x9efee0' (length=8)

Thanks to Artefacto for pointing this out, but my original code was passing the arrays by value, so thereby was recreating arrays including the referenced-one, and giving you bad memory values. I have since changed the code to force all params to be passed by reference. This will allow references, arrays, and object, to be passed in unmolested by the php engine. $w/$z are the same thing, but $w/$x/$y are not. The old code, actually showed the reference breakage and the fact that the memory addresses would change or match when all variables were passed in vs multiple calls to the same function. This was because PHP would reuse the same memory when doing multiple calls. Comparing the results of the original function would be useless. The new code should fix this problem.

FYI - I'm using php 5.3.2.

Sunday, September 4, 2022
 
sk0x50
 
3

You can try below code to merge array. Code generates desired output required to you. I have used sample array as given by you:

<?php
    $arr1=array(
        "384"=>array("name"=>"SomeMovieName1","age"=>"12.2 hrs","IMDBLink"=>"","IMDBRating"=>"", "coverArt"=>""),
        "452"=>array("name"=>"SomeMovieName2","age"=>"15.2 hrs","IMDBLink"=>"","IMDBRating"=>"", "coverArt"=>""),
        "954"=>array("name"=>"SomeMovieName3","age"=>"4.2 hrs","IMDBLink"=>"","IMDBRating"=>"", "coverArt"=>"")
    );
    $arr2=array(
       "384" => array("IMDBLink" => "7.2", "IMDBRating" => "http://www.imdb.com/LinkToMovie1", "coverArt" => "http://www.SomeLinkToCoverArt.com/1"),
       "452" => array("IMDBLink" => "5","IMDBRating" => "http://www.imdb.com/LinkToMovie2", "coverArt" => "http://www.SomeLinkToCoverArt.com/2"),
       "954"=>array("IMDBLink" => "8","IMDBRating" => "http://www.imdb.com/LinkToMovie3", "coverArt" => "http://www.SomeLinkToCoverArt.com/3")
    );
    $arr3 = array();
    foreach($arr1 as $key=>$val)
    {
         $arr3[] = array_merge($val, $arr2[$key]);
    }
    echo "<pre>";
    print_r($arr3);
?>
Tuesday, September 13, 2022
2

This should do the trick:

$myArray = array('item1', 'item2hidden', 'item3', 'item4', 'item5hidden');
$secondaryArray = array();

foreach ($myArray as $key => $value) {
    if (strpos($value, "hidden") !== false) {
        $secondaryArray[] = $value;
        unset($myArray[$key]);
    }
}

It moves all the entries that contain "hidden" from the $myArray to $secondaryArray.

Note: It's case sensitive

Wednesday, September 7, 2022
1

Your case can be easily implemented with recursion. That will be like:

function getVariants($string, $variants)
{
    //here's about stripping 1 symbol from string's right, so 
    //may be you'll prefer to work with string functions:
    $string  = is_array($string)?$string:str_split($string);
    $symbol  = array_pop($string);
    $variant = array_key_exists($symbol, $variants)?
               array_merge([$symbol], $variants[$symbol]):
               [$symbol];
    $result  = [];
    if(!count($string))
    {
        return $variant;
    }
    foreach(getVariants($string, $variants) as $piece)
    {
        foreach($variant as $char)
        {
            $result[] = $piece.$char;
        }
    }
    return $result;
}

-see fiddle demo. How is this working? The answer is: variation of string with length N is variations of it's right symbol 'multiplied' on variations of it's part without that symbol (i.e. with length N-1). By 'multiplication' I mean Decart product of two sets and then concatenation of two parts, that are in certain pair.

Tuesday, October 11, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :