/    Sign up×
Community /Pin to ProfileBookmark

call an array value that is outside current function

I’m trying to compare an array value within a function. The problem is, the array is outside the function and it doesn’t recognize the value. I think I can put $_server or $_get in front of $colors, but I can’t find any good examples. Can someone help me with my script below.

<?php
$doQuery = mysql_query(“SELECT data FROM usersettings WHERE user = ‘$friendID1′”)
or die(mysql_error());

$colors=mysql_fetch_array($doQuery);

[COLOR=”Red”]$colors[/COLOR]=unserialize($colors[‘data’]);

?>
————————————————————————

<?php
function Fetcher3()
{
echo [COLOR=”Red”]$colors[/COLOR][‘randompicwidth’];
}
Fetcher3();
?>

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@stephan_gerlachAug 24.2007 — How about this?

[code=php]

<?php
$doQuery = mysql_query("SELECT data FROM usersettings WHERE user = '$friendID1'")
or die(mysql_error());

$colors=mysql_fetch_array($doQuery);

$colors=unserialize($colors['data']);
?>
------------------------------------------------------------------------
<?php
function Fetcher3($colors)
{
echo $colors['randompicwidth'];
}
Fetcher3($colors);
?>



[/code]
Copy linkTweet thisAlerts:
@lehulaauthorAug 24.2007 — the problem is, i have many values from the array I want to grab, not just that one. Probably about 60+ values. So putting that wouldn't be practical. thanks though
Copy linkTweet thisAlerts:
@artemisAug 24.2007 — [code=php]
$doQuery = mysql_query("SELECT data FROM usersettings WHERE user = '$friendID1'")
or die(mysql_error());

$colors=mysql_fetch_array($doQuery);

$colors=unserialize($colors['data']);

------------------------------------------------------------------------

function Fetcher3(){
global $colors; # Add this line
echo $colors['randompicwidth'];
}
Fetcher3();
[/code]
Copy linkTweet thisAlerts:
@lehulaauthorAug 24.2007 — awsome, thankyou
Copy linkTweet thisAlerts:
@NogDogAug 24.2007 — While the global solution will work, it can be a maintenance nightmare. I would recommend simply supplying the array as a parameter of the function:
[code=php]
function Fetcher3($colors){
echo $colors['randompicwidth'];
}
Fetcher3($colors);
[/code]
×

Success!

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