Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts

    Array Convertion Help?

    Hi I was trying to create an array convertion function for my php class but got stuck some of the way through so thought I would ask for some help.

    What i'm trying to do is covert an array like this.
    PHP Code:
    array(
      
    'Game::Name::GameName',
      
    'Game::Points::null',
      
    'Game::Lives::10',
      
    'Game::Guesses::null',
      
    'Player::ID::1234567890'
    ); 
    To an array like this
    PHP Code:
    array(
        [
    Game] => array(
            [
    Name] => 'GameName',
            [
    Points] => null,
            [
    Lives] => 10,
            [
    Guesses] => null,
        ),
        
        [
    Player] => array(
            [
    ID] => 1234567890
        
    )
    ); 
    I tried doing a continuous function but it didn't add the sections under the game section.

    Any Help Welcome, Thank You

    DJCMBear
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Something like this perhaps:

    PHP Code:
    foreach ($inAarray as $val)
    {
        
    $data explode("::"$val);
        
    $outArray[$data[0]][$data[1]] = $data[2];


  • #3
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    Would this keep on going so if I had something like Game::Errors::Internal::null

    Would it display like this?
    PHP Code:
    array(
        [
    Game] => array(
            [
    Errors] => array(
                [
    Internal] => null
            
    )
        )

    What i'm looking for is a function that counts how many sections there are and the makes an array for that section.

    so like
    PHP Code:
    array(
      
    'Main',
      
    'NewMain::Inner',
      
    'OldMain::Outer::Inner',
      
    'NewOld::OldMain::Outer::Inner'

    Would become this.
    PHP Code:
    array(
       [
    0] => 'Main',
       [
    NewMain] => 'Inner',
       [
    OldMain] => array(
         [
    Outer] => 'Inner'
       
    ),
       [
    NewOld] => array(
         [
    OldMain] => array(
            [
    Outer] => 'Inner'
         
    )
       )

    Last edited by DJCMBear; 06-16-2010 at 06:30 PM.
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #4
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    can no one help???
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #5
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Well you changed the rules between your first and third posts. I'm just waiting for you to give us the final requirements.

  • #6
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    Basicly what I am looking for is a function which will do something like this.

    The function will lookat the string of each section by using func_get_args() and then for each of the arguments it can be any length so like this main::section::new or like main::name or even main::section::new::home, but the code in post #2 it only gets the first two parts and outputs the third part so if there are more or less than three sections it wont work.

    I need to check how many section there are in each of the function arguments and then make an array for that section.

    If this doesn't make sence im sorry but its hard to explain.
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #7
    New Coder
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    17
    Thanks
    1
    Thanked 1 Time in 1 Post
    here is a code snippet, unfortunaly its just the other way round. it cleans the dimensions of the array... But i think you should be able to modify it

    PHP Code:
    function nested_Array_to_one_dimensional_Array ($nested_Array) {
        
    //ein paar Variablen statisch definieren, sodass sie die Rekursion überleben
        
    static $one_dimensional_Array_keys;
        static 
    $one_dimensional_Array_values;
        static 
    $prefix '';
        static 
    $prefix_old;
        if(
    $prefix == '') {
            
    $one_dimensional_Array_keys = array();
            
    $one_dimensional_Array_values = array();
        }
        foreach (
    $nested_Array as $key => $value) {
            if (!
    is_array($value)) {
                
    //vor dem aktuellen Schlüssel ein Prefix und ein Unterstrich anbringen, falls nötig
                
    if ($prefix != '') {$key $prefix.'_'.$key;}
                
    //Den Schlüsselnnamen und den Wert sichern
                
    $one_dimensional_Array_keys[] = $key;
                
    $one_dimensional_Array_values[] = $value;
            } else {
                
    //Das Prefix der momentanen Iteration sichern
                
    $prefix_old $prefix;
                
    //Unterstrich am Anfang des Schlüssels vermeiden
                
    if ($prefix == '') {
                    
    $prefix $key;
                } else {
                    
    $prefix $prefix."_".$key;
                }
                
    //rekursiv das nächste Array einhängen
                
    nested_Array_to_one_dimensional_Array($value);
                
    //Das Prefix auf die Sicherung zurücksetzen
                
    $prefix $prefix_old;
            }
        }
        
    //Die beiden Array-Paare (Schlüssel und Werte) verbinden
        
    $one_dimensional_Array array_combine($one_dimensional_Array_keys$one_dimensional_Array_values);
        return 
    $one_dimensional_Array;


    Morri

  • #8
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    Thanks for the code but it didn't help me with what im trying to do.
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P


  •  

    Posting Permissions

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