如何将数组的字符串表示转换为普通数组?

时间:2021-10-21 18:22:43

I have string representation of an array in a file. I need to convert it to array . How to achieve this.

我在文件中有一个数组的字符串表示。我需要把它转换成数组。如何实现这一目标。

For example

例如

$arr = 'array(1,2,3,4,5,6)'; 

echo getType($arr); // string

//convert $arr to type array

echo getType($arr); // array

2 个解决方案

#1


0  

It depends on if the file is made up entirely of valid PHP. If it is you could just include the file. Otherwise, given that this string is valid PHP you could eval the string, though typically this is viewed as bad solution to almost any problem.

这取决于文件是否完全由有效的PHP组成。如果是的话,你可以把文件包括进去。否则,由于该字符串是有效的PHP,您可以eval该字符串,尽管通常这被认为是对几乎所有问题的糟糕解决方案。

If the entire file is valid PHP and you just want the array in another script use include.

如果整个文件是有效的PHP,而您只想要在另一个脚本中使用的数组包括。

somefile.php

return array(1,2,3,4,5,6);

someotherfile.php

$arr = include 'somefile.php';
var_dump($arr);

This gives you...

这给了你…

array(6) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
  [3]=>
  int(4)
  [4]=>
  int(5)
  [5]=>
  int(6)
}

#2


0  

I don't know if the string format is fixed or not.

我不知道字符串格式是否固定。

If not you could look at serialize() and unserialize().

如果您不能查看serialize()和unserialize()。

The other approach, assuming the format is as written, would be to execute the file, through require or include, rather than read it as a string. That would create the variable in your context. There are a few little issues with this approach, such as enclosing tags, but perhaps that is resolvable in your situation.

另一种方法是,假设格式是书面的,将通过需要或包含的方式来执行文件,而不是将其作为字符串读取。这将在您的上下文中创建变量。这种方法有一些小问题,比如封装标签,但是在您的情况下可能是可以解决的。

Lastly I found this approach as well: Execute PHP code in a string. This is an improved variant of my second approach.

最后,我还发现了这种方法:在字符串中执行PHP代码。这是我第二种方法的改进版本。

#1


0  

It depends on if the file is made up entirely of valid PHP. If it is you could just include the file. Otherwise, given that this string is valid PHP you could eval the string, though typically this is viewed as bad solution to almost any problem.

这取决于文件是否完全由有效的PHP组成。如果是的话,你可以把文件包括进去。否则,由于该字符串是有效的PHP,您可以eval该字符串,尽管通常这被认为是对几乎所有问题的糟糕解决方案。

If the entire file is valid PHP and you just want the array in another script use include.

如果整个文件是有效的PHP,而您只想要在另一个脚本中使用的数组包括。

somefile.php

return array(1,2,3,4,5,6);

someotherfile.php

$arr = include 'somefile.php';
var_dump($arr);

This gives you...

这给了你…

array(6) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
  [3]=>
  int(4)
  [4]=>
  int(5)
  [5]=>
  int(6)
}

#2


0  

I don't know if the string format is fixed or not.

我不知道字符串格式是否固定。

If not you could look at serialize() and unserialize().

如果您不能查看serialize()和unserialize()。

The other approach, assuming the format is as written, would be to execute the file, through require or include, rather than read it as a string. That would create the variable in your context. There are a few little issues with this approach, such as enclosing tags, but perhaps that is resolvable in your situation.

另一种方法是,假设格式是书面的,将通过需要或包含的方式来执行文件,而不是将其作为字符串读取。这将在您的上下文中创建变量。这种方法有一些小问题,比如封装标签,但是在您的情况下可能是可以解决的。

Lastly I found this approach as well: Execute PHP code in a string. This is an improved variant of my second approach.

最后,我还发现了这种方法:在字符串中执行PHP代码。这是我第二种方法的改进版本。