从数组的开头和结尾删除指定的值

时间:2015-09-14 09:27:47

标签: php

如何仅在具有与N / A不同的值的元素之间显示N / A,并在所有其他情况下将N / A替换为null?

键的数量是多种多样的,每个子阵列可以有~300个键。

示例:

Before     After
N/A   ->   null
N/A   ->   null     
10    ->   10
N/A   ->   N/A
20    ->   20
N/A   ->   null      
N/A   ->   null    

任何想法如何编写可以做到这一点的功能?

这是我的代码:

    $dataCount = count($data) - 1;
    $nextNotEmpty = null;

    foreach ($data as $k => $element) {
        $next = isset($data[$k + 1]) ? $data[$k + 1] : null;
        if ($next) {
            if ($nextNotEmpty) {
                foreach ($nextNotEmpty as $id => $val) {
                    if ($val === 'N/A') {
                        $element[$k][$id] = '';
                    }
                }
            } else {
                $nextNotEmpty = $next;
            }
        }

        if ($k === 0 || $k === $dataCount) {
            foreach ($element as $key => $value) {
                if ($value === 'N/A') {
                    $data[$k][$key] = '';
                }
            }
        }
    }

//source array:
Array
(
    [0] => Array
        (
            [key1] => N/A
            [key2] => 20
            [key3] => N/A
        )

    [1] => Array
        (
            [key1] => 10
            [key2] => 30
            [key3] => N/A
        )

    [2] => Array
        (
            [key1] => N/A
            [key2] => 40
            [key3] => N/A
        )

    [3] => Array
        (
            [key1] => 30
            [key2] => N/A
            [key3] => N/A
        )

    [4] => Array
        (
            [key1] => N/A
            [key2] => N/A
            [key3] => N/A
        )
//desired output array:
Array
(
    [0] => Array
        (
            [key1] => null
            [key2] => 20
            [key3] => null
        )

    [1] => Array
        (
            [key1] => 10
            [key2] => 30
            [key3] => null
        )

    [2] => Array
        (
            [key1] => N/A
            [key2] => 40
            [key3] => null
        )

    [3] => Array
        (
            [key1] => 30
            [key2] => null
            [key3] => null
        )

    [4] => Array
        (
            [key1] => null
            [key2] => null
            [key3] => null
        )

3 个答案:

答案 0 :(得分:0)

试试这个:

foreach($array as $key => $value){
    $temp = $value;
    // call function 
    replaceByNull($temp);
    // reverse array
    $temp = array_reverse($temp);
    // call function twice
    replaceByNull($temp);
    // reverse twice to return at initial order
    $array[$key] = array_reverse($temp);
}
function replaceByNull(&$array){
      $flag = true;
      foreach($array as $k => $v){
           if($v == 'N/A' && $flag != false){
             $array[$k] = null; // REPLACE N/A by null :)
          } else if($v != 'N/A' && $v != null){
             $flag = false; // Flag to stop replacing , number found
          }
      }

}

答案 1 :(得分:0)

试试这个:

foreach($source_array as $key=>$value){
   for($i=0;$i<3:$i++){
     if($value[$i]=='N/A'){
         $source_array[$key]$value[$i]=NULL;
     }
   }
}
print_r($source_array);

答案 2 :(得分:0)

我找到了解决方案:

/**
 * Show N/A only in case when there are dates without prices between dates that have prices.
 * Otherwise replace N/A with empty string.
 * @param array $prices
 * @return array
 */
private function removeNaValues($prices)
{
    $removeNa = function(&$prices, $keys, $reverse = false) {
        $len = count($prices);

        if ($reverse) {
            for ($i = $len - 1; $i > 0; $i--) {
                if (!$this->prepareNaData($i, $prices, $keys)) {
                    break;
                }
            }
        } else {
            for ($i = 0; $i < $len; $i++) {
                if (!$this->prepareNaData($i, $prices, $keys)) {
                    break;
                }
            }
        }
    };

    // remove some extra fields...
    if (count($prices)) {
        $keys = array_filter(array_keys($prices[0]), function($key) {
            return !in_array($key, ['dateTimestamp', 'date']);
        });
        // replace N/A's with empty string
        $removeNa($prices, $keys);
        $removeNa($prices, $keys, true);
    }

    return $prices;
}

/**
 * @param integer $index Current prices data index
 * @param array $prices Prices list
 * @param array $keys Series list to prepare data
 * @return int Count series to prepare
 */
private function prepareNaData($index, &$prices, &$keys) {
    foreach ($keys as $key => $val) {
        if (is_numeric($prices[$index][$val])) {
            unset($keys[$key]);
        } else {
            $prices[$index][$val] = '';
        }
    }

    return count($keys);
}