Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 对计算长度数组和特殊值使用非细孔化_Php_Mysql_Arrays_String_Count - Fatal编程技术网

Php 对计算长度数组和特殊值使用非细孔化

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

我的数据以序列化格式保存在数据库中。 我想取消序列化从select查询中获取的数据,然后计算其长度,并返回数据中某个值的索引。 未序列化数据的结果如下

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