Php 对计算长度数组和特殊值使用非细孔化
我的数据以序列化格式保存在数据库中。 我想取消序列化从select查询中获取的数据,然后计算其长度,并返回数据中某个值的索引。 未序列化数据的结果如下Php 对计算长度数组和特殊值使用非细孔化,php,mysql,arrays,string,count,Php,Mysql,Arrays,String,Count,我的数据以序列化格式保存在数据库中。 我想取消序列化从select查询中获取的数据,然后计算其长度,并返回数据中某个值的索引。 未序列化数据的结果如下 Array ( [0] => [1] => 3445 [2] => [3] => 3446 [4] => [5] => 3452 [6] => [7] => 3530 [8] => 3555 ) 如何计算特殊值的数组长度和返回索引? 在我的代码中,值的长度为false number $str
Array ( [0] => [1] => 3445 [2] => [3] => 3446 [4] => [5] => 3452 [6] => [7] => 3530 [8] => 3555 )
如何计算特殊值的数组长度和返回索引?
在我的代码中,值的长度为false number
$str = $row->cur;
$str_len=unserialize($str);
print_r($str_len);
$str_length=count($str_len);
使用count($arr)
计算数组的数组长度$arr
,使用array\u search($needle,$arr)
获取在源数组($arr
)中查找的值($needle
)的索引
调用回调函数(本例中为匿名函数)以筛选并返回源数组中的所有非空值
更新:你可以玩代码我想你只是在寻找数组搜索。请参阅,谢谢,但此代码将计算空数组的计数。如何显示计数数组的结果5而不是9?@mickmackusa谢谢,re。答案是,我实际上并不满意,也没有时间重构。继续努力!
<?php
$arr = ['', 3445, '', 3446, '', 3452, '', 3530, 3555,];
$arrayLen = count($arr);
echo 'array length : ' . $arrayLen; // 9
echo PHP_EOL;
// return index of value
$needle = 3452;
$index = array_search($needle, $arr);
echo 'found value ' . $needle . ' at index : ' . $index; // found value 3452 at index 5
// Count only non empty array entries
$arrNonEmpty = array_filter($arr, function($element) { return !empty($element); });
$count = count($arrNonEmpty);