简体   繁体   English

如何从多维数组中删除多列?

[英]How to remove multiple columns from the multi dimensional array?

I have a multi dimensional array like below.我有一个如下所示的多维数组。

array:1 [
  0 => array:3 [
    "picture_id" => "0"
    "car_name" => "CA"
    "from" => "2020"
    "to" => "2020"
  ]
]

I need to remove from & to from the above array and return result in same structure.我需要从上面的数组中from & to并以相同的结构返回结果。

I have tried following code我试过以下代码

    $whitelist = array("from", "to");
    $finalArray = [];
    foreach ($records as $record) {
        foreach ($record as $key => $item) {
            if (!in_array($key, $whitelist)) {
                $finalArray[$key] = $item;
            }
        }
    }

But the result I am getting is not multi-dimensional array like this:但我得到的结果不是这样的多维数组:

array:1 [
  "picture_id" => "0"
  "car_name" => "CA"
]

My expected result is:我的预期结果是:

array:1 [
  0 => array:3 [
    "picture_id" => "0"
    "car_name" => "CA"
  ]
]

You can try something like this:你可以尝试这样的事情:

$array = [
   [
    "picture_id" => "0",
    "car_name" => "CA",
    "from" => "2018",
    "to" => "2020"
  ],
       [
    "picture_id" => "1",
    "car_name" => "WA",
    "from" => "2010",
    "to" => "2019"
  ]
];

    $whitelist = array("from", "to");
    $finalArray = [];
    foreach ($array as $k =>  $record) {
        foreach ($record as $key => $item) {
            if (!in_array($key, $whitelist)) {
                $finalArray[$k][$key] = $item;
            }
        }
    }
print("<pre>".print_r($finalArray,true)."</pre>");

What will print out:将打印出什么:

Array
(
    [0] => Array
        (
            [picture_id] => 0
            [car_name] => CA
        )

    [1] => Array
        (
            [picture_id] => 1
            [car_name] => WA
        )

)

A short explanation...Basically you are looping over multi dimensional array as一个简短的解释......基本上你在多维数组上循环

foreach($array as $key => $value)

Where value is "inner" array, so in order to remove some elements from inner, and create a new multi dimensionalarray, you have to use keys, like in my example, from original array.其中 value 是“内部”数组,因此为了从内部删除一些元素并创建一个新的多维数组,您必须使用原始数组中的键,就像我的示例一样。 So you than actually make an array like:所以你比实际制作一个数组,如:

$newArray[$key] = $innerArray

And this gives you multi dimensional array.这为您提供了多维数组。 BR BR

Rather than having to go through each field to determine if it should be removed, you can usearray_diff_key() to remove all of the columns in one go.您可以使用array_diff_key()删除一个 go 中的所有列,而不必通过每个字段 go 来确定是否应该删除它。 This needs you to create a list of the fields you want to remove and for each record just get the difference between the record and the keys to remove...这需要您创建要删除的字段列表,并且对于每条记录,只需获取记录和要删除的键之间的差异...

// Keys to remove (note as key rather than value)
$remove = ["from" => 1, "to" => 1];
$finalArray = [];
foreach ($array as $record) {
    $finalArray[] = array_diff_key($record, $remove);
}
print_r($finalArray);

Try This尝试这个

$result = [];
foreach($yourArray as $key => $value){
  unset($value['from']);
  unset($value['to']);        //You can unset as many columns as you want specifically.
  $result[] = $value;
}
print_r($result);

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

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