/    Sign up×
Community /Pin to ProfileBookmark

Fill a four position array and reset when reach to 4 elements

Hi

I need to fill an array of size 4 and reset the array when it reach the 4 elements it can have.

something like:
$ar = array($var);
if(count($ar)==4) reset($ar);

How can it be done ?

Thank´s in advance

to post a comment
PHP

10 Comments(s)

Copy linkTweet thisAlerts:
@Vectorman211Jul 24.2006 — What do you mean by "reset"? the reset() function simply returns the array pointer to the first index. Please explain in more detail.
Copy linkTweet thisAlerts:
@amrigoauthorJul 24.2006 — I want to show 4 diferents images on the website home page, i have four diferent images names, recorded on the database.

id | image | upload_date
---------------------


1 | ab |2006-07-01

2 | cd |2006-07-02

3 | ef | 2006-07-03

4 | gh |2006-07-04

I show the most recent image uploaded for the first time the page load and i have these three images that i never show when the page load.

I think i can have an array wich stores the unique_id of the last image loaded

So the query will not bring that image the next time the page load. As i have only four images when i reach four elements in my array the array can start at zero elements again

1º time page load = $ar = [1]

2º time page load = $ar = [1, 2]

3º time page load = $ar = [1, 2, 3]

4º time page load = $ar = [1, 2, 3, 4]

5º time page load = $ar = [1]
Copy linkTweet thisAlerts:
@Vectorman211Jul 24.2006 — You mean:

[code=php]
if (count($ar) >= 4){
unset($ar);
$ar=array();
}
[/code]


That would essentially start you over with a blank array.
Copy linkTweet thisAlerts:
@amrigoauthorJul 24.2006 — Yes that is it, to reset the array, thank´s !

How can i fill the array until 4? array_push ?

$MyArray = array_push($MyArray, $_Session['InitialImage']);

How do i add an element to the array ?

Each page load i will have from the query a diferent variable to add to the array (until 4 elements)
Copy linkTweet thisAlerts:
@Vectorman211Jul 24.2006 — It's as simple as this:

[code=php]
$MyArray[]=$_SESSION['InitialImage'];
[/code]


array_push() essentially does the same thing, this is a shortcut.
Copy linkTweet thisAlerts:
@amrigoauthorJul 24.2006 — I always get just one elemnt in the array with the code below what can be wrong ?

Thank´s in advance

<i>
</i>if (count($MyArray) &gt;= 4){
unset($MyArray);
$MyArray[] = $_SESSION['InitialImage'];
} else{
$MyArray[] = $_SESSION['InitialImage'];
}
print_r($MyArray);
Copy linkTweet thisAlerts:
@amrigoauthorJul 24.2006 — From the query i have the $InitialImage variable :

<i>
</i>setSession("InitialImage",$InitialImage);
$ar[] = getSession("InitialImage");
print_r($ar);


But i always have just one value in the array

setSession and getSession functions will handle the session manipulation.

How can i fix it ?
Copy linkTweet thisAlerts:
@Vectorman211Jul 24.2006 — the array should be a session variable as well. otherwise you lose it when the script execution ends.

[code=php]
if (count($_SESSION['MyArray']) >= 4){
unset($_SESSION['MyArray']);
$_SESSION['MyArray'][] = $_SESSION['InitialImage'];
} else{
$_SESSION['MyArray'][] = $_SESSION['InitialImage'];
}
print_r($_SESSION['MyArray']);
[/code]
Copy linkTweet thisAlerts:
@amrigoauthorJul 24.2006 — thank´s a lot now it is all working

Best regards
Copy linkTweet thisAlerts:
@Vectorman211Jul 24.2006 — My pleasure ?
×

Success!

Help @amrigo spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...