Solved

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

Posted on 2016-09-18
5
52 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
Comment Utility
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
Comment Utility
what is this
0
 
LVL 2

Accepted Solution

by:
manuverhaegen earned 500 total points
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
@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
Comment Utility
I applied same method in my application
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How can I do this in Pyhton? 12 66
change name file before record in server 5 24
Time difference 10 33
mysql left join sentence 7 19
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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 count occurrences of each item in an array.

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now