I have seen this line:
I know that, to access a Class object's datamembers or methods, we would be using "->" . But what does this "=>" means ? :confused:Code:foreach($result as $key=>$val)
Thanks :wave:
Printable View
I have seen this line:
I know that, to access a Class object's datamembers or methods, we would be using "->" . But what does this "=>" means ? :confused:Code:foreach($result as $key=>$val)
Thanks :wave:
isn't it a separator used in associative arrays? :confused:
i find so, after google it.
i mean:
Code:<?php
$ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34);
foreach($ages as $key=>$val)
{
echo $key;
}
?>
Thanks :)
:wave:
It's not specifically used for associative arrays, but simply for describing a key (or index) and value pair. It's also used in the foreach statement. Here are some examples:
PHP Code:// Associative array representing one book:
// In this case, the key represents a field of data, and the value represents the field's value
$book = array(
"author" => "Richard Dawkins",
"title" => "The God Delusion",
"isbn" => "0618680004",
"year" => 2006
);
// Numeric array representing multiple books
// In this case, the key represents a book ID, and the value represents the book
$books = array(
0 => $book,
);
// Foreach
foreach($books as $index => $book){
echo "On book #{$index} in collection.\r\n";
echo "Book data:\r\n";
foreach($book as $key => $value){
echo "{$key}: {$value}\r\n";
}
}
Thanks :wave: