RE: [PHP] Referencing Multi Dimensional Arrays - PHP

This is a discussion on RE: [PHP] Referencing Multi Dimensional Arrays - PHP ; 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 ...

+ Reply to Thread
Results 1 to 2 of 2

RE: [PHP] Referencing Multi Dimensional Arrays

  1. Default RE: [PHP] Referencing Multi Dimensional Arrays

    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 [mailtohp-general@gremlins.biz]
    Sent: 01 December 2004 17:23
    To: php-general@lists.php.net
    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

    ___

  2. Default Re: [PHP] Referencing Multi Dimensional Arrays

    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?
    */

+ Reply to Thread