F
F
fastboot2020-07-02 17:58:44
PHP
fastboot, 2020-07-02 17:58:44

PHP $array[$custom_path] how to specify your path?

I have this:
$map[] = tree;
$map[] = items;
$map[] = 0;
$map[] = items;
$map[] = 5;
$map[] = items;
$map[] = 1;
$map[] = items;
$map[] = 0;

Use $map to build a branch to use this branch as an array pointer.

$array [here I want to specify the branch path][name] = "New name";
$array [here I want to specify the branch path][name_img] = "./favicon.ico";
$array [here I want to specify the branch path][name_description] = "New description";

I want to do something like this:
$path = [tree][items][0][items][5][items][1][items][0][name];
$array{[$path]} = "New name";

If anything:
var_dump( $array[tree][items][0][items][5][items][1][items][0][name] );
Returns:
string(20) "Additional item"

You just need $path to specify the path in the array and set new parameters in this array branch.
Why? because there is a branch recursion, the tree branches can be infinite (I exaggerate, just a lot)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
F
fastboot, 2020-07-03
@fastboot

function tree_array_read (array $array, array $map)
{
$path = "[tree]";
foreach ( $map as $ID => $VALUE)
{
$path .= "[items][{$VALUE}]";
}
$code = "\$out = \$array{$path};";
eval($code);
return $out;
}

A
Anton Anton, 2020-07-02
@Fragster

The telepath tells me that this is some kind of parser and you can cut in a little earlier with xpath or jsonpath requests to get the data you need.

N
nokimaro, 2020-07-02
@nokimaro

Just today there was a question about converting a multidimensional array into a one-dimensional one
. How to convert a multidimensional associative array into a one-dimensional one while preserving the path?
Convert the original array to a flat one, and then the path to any element can be set as a string.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question