2016-03-21 2 views
-2

Das Array $results hat die folgenden Werte. Ich möchte den Index des Arrays $results entfernen, wenn [total_ex_tax] einen negativen Wert hat. Brauche deine Unterstützung.So entfernen Sie den Index eines Arrays für eine bestimmte Übereinstimmung in PHP

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

Antwort

0

Versuchen Sie folgendes:

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

du versuchen:

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

Ergebnis:

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

) 
0

haben es nicht überprüft. Probieren Sie es aus:

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

} 
1

Versuchen folgenden 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); 

Sie benötigen Kombination von array_filter zu verwenden und array_map

0

Kurz Lösung mit array_filter Funktion:

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