php php获取数组的数据大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9018651/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 05:59:59  来源:igfitidea点击:

php get array's data size

phparrayssize

提问by Alex

Having this array:

有这个数组:

Array
(
    [_block1] => Array
        (
            [list] => Array
            (
                 [sub-list] => Array
                 (
                 )
            )
            [links] => Number
            [total] => Number
            ...
        )
    [_block2] => Array
        (
             [@attributes] => Array
             (
             )
             [title] => ...
             [data] => Array ()
             ...
        )
    [_block3] => Array
        (
             ..
        )
)

Those blocks contain data returned by api. Knowing that each api returns data in a different way/structure I need to measure/calculate the data/size inside of each and one of them and then do if data > Xor < do something.

这些块包含 api 返回的数据。知道每个api以不同的方式/结构返回数据,我需要测量/计算每个api内部的数据/大小,然后做if data > X或<做某事。

Is it possible? I have searched google but I only found count()and that isn't what I need to make this work.

是否可以?我已经搜索过谷歌,但我只找到了count(),这不是我完成这项工作所需要的。

Edit:Each and of the those blocks contain many other sub blocks, and I was thinking of calculating the data size in bytes, because count wont do the job here.

编辑:这些块中的每一个都包含许多其他子块,我正在考虑以字节为单位计算数据大小,因为 count 不会在这里完成这项工作。

回答by Masoud Rostami

echo  mb_strlen(serialize((array)$arr), '8bit');

回答by Davide Gualano

If I understood well your question, you need the size of each "block" subarray inside the main array.

如果我很好理解你的问题,你需要主数组中每个“块”子数组的大小。

You can do something like this:

你可以这样做:

$sizes = array();
foreach($returnedArray as $key => $content) {
    $sizes[$key] = count($content);
}

The $sizesarray will be an associative array which the various "block"s as keys and the size of the data as values.

$sizes数组将是一个关联数组,其中各种“块”作为键,数据的大小作为值。

Edit: after the edit of the question, if the data inside the innermost arrays are strings or integers you can use a function like this:

编辑:问题编辑后,如果最里面的数组中的数据是字符串或整数,你可以使用这样的函数:

function getSize($arr) {
    $tot = 0;
    foreach($arr as $a) {
        if (is_array($a)) {
            $tot += getSize($a);
        }
        if (is_string($a)) {
            $tot += strlen($a);
        }
        if (is_int($a)) {
            $tot += PHP_INT_SIZE;
        }
    }
    return $tot;
}

assuming to have only ASCII-encoded strings.

假设只有 ASCII 编码的字符串。

回答by SERPRO

Do you mean something like this?

你的意思是这样吗?

$x = 32;
foreach($blocks as $key => $block)
{
  if(getArraySize($block) < $x)
  {
     //Do Something
  }else
  {
     //Do another thing
  }
}


//Recursive function
function getArraySize($array)
{
   $size = 0;
   foreach($array as $element)
   {
      if(is_array($element))
         $size += getArraySize($element);
      else
         $size += strlen($element);
   }

   return $size;
}

回答by gargAman

To get the size in bytes you can use the below code.

要获取以字节为单位的大小,您可以使用以下代码。

$serialized = serialize($foo);
if (function_exists('mb_strlen')) {
    $size = mb_strlen($serialized, '8bit');
} else {
    $size = strlen($serialized);
}

I hope it will be helpful.

我希望它会有所帮助。