I already spent some hours figuring this one out but without any usable result. Maybe someone could help me out with this one, i want to use it inside a class so I want to build a method with as input the array and as output the rebuild array
I have this array:
Array
(
[0] => Array
(
[5] => 0-5
[8] => 0-8
[9] => 0-9
[10] => 0-10
)
[10] => Array
(
[11] => 10-11
[12] => 10-12
)
[11] => Array
(
[13] => 11-13
)
[12] => Array
(
[14] => 12-14
)
)
And this array should be transformed to this one:
Array
(
[0] => 0-5
[1] => 0-8
[2] => 0-9
[3] => Array
(
[0] => 0-10
[1] => Array
(
[0] => 10-11
[1] => Array
(
[0] => 11-13
)
[2] => 10-12
[3] => Array
(
[0] => 12-14
)
)
)
)
EDIT1: The values of the examples are also arrays en with this I want to process some actions. For example 0-9 is one action and stops but 0-10 has children. If 0-10 is finished and return true or false the next child is fired. If true 10-11 else 10-12. It is my first time on this forum sorry for any inconvenience
EDIT2: Maybe this example helps, this is the posted string in JSON. I have a couple of rules in the database wich will help to pick the right article for the job.
$postdata = '{"next":false,
"data":{"name":"patrick",
"etc":"etc",
"MotionDetection":"yes",
"nameOfTheRoom": ["Room1","Room2","Room3"],
"havePets":"yes"}';
Array
(
[0] => 0-5
[1] => 0-8
[2] => 0-9
[3] => Array
(
[0] => "yes" // MotionDetection
[1] => Array
(
[0] => "yes" // Have Pets
[1] => Array
(
[0] => "CountRecords" // Count nameOfTheRoom
[1] => "insertArticle A"
)
[2] => "No" // Have Pets
[3] => Array
(
[0] => "CountRecords" // Count nameOfTheRoom
[1] => "insertArticle B"
)
)
)
)
Try this:
function transform_arr(&$newarr, &$arr)
{
if(count($newarr) == 0)
{
if(!isset($arr[0]))
{
return;
}
$newarr = $arr[0];
}
$newarr2 = array();
$k = 0;
foreach( $newarr as $i => $value )
{
if( isset($arr[$i]) )
{
$newarr2[$k++] = $value;
$newarr2[$k++] = $arr[$i];
if(is_array($newarr2[$k - 1])) transform_arr($newarr2[$k - 1], $arr);
}
else
{
$newarr2[$k++] = $value;
}
}
$newarr = $newarr2;
}
$arr = array(
0 => array( 5=> '0-5', 8 => '0-8', 9 => '0-9', 10 => '0-10'),
10 => array( 11 => '10-11', 12 => '10-12'),
11 => array( 13 => '11-13'),
12 => array( 14 => '12-14') );
$newarr = array();
transform_arr($newarr, $arr);
var_dump($arr);
echo '<br/>';
var_dump($newarr);
Result:
[0]=> array(4) { [5]=> string(3) "0-5" [8]=> string(3) "0-8" [9]=> string(3) "0-9" [10]=> string(4) "0-10" } [10]=> array(2) { [11]=> string(5) "10-11" [12]=> string(5) "10-12" } [11]=> array(1) { [13]=> string(5) "11-13" } [12]=> array(1) { [14]=> string(5) "12-14" } } array(5) { [0]=> string(3) "0-5" [1]=> string(3) "0-8" [2]=> string(3) "0-9" [3]=> string(4) "0-10" [4]=> array(4) { [0]=> string(5) "10-11" [1]=> array(1) { [0]=> string(5) "11-13" } [2]=> string(5) "10-12" [3]=> array(1) { [0]=> string(5) "12-14" } } }