I have a problem When I want To rebuild my array . this is I am create on phpfiddle.org
http://phpfiddle.org/main/code/tzp-t1u
anyone can give me suggestion ?
I have a problem When I want To rebuild my array . this is I am create on phpfiddle.org
http://phpfiddle.org/main/code/tzp-t1u
anyone can give me suggestion ?
This code will help you
<?php
$result = array (
array(
'lev1'=> null,
'lev2'=> 34,
'lev3'=> 21,
'lev4'=> 22,
'child' => 'cyaiooo'
),
array(
'lev1'=> null,
'lev2'=> 34,
'lev3'=> 10,
'lev4'=> 8,
'child' => 'test1'
),
array(
'lev1'=> null,
'lev2'=> 34,
'lev3'=> 21,
'lev4'=> 22,
'child' => 'hodem'
)
);
$data = array();
foreach($result as $value)
{
if(count($data)==0)
{
$data[] = $value;
}else{
foreach($data as &$container)
{
if($value['lev1']==$container['lev1']
&&
$value['lev2']==$container['lev2']
&&
$value['lev3']==$container['lev3']
&&
$value['lev4']==$container['lev4'])
{
if(is_string($container['child']))
{
//get the string value
$string = $container['child'];
//unset the child string
unset($container['child']);
//declatre the array
$container['child'] = array();
$container['child'][] = $string;
}
$container['child'][] = $value['child'];
break;
}else{
$data[] = $value;
break;
}
}
}
}
print_r($data);