php字符串怎么转数组对象
-
PHP中将字符串转换为数组对象可以使用一些内置函数和操作符。下面是几种常见的方法:
1. 使用explode()函数将字符串按照指定的分隔符分割成数组:
“`php
$str = “apple,banana,orange”;
$arr = explode(“,”, $str);
print_r($arr);
“`输出结果为:
“`
Array
(
[0] => apple
[1] => banana
[2] => orange
)
“`2. 使用str_split()函数将字符串分割成单个字符的数组:
“`php
$str = “hello”;
$arr = str_split($str);
print_r($arr);
“`输出结果为:
“`
Array
(
[0] => h
[1] => e
[2] => l
[3] => l
[4] => o
)
“`3. 使用preg_split()函数根据正则表达式分割字符串成数组:
“`php
$str = “apple, banana, orange”;
$arr = preg_split(“/\s*,\s*/”, $str);
print_r($arr);
“`输出结果为:
“`
Array
(
[0] => apple
[1] => banana
[2] => orange
)
“`4. 使用json_decode()函数将符合JSON格式的字符串转换为数组对象:
“`php
$jsonStr = ‘{“name”:”John”,”age”:30,”city”:”New York”}’;
$arr = json_decode($jsonStr, true);
print_r($arr);
“`输出结果为:
“`
Array
(
[name] => John
[age] => 30
[city] => New York
)
“`以上是几种常见的将字符串转换为数组对象的方法,根据具体的需求选择适合的方法进行转换。
2年前 -
在PHP中,将字符串转换为数组或对象可以使用一些内置的函数和技巧。下面是几种常用的方法:
1. 使用explode()函数将字符串按特定的分隔符拆分为数组。例如,如果字符串是由逗号分隔的,可以使用下面的代码将其转换为数组:
“`php
$str = “apple,banana,orange”;
$arr = explode(“,”, $str);
print_r($arr);
“`
输出:
“`php
Array
(
[0] => apple
[1] => banana
[2] => orange
)
“`2. 使用json_decode()函数将字符串转换为对象。如果字符串是JSON格式的,可以使用下面的代码将其转换为对象:
“`php
$str = ‘{“name”:”John”, “age”:30, “city”:”New York”}’;
$obj = json_decode($str);
print_r($obj);
“`
输出:
“`php
stdClass Object
(
[name] => John
[age] => 30
[city] => New York
)
“`3. 使用preg_split()函数根据正则表达式将字符串拆分为数组。例如,如果字符串是以空格分隔的,可以使用下面的代码将其转换为数组:
“`php
$str = “Hello world, how are you?”;
$arr = preg_split(“/\s+/”, $str);
print_r($arr);
“`
输出:
“`php
Array
(
[0] => Hello
[1] => world,
[2] => how
[3] => are
[4] => you?
)
“`4. 使用str_split()函数将字符串拆分为单个字符的数组。例如,可以使用下面的代码将字符串转换为数组:
“`php
$str = “Hello”;
$arr = str_split($str);
print_r($arr);
“`
输出:
“`php
Array
(
[0] => H
[1] => e
[2] => l
[3] => l
[4] => o
)
“`5. 使用unserialize()函数将字符串反序列化为对象或数组。如果字符串是通过serialize()函数序列化的,可以使用下面的代码将其转换回原始的对象或数组:
“`php
$str = ‘a:3:{i:0;s:5:”apple”;i:1;s:6:”banana”;i:2;s:6:”orange”;}’;
$arr = unserialize($str);
print_r($arr);
“`
输出:
“`php
Array
(
[0] => apple
[1] => banana
[2] => orange
)
“`总结:
以上是在PHP中将字符串转换为数组或对象的几种常见方法。根据实际需求和字符串的格式,选择合适的方法进行转换即可。2年前 -
1. 使用explode()函数将字符串转为数组
可以使用explode()函数将一个字符串按照指定的分隔符拆分成数组。示例代码如下:
“`php
$str = “apple,banana,orange”;
$array = explode(“,”, $str);
print_r($array);
“`输出结果为:
“`
Array
(
[0] => apple
[1] => banana
[2] => orange
)
“`2. 使用json_decode()函数将数组转为对象
可以使用json_decode()函数将一个数组转为对象。示例代码如下:
“`php
$array = array(“name” => “John”, “age” => 30, “city” => “New York”);
$obj = (object) $array;
print_r($obj);
“`输出结果为:
“`
stdClass Object
(
[name] => John
[age] => 30
[city] => New York
)
“`3. 使用反序列化函数unserialize()将字符串转为对象
可以使用unserialize()函数将一个字符串转为对象。但是需要注意的是,该方法适用于通过serialize()函数序列化的对象。示例代码如下:
“`php
$str = ‘O:8:”stdClass”:3:{s:4:”name”;s:4:”John”;s:3:”age”;i:30;s:4:”city”;s:8:”New York”;}’;
$obj = unserialize($str);
print_r($obj);
“`输出结果为:
“`
stdClass Object
(
[name] => John
[age] => 30
[city] => New York
)
“`4. 使用json_decode()函数将字符串转为关联数组
如果要将一个字符串转为关联数组,可以使用json_decode()函数,并将第二个参数设置为true。示例代码如下:
“`php
$str = ‘{“name”:”John”,”age”:30,”city”:”New York”}’;
$array = json_decode($str, true);
print_r($array);
“`输出结果为:
“`
Array
(
[name] => John
[age] => 30
[city] => New York
)
“`通过以上方法,可以将字符串转为数组对象或关联数组。根据实际需求选择合适的方法来完成转换。
2年前