Solved

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

Posted on 2016-09-18
5
74 Views
Last Modified: 2016-10-07
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.
0
Comment
Question by:Pravin Bnakar
  • 2
  • 2
5 Comments
 
LVL 2

Expert Comment

by:manuverhaegen
ID: 41804394
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'));
0
 

Author Comment

by:Pravin Bnakar
ID: 41804505
what is this
0
 
LVL 2

Accepted Solution

by:
manuverhaegen earned 500 total points
ID: 41804532
You can searcht with this function inside a array

Building the array this is wat you do
$arr = array(0 => array(id=>1,name=>"cat 1"),
              1 => array(id=>2,name=>"cat 2"),
              2 => array(id=>3,name=>"cat 1"));

search in the array
$arr is the array
name = the field
cat 1 is the value

 print_r(search($arr, 'name', 'cat 1'));
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 41828802
@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

1
 

Author Comment

by:Pravin Bnakar
ID: 41833191
I applied same method in my application
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question