如何在 php 中删除特定匹配的数组索引


How to remove index of an array for specific match in php

数组$results具有以下值。如果数组$results的索引为负值,我想删除[total_ex_tax]索引。需要您的支持。

Array
(
    [0] => Array
        (
            [service_number] => 1300468746
            [total_ex_tax] => 28850.439453125
            [bill_date] => 2016-03-14 00:00:00
            [l1] => HNSW
            [l2] => Contact Centre
            [l3] => 3350
            [l4] => 71603
            [l5] => 
            [l6] => OneFACS
            [levelcount] => 4
            [l1_name] => Division
            [l2_name] => District
            [l3_name] => Cost Centre
            [l4_name] => Code
            [l5_name] =>  
            [l6_name] =>  
            [function_name] => Inbound
            [type] => 1300 Service
            [features] =>  
            [application] =>  
            [name] => Unknown
            [city] => Unknown
            [username] => HNSW 1300 Inbound 
        )
    [1] => Array
        (
            [service_number] => 0297162222
            [total_ex_tax] => -214.529296875
            [bill_date] => 2016-03-07 00:00:00
            [l1] => CS
            [l2] => CS Central Office
            [l3] => 916
            [l4] => 1002
            [l5] => 
            [l6] => OneFACS
            [levelcount] => 4
            [l1_name] => Division
            [l2_name] => District
            [l3_name] => Cost Centre
            [l4_name] => Code
            [l5_name] =>  
            [l6_name] =>  
            [function_name] => Cisco VoIP
            [type] => ISDN 120
            [features] => SIP F2M
            [application] => DCAshfield_CSAS1_DP
            [name] => Ashfield CS Head Office
            [city] => Ashfield
            [username] =>  
        )
    [2] => Array
        (
            [service_number] => 1800152152
            [total_ex_tax] => 16897.69921875
            [bill_date] => 2016-03-14 00:00:00
            [l1] => HNSW
            [l2] => Contact Centre
            [l3] => 3350
            [l4] => 71600
            [l5] =>  
            [l6] => OneFACS
            [levelcount] => 4
            [l1_name] => Division
            [l2_name] => District
            [l3_name] => Cost Centre
            [l4_name] => Code
            [l5_name] =>  
            [l6_name] =>  
            [function_name] => Inbound
            [type] => 1800 Service
            [features] =>  
            [application] =>  
            [name] => Liverpool Housing
            [city] => Liverpool
            [username] =>  
        )
)

尝试以下代码:

$x = [
    ['total_ex_tax' => -214.529296875, 'id'=>1],
    ['total_ex_tax' => 14.529, 'id'=>2],
];
function custom_filter($a){
    if($a['total_ex_tax'] < 0){
        return false;
    }
    return $a;
}
$x = array_filter(array_map('custom_filter',$x));
print_r($x);

您需要结合使用array_filter和array_map

试试这个:

$resultsNew = array();
foreach($results as $result)
{
    if(isset($result['total_ex_tax']) && $result['total_ex_tax'] >= 0)
        $resultsNew[] = $result;    
}
// $resultsNew is our result with non-negative total_ex_tax values

你可以试试这个:

<?php
//supposing that this is your array
$results = array(
    [
        "service_number" => 0297162222,
        "total_ex_tax" => -214.529296875
    ],
    [
        "service_number" => 1800152152,
        "total_ex_tax" => 16897.69921875
    ]
    );
for ( $num = 0; $num < count($results); $num ++ ) {
    foreach ( $results[$num] as $key => $value ) {
        //checks value if less than 0
        if ( $value < 0 ) {
            //removes the array index from the original array
            unset($results[$num]);
            break; //exits the loop
        }
    }
}
//resets the index values from the array
$results = array_values($results);
//displays the new result
print_r($results);

结果:

Array
(
    [0] => Array
        (
            [service_number] => 1800152152
            [total_ex_tax] => 16897.69921875
        )
)

还没有检查过。试试看:

$results = //your array;
foreach($results as $key->$value)
{
    if($value['total_ex_tax']!=="" && $value['total_ex_tax']<0){
        unset($results[$key]);
   }
}

具有array_filter函数的简短解决方案:

$results = array_filter($results, function($v){
    return $v['total_ex_tax'] > 0;
});