/    Sign up×
Community /Pin to ProfileBookmark

Passing arrays

Hi, can we pass array to other php file? Let say I have the script below and I need to pass the $a to myphp.php, how do we do that?

[CODE]
$a[]=”First”;
$a[]=”Second”;
$a[]=”Third”;
$total = count($a)
// I need to pass $a and $total to myphp.php
[/CODE]

Thanks in advance.

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@jinkasJun 03.2005 — Well, you don't really need to pass $total, since you can just calculate that in myphp.php. To pass the array, use http://us3.php.net/serialize and receive it in myphp.php with http://us3.php.net/unserialize However, if you are passing this publically (GET), make especially sure to do error/taint checking in myphp.php because the user can modify data in the array.

An alternative, if viable and if you aren't using GET, would be to make the array a session variable and work with the data through sessions.
Copy linkTweet thisAlerts:
@NogDogJun 03.2005 — Sessions would definitely be the cleanest way to do this, I think.

Script 1:
[code=php]
<?php
session_start();
$_SESSION['a'][] = "First";
$_SESSION['a'][] = "Second";
$_SESSION['a'][] = "Third";
?>
[/code]

Script 2:
[code=php]
<?php
session_start();
foreach($_SESSION['a'] as $key => $value)
{
echo "$key: $valuen";
}
echo "Total = " . count($_SESSION['a']);
?>
[/code]
×

Success!

Help @Ivan_SSY 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,
)...