473,385 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

navigate multidimensional array with path in flat array

I have a big multidimensional array and I need to access it at various
levels. The path is stored in a flat array:

I'm doing it like this:

$path = array("people", "men", "tall")

$my_array = <---- this is the big array!

foreach ($path as $x) {$p .= "[\"".$x."\"]" ;}
$str = "$part = $my_array".$p;
eval($str);

which works but is very ugly, I'd like to do something like

$temp = $my_array[$path];

but it doesn't work of course.

What can I do?

thanks

Oct 18 '07 #1
1 2964
On Thu, 18 Oct 2007 17:32:41 +0200, Snaggy <l.******@gmail.comwrote:
I have a big multidimensional array and I need to access it at various
levels. The path is stored in a flat array:

I'm doing it like this:

$path = array("people", "men", "tall")

$my_array = <---- this is the big array!

foreach ($path as $x) {$p .= "[\"".$x."\"]" ;}
$str = "$part = $my_array".$p;
eval($str);

which works but is very ugly, I'd like to do something like

$temp = $my_array[$path];

but it doesn't work of course.
something like:
function _get_from_array($array,$path){
$cur = $array;
while($key = array_shift($path)){
if(array_key_exists($cur,$pkey)){
$cur = $cur[$key];
} else {
trigger_error('array key doesn\'t exist');
return false;
}
}
return $cur;
}
$temp = _get_from_array($array,$path);

--
Rik Wasmus
Oct 18 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: TLOlczyk | last post by:
I have a brain cramp and I need some help. I have a chunk of code below which demonstrates a problem I have with multidimensional arrays. I want to keep it simple but something specific is...
9
by: Charles Banas | last post by:
i've got an interesting peice of code i'm maintaining, and i'd like to get some opinions and comments on it, hopefully so i can gain some sort of insight as to why this works. at the top of the...
1
by: Mark Smith | last post by:
I'm trying to copy data from a 1D array to a 2D array. The obvious thing doesn't work: int twoDee = new int; int oneDee = new int { 1, 2 }; Array.Copy(oneDee, 2, twoDee, 2, 2); This causes a...
2
by: chris | last post by:
Hi there, I created a Multidimensional array of labels Label lblMultiArray = new Label { {Label3, LblThuTotal}, {Label4,LblFriTotal} }; Now I would like to compare the values in the array,...
3
by: BobbyS | last post by:
I am trying to develop a multidimensional array for use of searching a very large database. I understand the concept of one and two dimensional arrays but this project would include up to 12 or 13...
2
by: TG | last post by:
Hi there. I am working with multi-dimensional arrays and I need to get coordinates of the min value in it. using myarray.argmin() returns the index in the flatten array, which is a first...
1
by: samoore33 | last post by:
<States> - <state> <id>NY</id> <Flat>False</Flat> <Value>Value1</Value> - <Taxes> - <Tax> <Min>Min1</Min> <Max>Max1</Max> <Value>Value2</Value>
3
by: Tyrone Slothrop | last post by:
I have created a script which recurses a display of directories like so: <? $dir = "/path/to/base/directory"; function scan_dir_recurse ($dir,$tab) { global $fileArr; $tab++; if ($tab 4) {...
9
by: Slain | last post by:
I need to convert a an array to a multidimensional one. Since I need to wrok with existing code, I need to modify a declaration which looks like this In the .h file int *x; in a initialize...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.