2

How to handle if I want to create an array like in following example:

$value="test";
$array=[];
$path=[2,17,513];

Now, I want to build an array based on the available data which should look like:

$array['2']['17']['513']='test';

Anyone any idea?

I tried array_shift to prepare but I miss the point where the single array data will be transformed to an array key....

3
  • 1
    Is there always 3 items in the $path array? Commented Jan 17, 2023 at 13:14
  • if not, you could build a recursive function. Could you provide us more details? With more lines in $array, $path? Ty Commented Jan 17, 2023 at 13:17
  • Related: stackoverflow.com/a/49563971, stackoverflow.com/questions/2579305 Commented Jan 17, 2023 at 13:24

1 Answer 1

-1
$value="test";
$array=[];
$path = [2, 17, 513];

[$a, $b, $c] = explode(",", $path);


$array[$a][$b][$c] = $value;


print_r($array[$a][$b][$c]); //returns 'test'
Sign up to request clarification or add additional context in comments.

1 Comment

Please provide some explanation for your code. Either in the code itself or outside of it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.