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

127

The array$results has the following values. I want to remove the index of array$results if[total_ex_tax] has negative value. Need your support.

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] =>  
        )
)
724

Answer

Solution:

Try following code:

$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);

You need to use combination of array_filter and array_map

349

Answer

Solution:

Try this:

$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
847

Answer

Solution:

You can try this:

<?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);

Result:

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

)
523

Answer

Solution:

haven't checked it. TRY IT OUT :

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

}
108

Answer

Solution:

Short solution witharray_filter function:

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

People are also looking for solutions to the problem: php - How to use the develop branch of Zend Framework 2 over composer?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.