如果键值不存在,则从数组中删除数组

时间:2021-07-24 02:19:52

标签: php

我有一个数组中的数组。如何搜索这些数组中的任何一个是否具有特定的键和值?如果没有从数组中删除这个数组。

示例:

array:3 [▼
  0 => array:2 [▼
    "location" => array:4 [▶]
    "promotions" => array:1 [▶]
  ]
  1 => array:2 [▼
    "city" => array:4 [▶]
    "promotions" => array:2 [▼
      0 => array:5 [▶]
      1 => array:5 [▼
        "distance" => "0.2288511878121104"
        "promoid" => 54
        "promo" => Promotion {#1259 ▶}
        "product" => Products {#1162 ▶}
        "productID" => 5
      ]
    ]
  ]
  2 => array:2 [▼
    "city" => array:4 [▶]
    "promotions" => []
  ]
]

我想搜索值为 5 的“productID”,并且我想删除促销中没有此值的数组。


已解决

    foreach ($locations as $key => $location) {
        foreach ($location['promotions'] as $item => $promotions) {
            if (is_array($promotions)) {
                foreach ($promotions as $k => $value) {
                    if (!is_array($value)) {
                        if ($k == 'productID' && $value != $id) {
                            unset($locations[$key]['promotions'][$item]);

                        }
                    }
                }
            }
        }
    }

2 个答案:

答案 0 :(得分:2)

您可以使用递归函数并unset() 目标

<?php
// example code

$a = [
    'test' => 'foo',
    'bar' => [
        'productID' => 5,
        'something' => 'else'
        ],
        'bar2' => [
        'productID' => 6,
        'something2' => 'else'
        ]
    ];
    
    function removeItem($array, $search) {
        $s = explode(":",$search);
        $skey = trim($s[0]);
        $sval = trim($s[1]);
        foreach ($array as $n => $v) {
            if ($n == $skey && $v == $sval) {
                unset($array[$n]);
            } else {
                if (is_array($v)) $v = removeItem($v, $search);
                $array[$n]  = $v;
            }
        }
        return $array;
    
    }
    $a = removeItem($a, 'productID:5');
    print_r($a);
    

示例:https://www.tehplayground.com/zJ2bKplP1pDaV8Ss

答案 1 :(得分:1)

很好的解决方案,您可以跳过 3er 循环,检查键是否设置为 isset()

foreach ($arr as $key => $location) {
    if (!is_array($location)) { //if child is not an array ignore it
        continue;
    }

    foreach ($location as $item => $promotions) {
        if (!is_array($location)) {//if child is not an array ignore it
            continue;
        }

        if (
            isset($promotions['productID']) && //3er lvl, Has the key?
            $promotions['productID'] == $id //Has the id
        ) {
            continue; //This is a match so, ignore it
        } else {
            unset($arr[$key][$item]); //This promotion doesn't have it.
        }
    }
}

而这个检查所有级别,它使用递归 fn

$ans = deepClean($arr, 'productID', 5);
function deepClean($arr, $search_key, $search_value): ?array
{
    if (
        isset($arr[$search_key]) &&  //Has the key
        $arr[$search_key] == $search_value  //Match value
    ) {
        return $arr;
    }

    //check children
    $currentLevel = [];
    foreach ($arr as $key => $value) {
        if (!is_array($value)) {
            continue;
        }

        $deepArr = deepClean($value, $search_key, $search_value);
        if (is_array($deepArr)) { //child has search?
            $currentLevel[$key] = $deepArr;
        }
    }

    return $currentLevel === [] ? null : $currentLevel;
}