Php 从数组的开头和结尾删除指定的值
如何仅在某些值不同于N/A的元素之间显示N/A,并在所有其他情况下用null替换N/A 键的数量多种多样,每个子数组可以有约300个键 例如:Php 从数组的开头和结尾删除指定的值,php,Php,如何仅在某些值不同于N/A的元素之间显示N/A,并在所有其他情况下用null替换N/A 键的数量多种多样,每个子数组可以有约300个键 例如: Before After N/A -> null N/A -> null 10 -> 10 N/A -> N/A 20 -> 20 N/A -> null N/A -> null 你知道怎么写这个函数吗 这是我的密
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
)
试试这个:
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
}
}
}
试试这个:
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);
foreach($source\u数组为$key=>$value){
对于($i=0;$i我找到了解决方案:
/**
* 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);
}
/**
*只有在有价格的日期之间有无价格的日期时,才显示不适用。
*否则,将N/A替换为空字符串。
*@param数组$prices
*@return数组
*/
私人功能removeNaValues(价格)
{
$removeNa=函数(&$prices,$keys,$reverse=false){
$len=计数(价格);
如果($反向){
对于($i=$len-1;$i>0;$i--){
如果(!$this->prepareNaData($i,$prices,$keys)){
打破
}
}
}否则{
对于($i=0;$i<$len;$i++){
如果(!$this->prepareNaData($i,$prices,$keys)){
打破
}
}
}
};
//删除一些额外字段。。。
如果(计算($价格)){
$keys=数组\过滤器(数组\键($prices[0]),函数($key){
return!在数组中($key,['dateTimestamp','date']);
});
//用空字符串替换不适用项
$removeNa($prices,$keys);
$removeNa($prices,$keys,true);
}
返回$price;
}
/**
*@param integer$index当前价格数据索引
*@param数组$prices价目表
*@param array$keys系列列表用于准备数据
*@return int Count系列要准备
*/
私有函数prepareNaData($index,&$prices,&$keys){
foreach($key为$key=>$val){
如果(是数值($prices[$index][$val])){
未设置($key[$key]);
}否则{
$prices[$index][$val]='';
}
}
返回计数(钥匙);
}
您尝试过什么吗?是的,我添加了代码。它没有按预期工作。我只需要删除数组中每个键的第一个值之前和最后一个值之后的N/A。N/A 10 N/A 20 N/A->null 10 N/A 20 null