[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2016-09-18
5
Medium Priority
?
91 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
The viewer will learn how to dynamically set the form action using jQuery.
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

656 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