简体   繁体   English

如何在php多维中搜索键值并仅使用搜索值返回数组

[英]how to search for key value in php multidimentional and return array with only search values

I have the following array 我有以下数组

Array
(
    [0] => Array
        (
            [program] => 615
            [program_title_override] => 
            [campuses] => Array
                (
                    [0] => Array
                        (
                            [campus] => 572
                            [campus_title_override] => New Jersey - Moorestown
                        )

                )

        )

    [1] => Array
        (
            [program] => 313
            [program_title_override] => 
            [campuses] => Array
                (
                    [0] => Array
                        (
                            [campus] => 267
                            [campus_title_override] => Colorado - Denver
                        )

                    [1] => Array
                        (
                            [campus] => 269
                            [campus_title_override] => Connecticut - East Windsor
                        )

                    [2] => Array
                        (
                            [campus] => 277
                            [campus_title_override] => Illinois - Melrose Park (Chicago Area)
                        )

                    [3] => Array
                        (
                            [campus] => 279
                            [campus_title_override] => Indiana - Indianapolis
                        )

                    [4] => Array
                        (
                            [campus] => 281
                            [campus_title_override] => Maryland - Columbia
                        )

                    [5] => Array
                        (
                            [campus] => 570
                            [campus_title_override] => New Jersey  - Mahwah (Bergen County/NY Metro Area)
                        )

                    [6] => Array
                        (
                            [campus] => 580
                            [campus_title_override] => New Jersey - Union
                        )

                    [7] => Array
                        (
                            [campus] => 576
                            [campus_title_override] => New York - Queens
                        )

                    [8] => Array
                        (
                            [campus] => 574
                            [campus_title_override] => Pennsylvania - Philadelphia
                        )

                    [9] => Array
                        (
                            [campus] => 560
                            [campus_title_override] => Tennessee - Nashville
                        )

                    [10] => Array
                        (
                            [campus] => 557
                            [campus_title_override] => Texas - Grand Prairie (Dallas/Fort Worth Metroplex)
                        )

                )

        )

    [2] => Array
        (
            [program] => 617
            [program_title_override] => 
            [campuses] => Array
                (
                    [0] => Array
                        (
                            [campus] => 269
                            [campus_title_override] => Connecticut - East Windsor
                        )

                    [1] => Array
                        (
                            [campus] => 279
                            [campus_title_override] => Indiana - Indianapolis
                        )

                    [2] => Array
                        (
                            [campus] => 570
                            [campus_title_override] => New Jersey  - Mahwah (Bergen County/NY Metro Area)
                        )

                    [3] => Array
                        (
                            [campus] => 557
                            [campus_title_override] => Texas - Grand Prairie (Dallas/Fort Worth Metroplex)
                        )

                )

        )

    [3] => Array
        (
            [program] => 582
            [program_title_override] => 
            [campuses] => Array
                (
                    [0] => Array
                        (
                            [campus] => 267
                            [campus_title_override] => Colorado - Denver
                        )

                    [1] => Array
                        (
                            [campus] => 269
                            [campus_title_override] => Connecticut - East Windsor
                        )

                    [2] => Array
                        (
                            [campus] => 277
                            [campus_title_override] => Illinois - Melrose Park (Chicago Area)
                        )

                    [3] => Array
                        (
                            [campus] => 279
                            [campus_title_override] => Indiana - Indianapolis
                        )

                    [4] => Array
                        (
                            [campus] => 560
                            [campus_title_override] => Tennessee - Nashville
                        )

                    [5] => Array
                        (
                            [campus] => 557
                            [campus_title_override] => Texas - Grand Prairie
                        )

                )

        )

in the campus_title_override key if the value matches "New Jersey" I want to return the array result matching new jersey array key. 如果值与“ New Jersey”匹配,请在campus_title_override键中返回与新泽西球衣数组键匹配的数组结果。 In the current array, in the array 0, 1, and 2 it has the value "New Jersey" in the campus_title_override key. 在当前数组中,在array 0, 1, and 2它的campus_title_override键中的值为"New Jersey" I want to only return array ids 0,1 and 2 . 我只想返回数组ids 0,1 and 2 Whatever matches the key value to "New Jersey" I want to return the main array result set. 无论键值与“ New Jersey”匹配如何,我都想返回主数组结果集。

I have tried the following example from stackoverflow 我已经尝试了来自stackoverflow的以下示例

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;
}


print_r(search($array, 'campus_title_override', 'New Jersey'));

and it returns empty result set. 并返回空结果集。

I also have question on how I'd do the same on this array set: 我也对如何在此数组集上执行相同操作有疑问:

       Array
(
    [0] => Array
        (
            [campus] => 269
            [campus_title_override] => Connecticut - East Windsor
            [programs] => Array
                (
                    [0] => Array
                        (
                            [program] => 313
                            [program_title_override] => 
                        )

                    [1] => Array
                        (
                            [program] => 582
                            [program_title_override] => 
                        )

                    [2] => Array
                        (
                            [program] => 617
                            [program_title_override] => 
                        )

                    [3] => Array
                        (
                            [program] => 584
                            [program_title_override] => 
                        )

                    [4] => Array
                        (
                            [program] => 619
                            [program_title_override] => 
                        )

                    [5] => Array
                        (
                            [program] => 621
                            [program_title_override] => 
                        )

                    [6] => Array
                        (
                            [program] => 625
                            [program_title_override] => 
                        )

                )

        )

    [1] => Array
        (
            [campus] => 271
            [campus_title_override] => Connecticut - New Britain
            [programs] => Array
                (
                    [0] => Array
                        (
                            [program] => 619
                            [program_title_override] => 
                        )

                    [1] => Array
                        (
                            [program] => 621
                            [program_title_override] => 
                        )

                    [2] => Array
                        (
                            [program] => 599
                            [program_title_override] => 
                        )

                    [3] => Array
                        (
                            [program] => 607
                            [program_title_override] => 
                        )

                )

        )

    [2] => Array
        (
            [campus] => 273
            [campus_title_override] => Connecticut - Shelton
            [programs] => Array
                (
                    [0] => Array
                        (
                            [program] => 619
                            [program_title_override] => 
                        )

                    [1] => Array
                        (
                            [program] => 599
                            [program_title_override] => 
                        )

                    [2] => Array
                        (
                            [program] => 607
                            [program_title_override] => 
                        )

                    [3] => Array
                        (
                            [program] => 609
                            [program_title_override] => 
                        )

                )

        )

    [3] => Array
        (
            [campus] => 283
            [campus_title_override] => Massachusetts - Somerville (Boston Area)
            [programs] => Array
                (
                    [0] => Array
                        (
                            [program] => 595
                            [program_title_override] => 
                        )

                    [1] => Array
                        (
                            [program] => 599
                            [program_title_override] => 
                        )

                    [2] => Array
                        (
                            [program] => 605
                            [program_title_override] => 
                        )

                )

        )

    [4] => Array
        (
            [campus] => 564
            [campus_title_override] => New Jersey - Iselin (Formerly located in Edison, NJ)
            [programs] => Array
                (
                    [0] => Array
                        (
                            [program] => 611
                            [program_title_override] => 
                        )

                    [1] => Array
                        (
                            [program] => 599
                            [program_title_override] => 
                        )

                    [2] => Array
                        (
                            [program] => 607
                            [program_title_override] => 
                        )

                )

        )

    [5] => Array
        (
            [campus] => 570
            [campus_title_override] => New Jersey - Mahwah (Bergen County/NY Metro Area)
            [programs] => Array
                (
                    [0] => Array
                        (
                            [program] => 313
                            [program_title_override] => 
                        )

                    [1] => Array
                        (
                            [program] => 617
                            [program_title_override] => 
                        )

                    [2] => Array
                        (
                            [program] => 619
                            [program_title_override] => 
                        )

                    [3] => Array
                        (
                            [program] => 621
                            [program_title_override] => 
                        )

                )

        )

Simply loop trough the array using a foreach loop 只需使用foreach循环遍历数组

For every element loop trough it's campuses property and check if the conditions you want are found. 对于每个元素循环槽,它都是campuses属性,并检查是否找到了所需的条件。

If the conditions are met, insert the main id into an array and break the 2nd for loop. 如果满足条件,则将主ID插入数组并中断第二个for循环。

Example: 例:

$ids = [];

$array = [
    [
        'campuses' => [
            [
                'campus_title_override' => 'a'
            ]
        ]
    ]
];

foreach ($array as $id => $obj)
{
    foreach ($obj['campuses'] as $campus)
    {
        if ($campus['campus_title_override'] == 'a')
        {
            array_push($ids, $id);
            break;
        }
    }
}

var_dump($ids);

You can learn more about Multi Demensional Arrays here and here . 您可以在此处此处了解有关多维阵列的更多信息。

Please read the articles to learn about what this code does, and don't just copy it ;) 请阅读文章以了解此代码的作用,而不仅仅是复制它;)

You can use 2 foreach (1 for elements, 1 for the campuses) and strpos() to search if the value of campus_title_override contains the value of the search. 您可以使用2个foreach (1个元素,1个校园)和strpos()来搜索campus_title_override的值campus_title_override包含搜索的值。

$array = array(/*your data*/); 
$search = "New Jersey";
$out = [];
// for each element in the array,
foreach ($array as $index => $item) {
  // for each campuses,
  foreach ($item['campuses'] as $campus) {
    // if 'campus_title_override' contains search,
    if (strpos($campus['campus_title_override'], $search) !== false) {
      $out[] = $item ;
      break; // no need to continue, we got the item.
    }
  }
}
print_r($out);

Outputs: ( https://3v4l.org/YRCma ) 输出:( https://3v4l.org/YRCma

Array
(
    [0] => Array
        (
            [program] => 615
            [program_title_override] => 
            [campuses] => Array
                (
                    [0] => Array
                        (
                            [campus] => 572
                            [campus_title_override] => New Jersey - Moorestown
                        )

                )

        )

    [1] => Array
        (
            [program] => 313
            [program_title_override] => 
            [campuses] => Array
                (
                    [0] => Array
                        (
                            [campus] => 267
                            [campus_title_override] => Colorado - Denver
                        )

                    [1] => Array
                        (
                            [campus] => 269
                            [campus_title_override] => Connecticut - East Windsor
                        )

                    [2] => Array
                        (
                            [campus] => 277
                            [campus_title_override] => Illinois - Melrose Park (Chicago Area)
                        )

                    [3] => Array
                        (
                            [campus] => 279
                            [campus_title_override] => Indiana - Indianapolis
                        )

                    [4] => Array
                        (
                            [campus] => 281
                            [campus_title_override] => Maryland - Columbia
                        )

                    [5] => Array
                        (
                            [campus] => 570
                            [campus_title_override] => New Jersey  - Mahwah (Bergen County/NY Metro Area)
                        )

For array with a depth level 2 ie 对于深度为2的数组,即

array(
[0]   =>  array(
               [key]   => 'value'
          )
)

You can use array_search but it will not work for your array. 您可以使用array_search,但不适用于您的阵列。 To search you will have to manually search using foreach loop. 要搜索,您将必须使用foreach循环手动搜索。

$mainArray = array(
        'compression' => array('7z', 'cbr', 'deb', 'gz', 'pkg', 'rar', 'rpm', 'sitx', 'tar.gz', 'zip', 'zipx'),
        'scripts' => array('php', 'js', 'css', 'asp', 'aspx', 'htm', 'html', 'cc', 'cpp', 'py', 'jsp'),
        'documentType' => array('txt', 'doc', 'docx', 'log', 'rtf'),
    );
    foreach($mainArray as $key=>$value){
        if (in_array(strtolower('php'), $value)){ // Return only if found during the loop
           return $key;
        }
    }

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM