+ 2

can anyone give me an example about second situation //$key=>$value

i dont understanding

9th Sep 2016, 12:13 PM
Raneem Mohamad
Raneem Mohamad - avatar
9 Answers
+ 5
// You can get current element's key. Example: <?php $names = array("John", "David", "Amy"); foreach ($names as $key => $value) { echo "value is ".$value.'<br />'; echo "key is ".$key.'<br />'; } ?> // output value is John key is 0 value is David key is 1 value is Amy key is 2
4th Dec 2016, 8:14 PM
Harsha Vardhan
Harsha Vardhan - avatar
+ 2
study arrays
9th Sep 2016, 9:43 PM
Hemant singh patwal
Hemant singh patwal - avatar
+ 2
$key would be the index of $value. e.g: $array = array("value1","value2"); foreach($array as $key=>$value) { echo $key."--".$value; } output: 0--value1 1--value2
26th Sep 2016, 5:44 AM
Alvaro Rojas
Alvaro Rojas - avatar
+ 1
Alvaro Rojas Sorry But there is a slight mistake by you in your code. You cannot use "array" as a variable $array. I think it is reserved word. Isn't it?
24th Nov 2016, 9:03 AM
BHAVUK JAIN
BHAVUK JAIN - avatar
+ 1
hieu
21st Dec 2016, 12:42 PM
Hieu Nguyen
Hieu Nguyen - avatar
0
You can get current element's key. Example: <?php $names = array("John", "David", "Amy"); foreach ($names as $key => $value) { echo $key.'<br />'; } ?>
19th Nov 2016, 2:24 PM
Christiano
Christiano - avatar
0
// Multidimensional associative array for learning purposes $multiarr = array( "Fruits" => array("Apples", "Pears", "Bananas", "Oranges"), "Cars" => array("BMW", "VW", "Toyota", "Audi"), "Cities" => array("New York", "London", "Toronto", "Sydney") ); // Looping through $multiarr which is a multidimensional array foreach($multiarr as $key => $subarrays) { // !!Printing $subarrays will produce error since $subarrays are arrays and not strings!! // We are printing the keys to use as headers. Keys can be associative or numeric echo "<h3>" . $key . "</h3>"; // Now we need to loop through arrays inside $multiarr to print out data. // No need for $key unless you need specific numeric info for further code foreach ($subarrays as $data) { echo $data . "<br>"; } } /* OUTPUT: Fruits: Apples Pears Bananas Oranges Cars BMW VW Toyota Audi Cities New York London Toronto Sydney */
11th Feb 2017, 1:46 PM
Willy
- 1
associative arrays
13th Sep 2016, 9:30 AM
Shashank Mishra
Shashank Mishra - avatar
- 1
if you use associative array eg array('name'=>30)
26th Sep 2016, 12:47 PM
satyam