RE: [PHP] Referencing Multi Dimensional Arrays

2004-12-02 Thread Ford, Mike
To view the terms under which this email is distributed, please go to 
http://disclaimer.leedsmet.ac.uk/email.htm



On 02 December 2004 15:17, Robinson, Matthew wrote:

>  Thanks for the help Mike, If I've got a reference to an
> entry and say,
> wanted to insert another entry, can I use array_push() with the
> reference? 

Please keep discussions on list so that anyone reading this thread can
follow it to its resolution -- plus which, someone else might still chip in
with better ideas than me!

The reference can be used interchangeably with the thing it's a reference to
-- so if it's a reference to an individual element, no you can't use it with
array_push() any more than you could, say,
$arr['index']['array']['element']; but if the reference is to the parent
array, then you could, just as you could with $arr['index']['array'] (if
that all makes sense!).

It might help you to read the manual section on references at
http://www.php.net/manual/en/language.references.php.

Cheers!

Mike

-
Mike Ford,  Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Headingley Campus, LEEDS,  LS6 3QS,  United Kingdom
Email: [EMAIL PROTECTED]
Tel: +44 113 283 2600 extn 4730  Fax:  +44 113 283 3211 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] Referencing Multi Dimensional Arrays

2004-12-02 Thread Ford, Mike
To view the terms under which this email is distributed, please go to 
http://disclaimer.leedsmet.ac.uk/email.htm



On 01 December 2004 17:31, Robinson, Matthew wrote:

> I have a search function:
> 
> $search_result = multi_array_search($net_array,"needle");
> 
> now search_result equals an array of keys to locate the
> needle, this is
> variable in count.
> 
> Sometimes the array of keys is 3 entries other times 5, I
> want a way of
> taking those entries and being able to do something like:
> 
> $net_array[multi-dimensional-key] = value;
> 
> where sometimes it might be in longhand:
> 
> $net_array["net1"]["net2"]["address1"]
> 
> or other times:
> 
> $net_array["net1"]["address1"]
> 
> but you don't know how deep you're going until the search returns you
> the keys. 

Coming to this a bit late, but one possibility might be:

   $result = &$net_array;
   foreach ($search_result as $index):
  $result = &$result[$index];
   endforeach;
   // $result is now a reference to the array element.

Alternatively, wouldn't it be possible for the search function to return
that reference itself?  Or do you particularly want to know what the
sequence of accessor keys is?

Usual caveats apply: this is all off the top of my head, and completely
untested!

Cheers!

Mike

-
Mike Ford,  Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Headingley Campus, LEEDS,  LS6 3QS,  United Kingdom
Email: [EMAIL PROTECTED]
Tel: +44 113 283 2600 extn 4730  Fax:  +44 113 283 3211 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Referencing Multi Dimensional Arrays

2004-12-01 Thread Richard Lynch
Robinson, Matthew wrote:
>   I've kind of solved this question already using recursion but I
> recon (in the grand tradition) that someone has a better solution!

Recursion and http://php.net/is_array is probably the most clear-cut
solution.

If you are concerned that the arrays could become *S* deep and huge
that rrecursion will blow up the stack, then most times you can take your
straight-forward recursive algorithm, and convert it to an
almost-as-straight-forward iterative algorithm.

In fact, it's been proven that any "tail recursion" can be converted to
iteration.

Some terminology that may be meaningless to the Reader, loosely translated
into English, partly for the amusement of Readers who do understand this
terminology:

"blow up the stack" - a function that calls itself so often that it ends
up eating up the whole computer in a fit of self-indulgent gorging on its
own beautiful voice.

"tail recursion" - recursion where the function call to recurse is at the
tail end of the function -- the last line, not counting closing brackets.

"iterative" - a simple "for" or "while" loop

PS  The real solution may be to not store your data in that manner, as it
clearly is giving you trouble...

-- 
Like Music?
http://l-i-e.com/artists.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Referencing Multi Dimensional Arrays

2004-12-01 Thread Jason Wong
On Thursday 02 December 2004 01:31, Robinson, Matthew wrote:
> lol, perhaps embedded a bit too deep in my pre-amble. My question is
> simply what's a good way to reference a given array entry when you don't
> know where it is or how deep the array is. I can do a multi dimensional
> array search and return an array of keys to locate the item, I then want
> to use that array of keys to be able to reference the data to perhaps do
> work on it ie:
>
> The array is a collection of network data that can consist of address
> allocation entries or other networks which can contain either more
> networks or allocations. I call my search function to locate a
> particular network which returns me an array of keys to locate that
> entry. I then want to pass that to another function to actually work on
> the data.
>
> I have a search function:
>
> $search_result = multi_array_search($net_array,"needle");
>
> now search_result equals an array of keys to locate the needle, this is
> variable in count.
>
> Sometimes the array of keys is 3 entries other times 5, I want a way of
> taking those entries and being able to do something like:
>
> $net_array[multi-dimensional-key] = value;
>
> where sometimes it might be in longhand:
>
> $net_array["net1"]["net2"]["address1"]
>
> or other times:
>
> $net_array["net1"]["address1"]
>
> but you don't know how deep you're going until the search returns you
> the keys.
>
> Hope that clears things a bit!

Certainly a lot clearer.

The only (easy) method I can think of is to use eval(), something like:

  $MyKey = "[allocations][network][0]";
  $MyKey = '$net_array' .  $MyKey;
  eval("\$val = $MyKey;");
  print_r($val);

Another more messy method would be to use something like preg_match_all() to 
grab the individual array keys, ie 'allocations', 'network', '0' then:

  switch (number of indices) {
  case 1 :
$val = $net_array[$idx1];
break;
  case 2 :
$val = $net_array[$idx1][$idx2];
break;
  ...
  }

-- 
Jason Wong -> Gremlins Associates -> www.gremlins.biz
Open Source Software Systems Integrators
* Web Design & Hosting * Internet & Intranet Applications Development *
--
Search the list archives before you post
http://marc.theaimsgroup.com/?l=php-general
--
/*
Is this the line for the latest whimsical YUGOSLAVIAN drama which also
makes you want to CRY and reconsider the VIETNAM WAR?
*/

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] Referencing Multi Dimensional Arrays

2004-12-01 Thread Robinson, Matthew
lol, perhaps embedded a bit too deep in my pre-amble. My question is
simply what's a good way to reference a given array entry when you don't
know where it is or how deep the array is. I can do a multi dimensional
array search and return an array of keys to locate the item, I then want
to use that array of keys to be able to reference the data to perhaps do
work on it ie:

The array is a collection of network data that can consist of address
allocation entries or other networks which can contain either more
networks or allocations. I call my search function to locate a
particular network which returns me an array of keys to locate that
entry. I then want to pass that to another function to actually work on
the data.

I have a search function:

$search_result = multi_array_search($net_array,"needle");

now search_result equals an array of keys to locate the needle, this is
variable in count.

Sometimes the array of keys is 3 entries other times 5, I want a way of
taking those entries and being able to do something like:

$net_array[multi-dimensional-key] = value;

where sometimes it might be in longhand:

$net_array["net1"]["net2"]["address1"] 

or other times:

$net_array["net1"]["address1"]

but you don't know how deep you're going until the search returns you
the keys.

Hope that clears things a bit!

Kind regards

Matthew
 

-Original Message-
From: Jason Wong [mailto:[EMAIL PROTECTED] 
Sent: 01 December 2004 17:23
To: [EMAIL PROTECTED]
Subject: Re: [PHP] Referencing Multi Dimensional Arrays

Sorry if I missed it, what was the question?



This message has been checked for all known viruses by the 
CitC Virus Scanning Service powered by SkyLabs. For further information visit
http://www.citc.it

___

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Referencing Multi Dimensional Arrays

2004-12-01 Thread Greg Donald
On Wed, 1 Dec 2004 17:14:56 -, Robinson, Matthew
<[EMAIL PROTECTED]> wrote:
> I've kind of solved this question already using recursion but I
> recon (in the grand tradition) that someone has a better solution!

Recursion and is_array() is the way I'd parse through it.


-- 
Greg Donald
Zend Certified Engineer
http://gdconsultants.com/
http://destiney.com/

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Referencing Multi Dimensional Arrays

2004-12-01 Thread Jason Wong
On Thursday 02 December 2004 01:14, Robinson, Matthew wrote:
>  I've kind of solved this question already using recursion but I
> recon (in the grand tradition) that someone has a better solution!
>
> I have an multi dimensional array that is of variable depth, a snippet
> is shown below. The problem is that I don't know how deep the array goes
> as any entry can contain further entries which can contain further
> entries and as the array grows the data is increasingly unknown. My
> first thought was to use variable variables but the following doesn't
> work:

Sorry if I missed it, what was the question?

-- 
Jason Wong -> Gremlins Associates -> www.gremlins.biz
Open Source Software Systems Integrators
* Web Design & Hosting * Internet & Intranet Applications Development *
--
Search the list archives before you post
http://marc.theaimsgroup.com/?l=php-general
--
/*
If we all work together, we can totally disrupt the system.
*/

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php