## 将数组转换为连接字符串
~~~
$arr = ['chinese'=>'PHP轻快框架','English'=>'PKFrame', 'year'=>2017];
echo Converter::arrayToJoinString($arr);
~~~
返回结果:
~~~
chinese=PHP%E8%BD%BB%E5%BF%AB%E6%A1%86%E6%9E%B6&English=PKFrame&year=2017
~~~
## 将 Object 转换成 Array
~~~
$str = '{"chinese":"PHP轻快框架","English":"PKFrame","year":2017}';
$object = json_decode($str);
var_dump($object);
echo '<Br/>';
$arr = Converter::objectToArray($object);
print_r($arr);
~~~
返回结果:
~~~
object(stdClass)#2 (3) { ["chinese"]=> string(15) "PHP轻快框架" ["English"]=> string(7) "PKFrame" ["year"]=> int(2017) }
Array ( [chinese] => PHP轻快框架 [English] => PKFrame [year] => 2017 )
~~~
## 返回输入数组中某个单一列的值
这个是解决 PHP 5.0 以下版本中不支持 array_column 函数的应用
~~~
$arr = [
['id'=>'1', 'content'=>'PHP轻快框架'],
['id'=>'2', 'content'=>'PKFrame']
];
$arr1 = Converter::arrayColumn($arr, 'content');
print_r($arr1);
echo '<br/>';
$arr2 = Converter::arrayColumn($arr, 'content', 'id');
print_r($arr2);
~~~
返回结果:
~~~
Array ( [0] => PHP轻快框架 [1] => PKFrame )
Array ( [1] => PHP轻快框架 [2] => PKFrame )
~~~
## 数组转换成可以保存的数组字符
~~~
$arr = [
['id'=>'1', 'content'=>'PHP轻快框架'],
['id'=>'2', 'content'=>'PKFrame']
];
$arr1 = Converter::arrayToSaveString($arr, 'content');
print_r($arr1);
~~~
返回结果:
~~~
<?php
return array (
0 =>
array (
'id' => '1',
'content' => 'PHP轻快框架',
),
1 =>
array (
'id' => '2',
'content' => 'PKFrame',
),
)
?>
~~~
## Xml 转 数组
包括根键,忽略空元素和属性(还存在隐藏性错误)
~~~
~~~
返回结果:
~~~
~~~
## 多维数组的递归
~~~
$list = array(
1 => array('id' => '1', 'pid' => 0, 'name' => '一级栏目一'),
2 => array('id' => '2', 'pid' => 0, 'name' => '一级栏目二'),
3 => array('id' => '3', 'pid' => 1, 'name' => '二级栏目一'),
4 => array('id' => '4', 'pid' => 1, 'name' => '二级栏目二'),
5 => array('id' => '5', 'pid' => 2, 'name' => '二级栏目三'),
6 => array('id' => '6', 'pid' => 3, 'name' => '三级栏目一'),
7 => array('id' => '7', 'pid' => 3, 'name' => '三级栏目二'));
$arr1 = Converter::recursion($list, 'id', 'pid');
// 升级版的递归函数, 判断不同级的数据结构
$arr1 = Converter::recursionPlus($list, 0, 'id', 'pid', 'id');
print_r($arr1);
~~~
返回结果:
~~~
Array
(
[0] => Array
(
[id] => 1
[pid] => 0
[name] => 一级栏目一
[childs] => Array
(
[0] => Array
(
[id] => 3
[pid] => 1
[name] => 二级栏目一
[childs] => Array
(
[0] => Array
(
[id] => 6
[pid] => 3
[name] => 三级栏目一
)
[1] => Array
(
[id] => 7
[pid] => 3
[name] => 三级栏目二
)
)
)
[1] => Array
(
[id] => 4
[pid] => 1
[name] => 二级栏目二
)
)
)
[1] => Array
(
[id] => 2
[pid] => 0
[name] => 一级栏目二
[childs] => Array
(
[0] => Array
(
[id] => 5
[pid] => 2
[name] => 二级栏目三
)
)
)
)
~~~