/    Sign up×
Community /Pin to ProfileBookmark

How to store values in variables from ARRAY scripts?

How to store values from ARRAY scripts like:
$output[$key][“words”] = count($array);

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@SyCoJan 05.2009 — You can write them to a file, save them to a database or echo them out and write them on a bit of paper.

Perhaps you can provide more information about what you are trying to do.
Copy linkTweet thisAlerts:
@toplisekauthorJan 05.2009 — I have gather some array values and need to store them in variables each value.

It is actually simple equestion if you can and maybe help.Each Array value should be separated and stored in variable like:

$words1='...array values';

$words2

...
Copy linkTweet thisAlerts:
@SyCoJan 05.2009 — As I'm not sure how much PHP you know, I thought you should know this is not a usual thing to do in PHP. The reason is each value in an array can already be accessed as an individual variable.
[code=php]
$arr[]='zero';
$arr[]='one';
$arr[]='two';
$arr['foo']='three';
$arr['bar']='four';

//print the array like this
print_r($arr);

//Or just a single element in the array
echo $arr[2];[/code]


Renaming variable for no good reason doesn't make sense and leads to confusion.

You see people doing a similar thing with POST vars all the time.
[code=php]$name=$_POST['name'];[/code]
Which is entirely pointless and you have to refer to this useless rename to find out where the value of $name came from. You also lose the superglobal power of the POST array.
[code=php]$name=trim($_POST['name']);[/code]
[I]Does[/I] make sense as the $name and POST['name'] may be different.

Simply renaming or reassigning variables, without actually doing something to them, is not a good thing. If you change an array to a normal variable and you loose the array key, which is also often a useful reference to the value. You change the nature of the container of the value and this will lead to confusion in more complex scripts.
Copy linkTweet thisAlerts:
@toplisekauthorJan 05.2009 — Hi,

thank you.

,,if you change an array to a normal variable,you loose the array key''

I do not like to lose array Key.

Is this in correct way?

Values in Arrays:
[code=php]
$output[$key]["words"] = count($array);
[/code]


to be shown in echo for words:
[code=php]
echo $output[2];
[/code]


and echo this Key as:
[code=php]
echo $output[1];
[/code]
Copy linkTweet thisAlerts:
@SyCoJan 05.2009 — [code=php]
//this is a number of elements in the array
count($array);

// you can assing that value to an array element
$output[$key]["words"] = count($array);

//this will now echo the same number as count($array);
echo $output[$key]["words"];

[/code]


$output[$key] is an array. It currently only has one element 'words'. You can't simply echo $output[$key] or you just get 'Array'. You can echo $output[$key]['words'] to get the value.


You can foreach() an array to access the values too and you can foreach a multi dimensional array as well. Just foreach each level of the array. So to access the values in the array you can do this.

[code=php]//assuming you have the value of $arraykey at this point
foreach($output[$arraykey] as $key => $value){
echo $value;
}

//or like this if you don't know the value of the key
//notice $output now has no key
foreach($output as $key => $value){ //value is an array
foreach($valueas $key2 => $value2){//value 2 is a normal variable
echo $value2;
}
}
[/code]



If you don't know how many levels a multi dimensional array has you can test the value with is_array() and call a recursive function to loop again until the value is not an array.


to be shown in echo for words

:
[code=php]
echo $output[2];
[/code]


and echo this Key as:
[code=php]
echo $output[1];
[/code]
×

Success!

Help @toplisek 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 6.17,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...