Avatar of Pravin Bnakar
Pravin Bnakar
 asked on

How to Search more than one key value in multidimensional arrays in php

Hello,
 I have below array , I want to search exact key value array form below array and store in new array
the Key are [5] and [4] and value are [S] and [Red]
that keys come dynamically whether it is one or more than two  
$variant_array  =
Array
(
    [566531271473754110] => Array
        (
            [p_id] => 7113
            [vID] => 56
            [5] => S
            [4] => Red
            [1] => 500
            [2] => 410
            [3] => 15
        )
[286531271473754110] => Array
        (
            [p_id] => 7113
            [vID] => 56
            [5] => S
            [4] => Red
            [1] => 500
            [2] => 420
            [3] => 15
        )

    [566531311473754110] => Array
        (
            [p_id] => 7113
            [vID] => 56
            [5] => M
            [4] => Blue
            [1] => 500
            [2] => 450
            [3] => 10
        )

    [566532101473754110] => Array
        (
            [p_id] => 7113
            [vID] => 56
            [5] => XS
            [4] => Red
            [1] => 500
            [2] => 450
            [3] => 25
        )

)
this is search key value
$val_exp =
Array
(
    [0] => S
    [1] => Red
)
$key_exp
Array
(
    [0] => 5
    [1] => 4
)

this is i had try,

foreach($variant_array as $keys=>$vals)
      {
            foreach($val_exp as $val_key=>$val_val)
            {
                  if($vals[$key_exp[$val_key]] == $val_val && $vals[3] > 0)
                  {
                        $available_variant[$keys] = $vals;
                  }
            }
      }
Please Solve this.
PHP

Avatar of undefined
Last Comment
Pravin Bnakar

8/22/2022 - Mon
manuverhaegen

Here is a example

function search($array, $key, $value)
{
    $results = array();

    if (is_array($array)) {
        if (isset($array[$key]) && $array[$key] == $value) {
            $results[] = $array;
        }

        foreach ($array as $subarray) {
            $results = array_merge($results, search($subarray, $key, $value));
        }
    }

    return $results;
}

$arr = array(0 => array(id=>1,name=>"cat 1"),
             1 => array(id=>2,name=>"cat 2"),
             2 => array(id=>3,name=>"cat 1"));

print_r(search($arr, 'name', 'cat 1'));
Pravin Bnakar

ASKER
what is this
ASKER CERTIFIED SOLUTION
manuverhaegen

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Ray Paseur

@Pravin Bnakar: Going forward, please use PHP var_export() to create test data for posting at E-E.  It will generate PHP code from your variables, greatly simplifying the work we need to do to give you tested and working examples.  And please use the code snippet feature when you post code here. Thanks!

Like so many things in software development, the devil is in the details.  For example, we do not know if you want to select an array when only one of the values match.  Or do both of the values have to match?  It's "little things" like this that lead to most programming problems!  But that said, this script uses your test data and shows how to select an array if either  value matches.  Hopefully this can help you get started.
https://iconoun.com/demo/temp_pravin.php
<?php // demo/temp_pravin.php
/**
 * https://www.experts-exchange.com/questions/28970703/How-to-Search-more-than-one-key-value-in-multidimensional-arrays-in-php.html
 */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('log_errors',     TRUE);


// TEST DATA FROM THE POST AT E-E
$variant_array =
[ '566531271473754110' => [ 'p_id' => 7113, 'vID' => 56, '5' => 'S',  '4' => 'Red',  '1' => 500, '2' => 410, '3' => 15 ]
, '286531271473754110' => [ 'p_id' => 7113, 'vID' => 56, '5' => 'S',  '4' => 'Red',  '1' => 500, '2' => 420, '3' => 15 ]
, '566531311473754110' => [ 'p_id' => 7113, 'vID' => 56, '5' => 'M',  '4' => 'Blue', '1' => 500, '2' => 450, '3' => 10 ]
, '566532101473754110' => [ 'p_id' => 7113, 'vID' => 56, '5' => 'XS', '4' => 'Red',  '1' => 500, '2' => 450, '3' => 25 ]
]
;

// TEST DATA FROM THE POST AT E-E "this is search key value"
$val_exp = [ 'S', 'Red' ];
$key_exp = [ 5, 4 ];

// MERGE THE SEARCH INFORMATION
$exps = array_combine($key_exp, $val_exp);

// SEARCH THE VARIANT ARRAY
$out = [];
foreach ($variant_array as $v_key => $v_arr)
{
    foreach ($exps as $e_key => $e_val)
    {
        if ($v_arr[$e_key] == $e_val)
        {
            $out[$v_key] = $v_arr;
        }
    }
}

// SHOW THE ORIGINAL DATA AND THE WORK PRODUCT
echo '<pre>';
print_r($variant_array);
print_r($out);

Open in new window

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Pravin Bnakar

ASKER
I applied same method in my application