Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to map array depth?

Tags:

arrays

php

I'm not sure how to call this, and I can't find what I'm looking for because I must be looking for the wrong keywords, so even the right search term would help me out here:

I have a $map array and a $data array. The $data array is a dynamic array based on a xml that is parsed. Simplified Example:

$data = array('a' => array(0=> array('hello'=>'I want this data')));

$map = array('something' => 'a.0.hello');

Now I'd like to set $test to the value of $data['a']['0']['hello'] by somehow navigating there using $map['something']. The idea behind this is to create a mapping array so that no code changes are required if the xml format is to be changed, only the mapping array. Any help in the good direction is very much appreciated :)

like image 274
jwebdev Avatar asked Jun 01 '26 19:06

jwebdev


1 Answers

// Set pointer at top of the array 
$path = &$data;
// Asume that path is joined by points
foreach (explode('.', $map['something']) as $p) 
   // Make a next step
   $path = &$path[$p];
echo $path; // I want this data
like image 108
splash58 Avatar answered Jun 03 '26 09:06

splash58