?
Solved

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

Posted on 2016-09-18
5
Medium Priority
?
88 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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 111

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

Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

777 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