Re: Referencing Multi Dimensional Arrays

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux