...

View Full Version : array push question



durangod
10-26-2012, 08:57 AM
I like array push but i dont like the stack affect, all i need is the very last element that shows everything that has been pushed, i dont need anything prior to that.

How do i get it to replace itself with current content plus new push content each time so every pass of the foreach, the pushed array only has one element with everything in there?

this is not correct im sure but this is where my thought is something like



// i know this is sooo wrong lol just thinking is all

$myarray[] = $myarray[].$newdata


//even this gives me the stack effect

$myarray[] = $newdata;

update, just tried this as well, but im left with an empty array lol

array_push($myarray,$data); // Push the value into the next available index
$myarray = array_shift($myarray); // strip prev stack elements

durangod
10-26-2012, 09:29 AM
Ok might have it but im wondering of there is a better way.




//set as array
$myarray = array();

//start foreach loop that processes another query

//grab the value you want in the array and push it inside

array_push($myarray,$data);

//foreach finally finishes loop

//load just the last element from the built array and make a new array from that

$all_myinfo[] = array_pop($myarray); //this was corrected below to remove the []

// do i need to unset the original array?

unset($myarray);

//so that leaves me with

Array
(
[0] => ,'that has 16 values sep by comma', 'and so on and so on',
)

//so now im guessing i need to explode this to get something i can loop thru?

update again, so when i tried to explode this by comma i disovered i need to load the new array without the []

//so it should be this

$all_myinfo = array_pop($myarray);

//now i can explode but another issue, it has a leading comma before the explode so in the explode result the element [0] is empty and all the other elements are there.

// i dont like that.. now i need to see how to fix this issue, this just keeps getting to be a bigger and bigger deal all for a very simple task grrr

//update ok i had to toss a trim in there

//strip any leading or trailing commas - or [0] element will be blank after explode
$all_myinfo = trim($all_myinfo,',');

//now i can explode it

$fulldata_array = explode(',',$all_myinfo);

//now i have a good looking array that i can loop thru.

//im sure Tango or Fou-Lu or someone else good can do all this in about 2 lines of code lol... :)

Fou-Lu
10-26-2012, 04:48 PM
How do i get it to replace itself with current content plus new push content each time so every pass of the foreach, the pushed array only has one element with everything in there?


I don't understand what you are doing here. Please show an example of how its walked, what is added and the end result. If you want to end up with a single string comma separated, then do not waste your time with iterating it at all, just implode it.

Also, do not modify anything in a foreach. Ever. It relies on internal pointer operations, so if you modify it than the results become undefined. Use while syntax with push/pop and shift/unshift instead.

durangod
10-26-2012, 07:02 PM
what i was trying to avoid was the stacking that push does, it starts out 1 item then two items and so on as below indicates.

Thats why i did push but with a pop into another array the last item only (here number 18)

my pref would be to build it ontop of itself each time and replace itself each time so i dont need to use pop, and at the end i have just one item to explode on a comma seperated element.



Array
(
[0] => ,'http://www.mysite.com/members/104.jpg'
[1] => ,'http://www.mysite.com/members/104.jpg','http://www.mysite.com/members/108.jpg'
[2] => ,'http://www.mysite.com/members/104.jpg','http://www.mysite.com/members/108.jpg','http://www.mysite.com/members/42.jpg'
[3] => ,'http://www.mysite.com/members/104.jpg','http://www.mysite.com/members/108.jpg','http://www.mysite.com/members/42.jpg','http://www.mysite.com/members/57.jpg'
[4] => ,'http://www.mysite.com/members/104.jpg','http://www.mysite.com/members/108.jpg','http://www.mysite.com/members/42.jpg','http://www.mysite.com/members/57.jpg','http://www.mysite.com/members/66.jpg'
[5] => ,'http://www.mysite.com/members/104.jpg','http://www.mysite.com/members/108.jpg','http://www.mysite.com/members/42.jpg','http://www.mysite.com/members/57.jpg','http://www.mysite.com/members/66.jpg','http://www.mysite.com/members/67.jpg'
[6] => ,'http://www.mysite.com/members/104.jpg','http://www.mysite.com/members/108.jpg','http://www.mysite.com/members/42.jpg','http://www.mysite.com/members/57.jpg','http://www.mysite.com/members/66.jpg','http://www.mysite.com/members/67.jpg','http://www.mysite.com/members/68.jpg'
[7] => ,'http://www.mysite.com/members/104.jpg','http://www.mysite.com/members/108.jpg','http://www.mysite.com/members/42.jpg','http://www.mysite.com/members/57.jpg','http://www.mysite.com/members/66.jpg','http://www.mysite.com/members/67.jpg','http://www.mysite.com/members/68.jpg','http://www.mysite.com/members/69.jpg'
[8] => ,'http://www.mysite.com/members/104.jpg','http://www.mysite.com/members/108.jpg','http://www.mysite.com/members/42.jpg','http://www.mysite.com/members/57.jpg','http://www.mysite.com/members/66.jpg','http://www.mysite.com/members/67.jpg','http://www.mysite.com/members/68.jpg','http://www.mysite.com/members/69.jpg','http://www.mysite.com/members/70.jpg'
[9] => ,'http://www.mysite.com/members/104.jpg','http://www.mysite.com/members/108.jpg','http://www.mysite.com/members/42.jpg','http://www.mysite.com/members/57.jpg','http://www.mysite.com/members/66.jpg','http://www.mysite.com/members/67.jpg','http://www.mysite.com/members/68.jpg','http://www.mysite.com/members/69.jpg','http://www.mysite.com/members/70.jpg','http://www.mysite.com/members/71.jpg'
[10] => ,'http://www.mysite.com/members/104.jpg','http://www.mysite.com/members/108.jpg','http://www.mysite.com/members/42.jpg','http://www.mysite.com/members/57.jpg','http://www.mysite.com/members/66.jpg','http://www.mysite.com/members/67.jpg','http://www.mysite.com/members/68.jpg','http://www.mysite.com/members/69.jpg','http://www.mysite.com/members/70.jpg','http://www.mysite.com/members/71.jpg','http://www.mysite.com/members/73.jpg'
[11] => ,'http://www.mysite.com/members/104.jpg','http://www.mysite.com/members/108.jpg','http://www.mysite.com/members/42.jpg','http://www.mysite.com/members/57.jpg','http://www.mysite.com/members/66.jpg','http://www.mysite.com/members/67.jpg','http://www.mysite.com/members/68.jpg','http://www.mysite.com/members/69.jpg','http://www.mysite.com/members/70.jpg','http://www.mysite.com/members/71.jpg','http://www.mysite.com/members/73.jpg','http://www.mysite.com/members/74.jpg'
[12] => ,'http://www.mysite.com/members/104.jpg','http://www.mysite.com/members/108.jpg','http://www.mysite.com/members/42.jpg','http://www.mysite.com/members/57.jpg','http://www.mysite.com/members/66.jpg','http://www.mysite.com/members/67.jpg','http://www.mysite.com/members/68.jpg','http://www.mysite.com/members/69.jpg','http://www.mysite.com/members/70.jpg','http://www.mysite.com/members/71.jpg','http://www.mysite.com/members/73.jpg','http://www.mysite.com/members/74.jpg','http://www.mysite.com/members/75.jpg'
[13] => ,'http://www.mysite.com/members/104.jpg','http://www.mysite.com/members/108.jpg','http://www.mysite.com/members/42.jpg','http://www.mysite.com/members/57.jpg','http://www.mysite.com/members/66.jpg','http://www.mysite.com/members/67.jpg','http://www.mysite.com/members/68.jpg','http://www.mysite.com/members/69.jpg','http://www.mysite.com/members/70.jpg','http://www.mysite.com/members/71.jpg','http://www.mysite.com/members/73.jpg','http://www.mysite.com/members/74.jpg','http://www.mysite.com/members/75.jpg','http://www.mysite.com/members/100.jpg'
[14] => ,'http://www.mysite.com/members/104.jpg','http://www.mysite.com/members/108.jpg','http://www.mysite.com/members/42.jpg','http://www.mysite.com/members/57.jpg','http://www.mysite.com/members/66.jpg','http://www.mysite.com/members/67.jpg','http://www.mysite.com/members/68.jpg','http://www.mysite.com/members/69.jpg','http://www.mysite.com/members/70.jpg','http://www.mysite.com/members/71.jpg','http://www.mysite.com/members/73.jpg','http://www.mysite.com/members/74.jpg','http://www.mysite.com/members/75.jpg','http://www.mysite.com/members/100.jpg','http://www.mysite.com/members/101.jpg'
[15] => ,'http://www.mysite.com/members/104.jpg','http://www.mysite.com/members/108.jpg','http://www.mysite.com/members/42.jpg','http://www.mysite.com/members/57.jpg','http://www.mysite.com/members/66.jpg','http://www.mysite.com/members/67.jpg','http://www.mysite.com/members/68.jpg','http://www.mysite.com/members/69.jpg','http://www.mysite.com/members/70.jpg','http://www.mysite.com/members/71.jpg','http://www.mysite.com/members/73.jpg','http://www.mysite.com/members/74.jpg','http://www.mysite.com/members/75.jpg','http://www.mysite.com/members/100.jpg','http://www.mysite.com/members/101.jpg','http://www.mysite.com/members/102.jpg'
[16] => ,'http://www.mysite.com/members/104.jpg','http://www.mysite.com/members/108.jpg','http://www.mysite.com/members/42.jpg','http://www.mysite.com/members/57.jpg','http://www.mysite.com/members/66.jpg','http://www.mysite.com/members/67.jpg','http://www.mysite.com/members/68.jpg','http://www.mysite.com/members/69.jpg','http://www.mysite.com/members/70.jpg','http://www.mysite.com/members/71.jpg','http://www.mysite.com/members/73.jpg','http://www.mysite.com/members/74.jpg','http://www.mysite.com/members/75.jpg','http://www.mysite.com/members/100.jpg','http://www.mysite.com/members/101.jpg','http://www.mysite.com/members/102.jpg','http://www.mysite.com/members/103.jpg'
[17] => ,'http://www.mysite.com/members/104.jpg','http://www.mysite.com/members/108.jpg','http://www.mysite.com/members/42.jpg','http://www.mysite.com/members/57.jpg','http://www.mysite.com/members/66.jpg','http://www.mysite.com/members/67.jpg','http://www.mysite.com/members/68.jpg','http://www.mysite.com/members/69.jpg','http://www.mysite.com/members/70.jpg','http://www.mysite.com/members/71.jpg','http://www.mysite.com/members/73.jpg','http://www.mysite.com/members/74.jpg','http://www.mysite.com/members/75.jpg','http://www.mysite.com/members/100.jpg','http://www.mysite.com/members/101.jpg','http://www.mysite.com/members/102.jpg','http://www.mysite.com/members/103.jpg','http://www.mysite.com/members/105.jpg'
[18] => ,'http://www.mysite.com/members/104.jpg','http://www.mysite.com/members/108.jpg','http://www.mysite.com/members/42.jpg','http://www.mysite.com/members/57.jpg','http://www.mysite.com/members/66.jpg','http://www.mysite.com/members/67.jpg','http://www.mysite.com/members/68.jpg','http://www.mysite.com/members/69.jpg','http://www.mysite.com/members/70.jpg','http://www.mysite.com/members/71.jpg','http://www.mysite.com/members/73.jpg','http://www.mysite.com/members/74.jpg','http://www.mysite.com/members/75.jpg','http://www.mysite.com/members/100.jpg','http://www.mysite.com/members/101.jpg','http://www.mysite.com/members/102.jpg','http://www.mysite.com/members/103.jpg','http://www.mysite.com/members/105.jpg','http://www.mysite.com/members/106.jpg'
)




so basically after i push the image names in there, i then pop the last element, then i trim it to get rid of the leading and trailing comma, then i explode that to get my final comma sep individual element array to use with each anchor which will include my lightbox rel


basically this goes in part with my other thread here, but i wanted to seperate this portion into a new thread becuse it is the most important part and i want to focus on getting the foundation correct.

http://www.codingforums.com/showthread.php?t=279440

where i am having to rewrite the page in order to use the feature of lightbox, what i am doing is i have decided to forgo the show image function on that page as well as the for each and take out both anchor tags and rewrite that whole section so that

1. it makes sense to me. That code on that other tread is so confusing.
2. its easire and has less complicated structure
2a. has one array in the end that i can reference both small image for the page as well as large image in one anchor tag when clicked it will use lightbox.

so first part of the rebuild is to build the appropriate array.

Fou-Lu
10-26-2012, 08:10 PM
$a = range(1, 50);
printf("'%s'" . PHP_EOL, implode("', '", $a));

Would result in:


'1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50'


The difference is I can see if 29 exists in this list without needing to iterate it.

You don't need to rebuild your array. You need to figure out what JS wants in the end and give it just that.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum