SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Non-Member
    Join Date
    Jan 2004
    Location
    Planet Earth
    Posts
    1,764
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help !! Create multiple array via string split

    Hello, here is a problem I've been trying to work out for the last hour or so. I have a string which I want to create a multiple array from, though in some cases, there may only be a depth of one.

    Ie I want this

    PHP Code:
    String1.String2 
    Turned into

    PHP Code:
    Array {
    [
    String1][String2] } 
    So I can put a value into this array, such as

    PHP Code:
    echo( $ar['String1']['String2'] ); // results in 
    Hello World

    The value would proberly be a function parameter, so that isn't the problem. I've tried using this, which works, but not so if array is only one deep.

    PHP Code:
    list( $first$second ) = explode'.'$key );
    #
    $array[$first][$second] = $parameterValue
    Does anyone have any ideas ? Looked at using

    PHP Code:
    foreach ( $x as $a ) { ... array_shift( ... ) ... } 
    But no luck there either

    Thanks in advance.

  2. #2
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have no idea how you turn String1.String2 into Hello World, care to explain again what you're trying to archive with a slightly better example?

  3. #3
    Non-Member
    Join Date
    Jan 2004
    Location
    Planet Earth
    Posts
    1,764
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Something more complicated that 'Hello World' I'm afraid

    Okay. You have a string - created elsewhere, not important - which I've needed to split by the period in the string yes ? ie

    PHP Code:
    module.administrator 
    But also string could be without a period, ie

    PHP Code:
    ViewNews 
    I could easilly pass the above via an array - which I'm doing at the moment - but I'm making some improvements, so instead of passing an array, I'm ''Pushing'' all the

    PHP Code:
     $var => $value 
    to the Controller directly. But since there are either one or two (or even more) depths to the target array, I decided to split/explode a string via the period, if you follow ?

    Now, I have for example, this

    PHP Code:
    ...
    <
    module>administrator</module>
    ... 
    Which I then turn into this

    PHP Code:
     module.administrator 
    Which on the Controller's ''Push'' method, would create this

    PHP Code:
     $array['module']['administrator'] = 'Some String'
    Another point is I noticed that with my current methods, duplicate entries, ie

    PHP Code:
     ['module']['administrator'
    could be over-written with, for example
    PHP Code:
     ['module']['editor'
    Which is another problem

    Hope this helps a lot more Gaheris ?

  4. #4
    SitePoint Wizard Ren's Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    1,060
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
        function getKey(&$arr$key$seperator '.')
        {
            foreach(
    explode($seperator$key) as $subkey)
            {
                if (!
    is_array($arr) || !isset($arr[$subkey]))
                    return 
    NULL;
                
    $arr $arr[$subkey];
            }
            return 
    is_array($arr) ? NULL $arr;
        }


        function 
    setKey(&$arr$key$value$seperator '.')
        {
            foreach(
    explode($seperator$key) as $subkey)
            {
                if (!isset(
    $arr[$subkey]) || !is_array($arr[$subkey]))
                    
    $arr[$subkey] = array();
                
    $arr = &$arr[$subkey];
            }
            
    $arr $value;
        }


        
    $arr = array();

        
    setKey($arr'String1.String2''test');
        
    setKey($arr'module.administrator''module-administrator');
        
    setKey($arr'module.editor''module-editor');

        
    var_dump($arr);
        
    var_dump(getKey($arr'module.editor')); 

  5. #5
    Non-Member
    Join Date
    Jan 2004
    Location
    Planet Earth
    Posts
    1,764
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Ren, this one has being doing my head in for a few days now

  6. #6
    Non-Member
    Join Date
    Jan 2004
    Location
    Planet Earth
    Posts
    1,764
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wahey. I'm an addict


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •