...

View Full Version : Resolved creating and loading sub key element



durangod
10-29-2012, 10:32 PM
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:




$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:




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)




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

davidjones1990
10-29-2012, 10:55 PM
You can't have identical keys in a array so you need to have a multidimensional array.

So it should look like this:



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:



$picful[] = [ 'url' => $CONST_LINK_ROOT . '/includes$full->Path',
'path' => "includes" . $medium->Path
]


Hope that helps

durangod
10-29-2012, 11:09 PM
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 ]

Fou-Lu
10-29-2012, 11:13 PM
Watch this syntax above though. Using [] is a very new feature in PHP (like, 5.4 new).
The classic way is just as simple:


$picful[] = array('url' => 'urlstuffs', 'path' => 'pathstuffs');

davidjones1990
10-29-2012, 11:15 PM
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.

durangod
10-29-2012, 11:24 PM
Fou-Lu

can i do something like this




$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




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 :)

Fou-Lu
10-29-2012, 11:34 PM
No, the fill methods don't deal with changing data. You can use combination data from two arrays if you want:


$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.

durangod
10-29-2012, 11:49 PM
ahhhh haaaaa, understood, thanks so much. Now i know why push would not work for me lol...

thanks again :thumbsup:

Fou-Lu
10-30-2012, 12:57 AM
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 (http://php.net/manual/en/class.splfixedarray.php) 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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum