Page 1 of 1

Problem in reading from Array in 'foreach'

Posted: Fri Apr 09, 2010 5:37 pm
by Shamma009
What exactly where you trying to do here?

Code: Select all

		array ($finaltot[$j] = $row);

Re: Problem in reading from Array in 'foreach'

Posted: Fri Apr 09, 2010 5:52 pm
by requinix
Whoops, looks like someone deleted a post.

Code: Select all

$final =  $value* $totElectrici;
$value is an array. You can't multiply an array by a number.

Code: Select all

$finaltot[$j] = $row
That bit needs to be slightly different.

Re: Problem in reading from Array in 'foreach'

Posted: Sat Apr 10, 2010 3:56 am
by Shamma009
how can I get the value out of the array ?

Re: Problem in reading from Array in 'foreach'

Posted: Sat Apr 10, 2010 11:59 am
by minorDemocritus
Shamma009 wrote:how can I get the value out of the array ?
2 ways. If the array is keyed by names, you can reference by the key name or key index:

Code: Select all

$userInfo = array('name' => 'john', 'birthday' => '1985-08-23', 'town' => 'jacksonville');
$name = $userInfo['name'];
$birthday = $userInfo['birthday'];
// $name and $birthday will be the same if you do it this way:
$name = $userInfo[0];
$birthday = $userInfo[1];
If the array is keyed by integers, you have to reference by the key index:

Code: Select all

$errorMessages = array('Invalid username or password', 'Email address is not valid', 'Your session has expired, please log in again.');
$firstError = $errorMessages[0];
$secondError = $errorMessages[1];
foreach ($errorMessage as $error) {
    echo 'The error received was: ' . $error;
}
More, very helpful information is available here... I STRONG RECOMMEND reading the whole thing, since there are a few gotchas to be careful of with arrays: http://php.net/manual/en/language.types.array.php.