清除数组中的空值,并重新排列数组键值

本文介绍了如何使用PHP中的array_filter()函数来过滤数组中的特定元素,并通过array_values()函数来重新索引过滤后的数组键名。提供了具体的代码示例,展示了如何有效管理数组数据。
//array_filter()函数的功能是利用回调函数来对数组进行过滤,如果没有回调函数,那么默认就是删除数组中值为false的项目。
例如:
$FileArray=array(0=>'1',1=>'',2=>'',3=>'go');
var_dump(array_file($FileArray))
输出结果为:
array(size=2)
    0 => string '1' (length=1)
    3 => string 'go' (length=2)
这样就引申出来一个新问题,如果我想对array_filter()处理过的新数组的键名序列化成0,1,2,3这样的怎么办呢?
这在数组比较中是很有用的,这里有两个函数可以选择array_values(),sort(). 
 
array_values()返回数组中所有的值,并给其建立数字索引。(注意,用这个之后key就是数字索引了)
 
sort()对数组进行排序。当函数结束时数组单元将被从最低到最高重新安排。成功返回true (注意: 本函数为 array 中的单元赋予新的键名。这将删除原有的键名而不仅是重新排序。 )*sort函数返回值是布尔类型。
 

这里我选择了array_values()函数来处理:

var_dump(array_values(array_filter($FileArray)));
输出结果为:
array(size=2)
    0 => string '1' (length=1)
    1=> string 'go' (length=2)
将上述代码封装成类,要求可以传入字典数据类型,其中字典的键值对分别对应数据名(数据线的名字)和数据数组(数据图像);要求可以保存数据为.csv文件类型,保存时询问保存路径和文件名。 %% 收集所有需要写入的数据 dataMap = containers.Map(); dataMap('Comper1MHz_PeakValue') = Comper1MHz_Point_PeakValue_col; dataMap('Comper1MHz_hiddenPolarity') = Comper1MHz_Point_hiddenPolarity_col; dataMap('Comper1MHz_TimeCount') = Comper1MHz_Point_TimeCount_col; dataMap('Comper1MHz_FifoSum') = Comper1MHz_Point_FifoSum_col; dataMap('Comper1MHz_TSD_ShakeCnt') = Comper1MHz_Point_TSD_ShakeCnt_col; dataMap('Comper1MHz_CurrentTouchSt') = Comper1MHz_Point_CurrentTouchSt_col; dataMap('Comper1MHz_PurrentTouchSt') = Comper1MHz_Point_PurrentTouchSt_col; dataMap('Comper13MHz_PeakValue') = Comper13MHz_Point_PeakValue_col; dataMap('Comper13MHz_hiddenPolarity') = Comper13MHz_Point_hiddenPolarity_col; dataMap('Comper13MHz_TimeCount') = Comper13MHz_Point_TimeCount_col; dataMap('Comper13MHz_FifoSum') = Comper13MHz_Point_FifoSum_col; dataMap('Comper13MHz_TSD_ShakeCnt') = Comper13MHz_Point_TSD_ShakeCnt_col; dataMap('Comper13MHz_CurrentTouchSt') = Comper13MHz_Point_CurrentTouchSt_col; dataMap('Comper13MHz_PurrentTouchSt') = Comper13MHz_Point_PurrentTouchSt_col; function writeColumnToCSV(filename, data, columnIndex) % WRITECOLUMNTOCSV 将数组写入CSV文件的指定列,支持自定义路径 % 参数: % filename - 字符串,目标CSV文件完整路径(例如 'C:\data\results.csv') % data - 数值数组、字符串数组或字符元胞数组 % columnIndex - 正整数,指定数据写入的列号(从1开始) % % 新增功能: % 1. 支持完整文件路径输入 % 2. 自动创建缺失的目录 % =============== 参数验证 =============== try if ~ischar(filename) || isempty(filename) error('文件名必须是非空字符串'); end % 检查路径合法性 [filepath, ~, ext] = fileparts(filename); if isempty(ext) error('文件名必须包含扩展名(如 .csv)'); end if ~isnumeric(columnIndex) || columnIndex < 1 || mod(columnIndex,1) ~= 0 error('列号必须是大于0的整数'); end if ~isnumeric(data) && ~iscellstr(data) && ~isstring(data) error('数据必须是数值数组、字符串数组或字符元胞数组'); end data = data(:); numRows = length(data); catch ME fprintf('参数错误: %s\n', ME.message); rethrow(ME); end % =============== 目录创建 =============== % 新增功能:自动创建缺失目录 if ~isempty(filepath) && ~isfolder(filepath) try mkdir(filepath); fprintf('创建目录: %s\n', filepath); catch ME error('目录创建失败: %s', ME.message); end end % =============== 文件处理 =============== try % 读取现有文件或初始化 if exist(filename, 'file') == 2 fileCell = readcell(filename); % 将missing类型转换为空字符串 for i = 1:numel(fileCell) try if ismissing(fileCell{i}) fileCell{i} = ''; end catch continue; % 跳过不支持ismissing的类型 end end else fileCell = {}; end [currentRows, currentCols] = size(fileCell); % 扩展列数 if columnIndex > currentCols fileCell(:, end+1:columnIndex) = {''}; [currentRows, currentCols] = size(fileCell); end % 扩展行数 if numRows > currentRows fileCell(end+1:numRows, 1:currentCols) = {''}; end % 数据转换 if isnumeric(data) dataCell = num2cell(data); for i = 1:numel(dataCell) if isnumeric(dataCell{i}) && isnan(dataCell{i}) dataCell{i} = ''; end end elseif isstring(data) dataCell = cellstr(data); else dataCell = data; end % 数据写入 fileCell(1:numRows, columnIndex) = dataCell; writecell(fileCell, filename); fprintf('成功写入 %d 行数据到 %s 的第 %d 列\n', ... numRows, filename, columnIndex); catch ME fprintf('文件操作错误: %s\n', ME.message); rethrow(ME); end end 同时修改writeColumnToCSV函数,使其可以传入字典,且按照字典中的顺序和数量自动写入文件中
最新发布
10-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值