如何和getimagesize()结果转换为变量? [英] How to convert getimagesize() result to variables?

查看:195
本文介绍了如何和getimagesize()结果转换为变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有给我回的所有结果的函数和getimagesize()作为关联数组或自我解释的名称单变量?我知道,我可以用一个foreach循环做,但必须有一个更好的办法。

Is there a function that gives me back all results of getimagesize() as associative array or single variables with self-explaining names? I know that I could do it with a foreach loop, but there must be a better way.

的var_dump(和getimagesize('foo.png'))的结果;

array
  0 => int 500
  1 => int 250
  2 => int 3
  3 => string 'width="500" height="250"' (length=24)
  'bits' => int 8
  'mime' => string 'image/png' (length=9)

的list()

列表 ,因为它的行为不工作:

list()

Function list does not work because of its behavior:

注:列表()只适用于数字数组并假定数字
  指数从0开始。

Note: list() only works on numerical arrays and assumes the numerical indices start at 0.

提取,如果您使用preFIX 可以工作

请注意,如果flags是EXTR_ preFIX_SAME是preFIX时才需要,
  EXTR_ preFIX_ALL,EXTR_ preFIX_INVALID或EXTR_ preFIX_IF_EXISTS。如果
  prefixed结果不是合法的变量名,将不会导入到
  符号表。 prefixes从阵列自动分离
  一个下划线字符键。

Note that prefix is only required if flags is EXTR_PREFIX_SAME, EXTR_PREFIX_ALL, EXTR_PREFIX_INVALID or EXTR_PREFIX_IF_EXISTS. If the prefixed result is not a valid variable name, it is not imported into the symbol table. Prefixes are automatically separated from the array key by an underscore character.

但后来你会喜欢&LT变量,preFIX> _0 < preFIX> _1 等。这不是自我解释。

But then you would have variables like <prefix>_0 and <prefix>_1 and so on. That is not self-explaining.

推荐答案

只有我找到的方法是使用的 array_values​​() 获得纯数字索引数组和的 的list()

Only way I found is to use array_values() to get a pure numerical indexed array and use it with list().

解决方案单一变量:

list(
      $width, 
      $height, 
      $mimeType, 
      $htmlAttr, 
      $bits, 
      $mime
) = array_values(getimagesize('foo.png'));

解决方案关联数组:

list(
     $imgInfo['width'], 
     $imgInfo['height'], 
     $imgInfo['mimeType'], 
     $imgInfo['htmlAttr'], 
     $imgInfo['bits'], 
     $imgInfo['mime']
) = array_values(getimagesize('foo.png'));

这篇关于如何和getimagesize()结果转换为变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆