Build an array index from a string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • BravoFoxtrot

    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'=>a rray('id'=>1,
    'title'=>'Manag er')),
    array('Data'=>a rray('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?
  • Michael Fesser

    #2
    Re: Build an array index from a string

    ..oO(BravoFoxtr ot)
    >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.
    >
    >$arrayToAcce ss = array( array('Data'=>a rray('id'=>1,
    >'title'=>'Mana ger')),
    array('Data'=>a rray('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

    Comment

    • BravoFoxtrot

      #3
      Re: Build an array index from a string

      On May 27, 1:44 pm, Michael Fesser <neti...@gmx.de wrote:
      .oO(BravoFoxtro t)
      >
      >
      >
      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'=>a rray('id'=>1,
      'title'=>'Manag er')),
      array('Data'=>a rray('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'=>a rray('id'=>1, 'title'=>'Manag er')),
      array('Data'=>a rray('id'=>1, 'title'=>'Clerk '))
      );

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

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


      The result of getVal is "Manager"

      Comment

      • C. (http://symcbean.blogspot.com/)

        #4
        Re: Build an array index from a string

        On May 27, 10:51 pm, BravoFoxtrot <bravofoxtro... @gmail.comwrote :
        On May 27, 1:44 pm, Michael Fesser <neti...@gmx.de wrote:
        >
        >
        >
        .oO(BravoFoxtro t)
        >
        >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.
        >
        >$arrayToAcce ss = array( array('Data'=>a rray('id'=>1,
        >'title'=>'Mana ger')),
        array('Data'=>a rray('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'=>a rray('id'=>1, 'title'=>'Manag er')),
        array('Data'=>a rray('id'=>1, 'title'=>'Clerk '))
        );
        >
        echo getVal($arrayTo Access, array(0,'Data', 'title'));
        echo "\n";
        var_dump($array ToAccess);
        >
        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'=>'Ma nager');
        $data[]=array('1'=>'Cl erk');

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

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

        C.


        Comment

        • Michael Fesser

          #5
          Re: Build an array index from a string

          ..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('appl ication/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

          Comment

          Working...