@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.
@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]