我有这样的数组。
$array = array ( '18' => array ( '1' =>'', '14' =>''),
'23' => array ( '24' =>''),
'24' => array ( '25' =>'', '26' =>''),
'26' => array ( '27' =>'', '28' =>''),
'30' => array ( '23' =>'', '31' =>'')
);
我希望这个阵列能够像这样重新排列。
Array
(
[18] => Array
(
[1] =>
[14] =>
)
[30] => Array
(
[23] => Array
(
[24] => Array
(
[25] =>
[26] => Array
(
[27] =>
[28] =>
)
)
)
)
)
感谢别人的帮助。
答案 0 :(得分:0)
这个脚本太近了但是在24个索引之后有一些问题
<?php
class arrayTest
{
//create the array
public $array = array ( '18' => array ( '1' => '', '14' => ''),
'23' => array ( '24' =>''),
'24' => array ( '25' =>'', '26' =>''),
'26' => array ( '27' =>'', '28' =>''),
'30' => array ('23'=>'','31'=>'')
);
public $data = array();
public function index()
{
//data array
$data =array();
//process each key of the array
foreach($this->array as $key=>$val)
{
//check the key value
if($key==18 || $key==30)
{
//process the child array
foreach($val as $key1=>$val1)
{
//bind the expectation
$data[$key][$key1] =isset($this->array[$key1])?$this->get_array($key1):'';
}
}
}
//output
print_r($data);
}
//function process the each array
function get_array($index)
{
//process the each array
foreach($this->array[$index] as $key=>$value)
{
if(isset($this->array[$key]) && is_array($this->array[$key]))
{
foreach($this->array[$key] as $key1=>$value1)
{
//
$this->data[$key] = isset($this->array[$key1])?$this->get_array($key1):'';
}
}else{
$this->data[$key] = '';
}
}
return $this->data;
}
}
$obj = new arrayTest();
$obj->index();
答案 1 :(得分:0)
<强> 2。编辑:强>
我修改了我的迭代解决方案,以便检查文本格式的结果数组的总长度。如果长度没有改变,迭代将退出。
(修改)示例数据:
$a=array('18'=> array ( '1' =>'', '14' =>''),
'20' => array ( '32' =>''),
'23' => array ( '24' =>''),
'24' => array ( '25' =>'', '26' =>''),
'26' => array ( '27' =>'', '28' =>''),
'29' => array ( '33' =>''),
'30' => array ( '23' =>'', '31' =>''),
'32' => array ( '30' =>''),
'33' => array ( '32' =>'')
);
代码:
function plough(&$a){ // ploughs throught the array once from top to bottom ...
foreach ($a as $ak => &$av){
foreach($av as $k => &$v) {
if(isset($a[$k])) $v=$a[$k];
}
}
return strlen(print_r($a,1)); // returns string-length of print_r-output
}
// iterate until there is no change in string-length any more ...
for ($al1=1+$al0=0 ;$al0<$al1; $al1=plough($a)) $al0=$al1;
print_r($a);
通过将&
放在foreach
循环中的变量名之前,您正在处理对这些变量的引用,而不是复制的值。在我的(修改过的)示例中应用例程会产生以下结果
输出:
Array
(
[18] => Array
(
[1] =>
[14] =>
)
[20] => Array
(
[32] => Array
(
[30] => Array
(
[23] => Array
(
[24] => Array
(
[25] =>
[26] => Array
(
[27] =>
[28] =>
)
)
)
[31] =>
)
)
)
[23] => Array
(
[24] => Array
(
[25] =>
[26] => Array
(
[27] =>
[28] =>
)
)
)
[24] => Array
(
[25] =>
[26] => Array
(
[27] =>
[28] =>
)
)
[26] => Array
(
[27] =>
[28] =>
)
[29] => Array
(
[33] => Array
(
[32] => Array
(
[30] => Array
(
[23] => Array
(
[24] => Array
(
[25] =>
[26] => Array
(
[27] =>
[28] =>
)
)
)
[31] =>
)
)
)
)
[30] => Array
(
[23] => Array
(
[24] => Array
(
[25] =>
[26] => Array
(
[27] =>
[28] =>
)
)
)
[31] =>
)
[32] => Array
(
[30] => Array
(
[23] => Array
(
[24] => Array
(
[25] =>
[26] => Array
(
[27] =>
[28] =>
)
)
)
[31] =>
)
)
[33] => Array
(
[32] => Array
(
[30] => Array
(
[23] => Array
(
[24] => Array
(
[25] =>
[26] => Array
(
[27] =>
[28] =>
)
)
)
[31] =>
)
)
)
)