-1

I want to find a workaround for the following problem:

I have n vectors(unique), like the following: ("val1", "val2", "val3", ..., "valn" ). Each vector's length is different.

I want to add any of those in a new array, but using the vector values(val1,val2,val3) elements as sub-elements recursively for the new array, taken from the main vector(val1 => val+1 => val+2 => val+3 => ... val+n => solution), and the last element of the vector is an integer or a string(not a sub-array/vector as the others), which will match with the last element of the new array, and it's new array's soluton/target.

The workaround solution I am applying right now is this: Let's suppose the target(solution) is the end value of the array(an integer or string). In this case I suppose to work on a vector with 4 elements, where the last one is the solution.

$vector = array("val1", "val2", "val3", "target");
$count = count($vector);
$new_array = array();
switch($count){
case 1:
....
case 4:
$new_array[$vector[0]][$vector[1]][$vector[2]] = $vector[3];
/*New array will be
$new_array = [
val1 => 
  val2 => 
    val3 => "target"
];
*/
break;
}

The vectors I am using are many and with different sizes, so the solution/target can be in the 1st element, second, third and so on, so I applied in my switch any cases from 0 to 5 for example, working as wrote above.

I think there could be a better solution, to loop inside a for(or better, a while) cycle But I am currently having no ideas on how it should be, and I didn't find any workaround in the web.

Does anyone have a soluton for this? Thanks in advance

2 Answers2

0

Hi
You can change your code like it:

<?php

$vector = array("val1", "val2", "val3", "val4", "val5", "target");
$count = count($vector);
$new_array = array();

$new_array[$vector[$count - 2]] = $vector[$count - 1];

for ($i=($count - 3); $i >= 0; $i--) {
    $temp_array = array();
    $temp_array[$vector[$i]] = $new_array;
    $new_array = $temp_array;
}

print_r($new_array);

And the result will be like it:

Array
(
    [val1] => Array
        (
            [val2] => Array
                (
                    [val3] => Array
                        (
                            [val4] => Array
                                (
                                    [val5] => target
                                )

                        )

                )

        )

)
azibom
  • 1,769
  • 1
  • 7
  • 21
0

You can build the resulting array starting from the most recent nested element:

$vector = array("val1", "val2", "val3", "val4", "val5", "target");

$new_array = array_pop($vector);

foreach(array_reverse($vector) as $val) {
    $new_array = [$val => $new_array];
}

print_r($new_array);
id'7238
  • 2,428
  • 1
  • 3
  • 11
  • Listen, what if I start from a pre-built array, and the $new_array should be inside it? Example: $main_array = array("val1" => array()); $vector = array("val1", "val2", "val3", "val4", "val5", "target"); $new_array = array_pop($vector); foreach(array_reverse($vector) as $val) { $new_array = [$val => $new_array]; } what function should I write here? $main_array = array_merge($main_array, $new_array); does not work properly, it loses data if the main_array has many fields and sub_fields – Newbie at Programming Nov 27 '20 at 12:18
  • Here I got an example: $main_array = array( "child" => array("field1" => array(), "field2" => array(), "field3" => array("sub_field") => array()); I am updating this array in a for cycle, where each cycle changes a field of those(first field1, ..and so on). Now let's assume to use your function where vectors are: $vector1 = array("child", "field1", "value"); $vector2 = array("child", "field2", "value2"); I generate $new_array for each of them and array_merge in each cycle, the main_array will not be as expected: it will contain only one of those, so the integrity will be lost. – Newbie at Programming Nov 27 '20 at 12:32
  • It is possible that the `array_replace_recursive` function will do. – id'7238 Nov 27 '20 at 12:51
  • Sure a working solution, all seems to work well; I have a last problem: In some cases, I need to remove kind of fields with unset commands, how can I add it to this foreach cycle? Assume the elements of vector are fully contained in the array with your foreach cycle and array_replace_recursive(a try-catch will trigger if it's not). Formerly I used something like: $vector = [a,b,c]; $array = ...; $count = $count(vector); if($count == 3){ unset($array[0][1]); } What should be the correct workaround inside an array_reverse cycle? – Newbie at Programming Nov 27 '20 at 13:49