473,383 Members | 1,733 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,383 software developers and data experts.

Build an array index from a string

Hi,

I'm trying to build an index into a multi dimensional associative
array. I may not know how many dimensions there are so i want to pass
the array indexes as a variable.

$arrayToAccess = array( array('Data'=>array('id'=>1,
'title'=>'Manager')),
array('Data'=>array('id'=>1,
'title'='Clerk')) );

$index = "['Data']['title']";
// or $index=''; if this is a single dim array ie $arrayToAccess =
array('Manager', 'Clerk');

foreach ( $arrayToAccess as $data {
$foundTitle = ${data.$index};
// do something to found title
}

Is there a way to do this in php without have to build a looping
structure?
Jun 2 '08 #1
4 2262
..oO(BravoFoxtrot)
>I'm trying to build an index into a multi dimensional associative
array. I may not know how many dimensions there are so i want to pass
the array indexes as a variable.

$arrayToAccess = array( array('Data'=>array('id'=>1,
'title'=>'Manager')),
array('Data'=>array('id'=>1,
'title'='Clerk')) );

$index = "['Data']['title']";
// or $index=''; if this is a single dim array ie $arrayToAccess =
array('Manager', 'Clerk');

foreach ( $arrayToAccess as $data {
$foundTitle = ${data.$index};
// do something to found title
}

Is there a way to do this in php without have to build a looping
structure?
Yes, with eval(). But eval() is evil and inefficient. You better use a
more simple form for your index, which can easily be split into parts,
e.g.

$index = 'data/title';

Split it, either with explode() or step-by-step with strtok(), and then
loop through your array to find the element you want.

Micha
Jun 2 '08 #2
On May 27, 1:44 pm, Michael Fesser <neti...@gmx.dewrote:
.oO(BravoFoxtrot)
I'm trying to build an index into a multi dimensional associative
array. I may not know how many dimensions there are so i want to pass
the array indexes as a variable.
$arrayToAccess = array( array('Data'=>array('id'=>1,
'title'=>'Manager')),
array('Data'=>array('id'=>1,
'title'='Clerk')) );
$index = "['Data']['title']";
// or $index=''; if this is a single dim array ie $arrayToAccess =
array('Manager', 'Clerk');
foreach ( $arrayToAccess as $data {
$foundTitle = ${data.$index};
// do something to found title
}
Is there a way to do this in php without have to build a looping
structure?

Yes, with eval(). But eval() is evil and inefficient. You better use a
more simple form for your index, which can easily be split into parts,
e.g.

$index = 'data/title';

Split it, either with explode() or step-by-step with strtok(), and then
loop through your array to find the element you want.

Micha
Thanks for the tip. I came up with this function on the dentist's
chair this afternoon.

$arrayToAccess = array(
array('Data'=>array('id'=>1, 'title'=>'Manager')),
array('Data'=>array('id'=>1, 'title'=>'Clerk'))
);

echo getVal($arrayToAccess, array(0,'Data','title'));
echo "\n";
var_dump($arrayToAccess);

function getVal($array, $index)
{
foreach ( $index as $i )
{
$array = $array[$i];
}
return $array;
}
The result of getVal is "Manager"
Jun 2 '08 #3
On May 27, 10:51 pm, BravoFoxtrot <bravofoxtro...@gmail.comwrote:
On May 27, 1:44 pm, Michael Fesser <neti...@gmx.dewrote:
.oO(BravoFoxtrot)
>I'm trying to build an index into a multi dimensional associative
>array. I may not know how many dimensions there are so i want to pass
>the array indexes as a variable.
>$arrayToAccess = array( array('Data'=>array('id'=>1,
>'title'=>'Manager')),
array('Data'=>array('id'=>1,
>'title'='Clerk')) );
>$index = "['Data']['title']";
>// or $index=''; if this is a single dim array ie $arrayToAccess =
>array('Manager', 'Clerk');
>foreach ( $arrayToAccess as $data {
$foundTitle = ${data.$index};
// do something to found title
>}
>Is there a way to do this in php without have to build a looping
>structure?
Yes, with eval(). But eval() is evil and inefficient. You better use a
more simple form for your index, which can easily be split into parts,
e.g.
$index = 'data/title';
Split it, either with explode() or step-by-step with strtok(), and then
loop through your array to find the element you want.
Micha

Thanks for the tip. I came up with this function on the dentist's
chair this afternoon.

$arrayToAccess = array(
array('Data'=>array('id'=>1, 'title'=>'Manager')),
array('Data'=>array('id'=>1, 'title'=>'Clerk'))
);

echo getVal($arrayToAccess, array(0,'Data','title'));
echo "\n";
var_dump($arrayToAccess);

function getVal($array, $index)
{
foreach ( $index as $i )
{
$array = $array[$i];
}
return $array;

}

The result of getVal is "Manager"
It looks as if you're trying (badly) to implement second and third
normal forms using arrays - I can't see from your code what you're
trying to achieve but the route you are taking is a dead end.

Depending on what it is you really want to do with your data, maybe
you just need:

$data=array();
$data[]=array('1'=>'Manager');
$data[]=array('1'=>'Clerk');

or perhaps:
$data[]=array('id'=>1, 'title'=>'Manager');
$data[]=array('id'=>1, 'title'=>'Clerk');

Regardless it's a very inefficient **collection** unless you use
indexing on the outer keys of the array...something like
$data=array();
$data['Manager']=array(1=>'first',2=>'second'...);
$data['Clerk']=array(1=>'first'....);

C.
Jun 2 '08 #4
..oO(C. (http://symcbean.blogspot.com/))
>It looks as if you're trying (badly) to implement second and third
normal forms using arrays - I can't see from your code what you're
trying to achieve but the route you are taking is a dead end.
He has a tree-like structure of nested arrays and wants to have a
convenient way to access any arbitrary element fromt the tree by just
specifying the path to it. Nothing special. And the function he found at
the dentist seems to work fine.

In my own applications I use something similar (slightly more complex)
to store various values in a kind of registry, which is simply an array
tree. I can specify the requested key as 'application/database/tables'
for example and get the value stored there with something like

$tables = $registry->getValue('application/database/tables');

The getValue() method then simply splits the passed string and walks
down the registry array tree to find the value I want, quite similar
to the situation we have here. It becomes even more convenient with
implementing the ArrayAccess interface in my registry class:

$tables = $registry['application/database/tables'];

Micha
Jun 2 '08 #5

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

Similar topics

1
by: Phil Powell | last post by:
Consider this: class ActionHandler { ...
5
by: Denis Perelyubskiy | last post by:
Hello, I need to make an array of elements accross forms. My javascript skills, as evident from this question, are rather rudimentary. I tried to make an associative array and index it with...
47
by: VK | last post by:
Or why I just did myArray = "Computers" but myArray.length is showing 0. What a hey? There is a new trend to treat arrays and hashes as they were some variations of the same thing. But they...
38
by: VK | last post by:
Hello, In my object I have getDirectory() method which returns 2-dimentional array (or an imitation of 2-dimentional array using two JavaScript objects with auto-handled length property - please...
3
by: Ben | last post by:
Hi I am creating a dynamic function to return a two dimensional array from a delimeted string. The delimited string is like: field1...field2...field3... field1...field2...field3......
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
2
by: JJA | last post by:
I'm looking at some code I do not understand: var icons = new Array(); icons = new GIcon(); icons.image = "somefilename.png"; I read this as an array of icons is being built. An element of...
5
by: trifinite | last post by:
I am having difficulty understanding why the following code does not result in error. The code below simply traverses a string, extracting any continuous sequence of digits (0 - 9) and placing them...
10
by: Mo | last post by:
Hi, I am trying to write a code to build a string 768 characters long. This string is going to be written to a file which is then read by another application. The format of the string is already...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.