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 9 of 9
  1. #1
    Senior Coder
    Join Date
    Nov 2010
    Posts
    1,341
    Thanks
    260
    Thanked 32 Times in 31 Posts

    creating and loading sub key element

    Hi i am building an array and i want to load the main key and then a sub key but not sure how.

    here is the code:

    PHP Code:

           $picful 
    = array();
           
    $medarr = array();

    //begin loop
           
    $url_src .=",'$CONST_LINK_ROOT/includes$full->Path'";
           
    $storemed "includes".$medium->Path;
           
    $medarr[] = $storemed;

           
    // load new array with full size image link and sub link of med image
          // Push the value into the next available index  
             
    array_push($picful,$url_src,$medarr);   

    //end loop 

    and here is the output:

    PHP Code:

    Array
    (
        [
    0] => ,'http://www.mysite.com/includes/members/61.jpg'
        
    [1] => Array
            (
                [
    0] => includes/members/61_small.jpg
            

    that is obviously not what i want, what i want is something like this for example)

    PHP Code:

    Array
    (
        [
    0] => ,'http://www.mysite.com/includes/members/61.jpg'
        
    [path] => includes/members/61_small.jpg

        
    [1] => ,'http://www.mysite.com/includes/members/61.jpg'
        
    [path] => includes/members/61_small.jpg

        
    [2] => ,'http://www.mysite.com/includes/members/61.jpg'
        
    [path] => includes/members/61_small.jpg

            
    )

    so that each element has its own sub element named path 
    Last edited by durangod; 10-29-2012 at 11:10 PM.

  • #2
    New Coder
    Join Date
    Sep 2011
    Posts
    63
    Thanks
    0
    Thanked 7 Times in 7 Posts
    You can't have identical keys in a array so you need to have a multidimensional array.

    So it should look like this:

    PHP Code:
    Array 

        [
    0] => Array 
            ( 
                [
    url] => 'http://www.mysite.com/includes/members/61.jpg'
                
    [path] => includes/members/61_small.jpg 
            
    }

         [
    1] => Array 
            ( 
                [
    url] => 'http://www.mysite.com/includes/members/61.jpg'
                
    [path] => includes/members/61_small.jpg 
            
    }


    The code you need should be something like this:

    PHP Code:
    $picful[] = [ 'url' => $CONST_LINK_ROOT '/includes$full->Path'
                     
    'path' => "includes" $medium->Path
                   

    Hope that helps

  • #3
    Senior Coder
    Join Date
    Nov 2010
    Posts
    1,341
    Thanks
    260
    Thanked 32 Times in 31 Posts
    yeah that looks better but i am getting an invalid [
    i wonder if we are missing quote or something,
    i did add a ; at the end after the ]
    Last edited by durangod; 10-29-2012 at 10:11 PM.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Watch this syntax above though. Using [] is a very new feature in PHP (like, 5.4 new).
    The classic way is just as simple:
    PHP Code:
    $picful[] = array('url' => 'urlstuffs''path' => 'pathstuffs'); 

  • Users who have thanked Fou-Lu for this post:

    durangod (10-29-2012)

  • #5
    New Coder
    Join Date
    Sep 2011
    Posts
    63
    Thanks
    0
    Thanked 7 Times in 7 Posts
    Yeh sorry the code I write on here is never tested.

    You may need to use array() instead of [] depending on your version of PHP.

  • Users who have thanked davidjones1990 for this post:

    durangod (10-29-2012)

  • #6
    Senior Coder
    Join Date
    Nov 2010
    Posts
    1,341
    Thanks
    260
    Thanked 32 Times in 31 Posts
    Fou-Lu

    can i do something like this

    PHP Code:

    $picful 
    = array();

     
    $url_src .=",'$CONST_LINK_ROOT/includes$full->Path'";
     
    $storemed includes$medium->Path;

    $pickeys = array('url','path');

    //i know this is wrong, 

    //but can i combine push and fill keys?

    array_push($picful(array_fill_keys($pickeys,$url_src,$storemed)); 


    UPDATE: nevermind the question about using array fill, that worked perfect Fou-Lu

    PHP Code:

    here is the code 


     $picful
    [] = array('url' => $url_src'path' => $storemed);  

    and 
    output


    I grabbed the last element PERFECT :)

    [
    3] => Array
            (
                [
    url] => ,'http://www.mysite.com/includes/members/61.jpg','http://www.mysite.com/includes/members/58.jpg','http://www.mysite.com/includes/members/59.jpg','http://www.mysite.com/includes/members/60.jpg'
                
    [path] => ,'includes/members/61_small.jpg','includes/members/58_small.jpg','includes/members/59_small.jpg','includes/members/60_small.jpg'
            

    outstanding
    Last edited by durangod; 10-29-2012 at 10:36 PM.

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    No, the fill methods don't deal with changing data. You can use combination data from two arrays if you want:
    PHP Code:
    $a = array('cat''dog');
    $b = array('feline''canine');
    $combined array_combine($b$a); // array('feline' => 'cat', 'canine' => 'dog') 
    You can push all you want to get there, but ultimately you cannot push an associative offset. Pushing is an array technique, and associative offsets are hashtable techniques. Hashtables use "put" techniques, which is what $a['associativeoffset'] is used for.

  • #8
    Senior Coder
    Join Date
    Nov 2010
    Posts
    1,341
    Thanks
    260
    Thanked 32 Times in 31 Posts
    ahhhh haaaaa, understood, thanks so much. Now i know why push would not work for me lol...

    thanks again

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Yep you bet.
    Ultimately, PHP has limited uses for push/pop and shift/unshift. Since it doesn't have an actual array in the language itself, its typically easier to use the (effectively) get / put (or in PHP wording, offsetGet and offsetSet) in almost any situation.
    If you want behaviour closer to an array, PHP does have the splFixedArray object. I'll have to test if it works with push/pop though; I somehow don't believe that there is any interface chain to those methods.


  •  

    Posting Permissions

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