majortom84 Posted June 2, 2014 Share Posted June 2, 2014 How would you access the number 10 in the $arr array? <?php $val = ‘1.2.3.4.5.6.7.8.9.10.11.12’; $arr[] = array(‘a’=>explode(‘.’,$val)); ?> Link to comment https://forums.phpfreaks.com/topic/288947-access-array-element/ Share on other sites More sharing options...
boompa Posted June 2, 2014 Share Posted June 2, 2014 Looks like a test or homework question. Can you figure it out by adding the line I put there? <?php $val = ‘1.2.3.4.5.6.7.8.9.10.11.12’; $arr[] = array(‘a’=>explode(‘.’,$val)); print_r($arr); ?> Link to comment https://forums.phpfreaks.com/topic/288947-access-array-element/#findComment-1481700 Share on other sites More sharing options...
Barand Posted June 2, 2014 Share Posted June 2, 2014 First of all, use the correct quotes, your editor is using "smart quotes" and not ordinary single quotes. Then as Boompa suggested you should get Array ( [0] => Array ( [a] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 [8] => 9 [9] => 10 [10] => 11 [11] => 12 ) ) ) Now you can see the indexes you need to provide to access "10" Link to comment https://forums.phpfreaks.com/topic/288947-access-array-element/#findComment-1481701 Share on other sites More sharing options...
majortom84 Posted June 2, 2014 Author Share Posted June 2, 2014 ah I see so.... $arr[9] would be this element Link to comment https://forums.phpfreaks.com/topic/288947-access-array-element/#findComment-1481708 Share on other sites More sharing options...
Jacques1 Posted June 3, 2014 Share Posted June 3, 2014 No. Please look at the structure: You have three nested arrays. $arr has a single element, namely an associative array at index 0. This associative array again has a single element, namely a numerical array at key "a". And this numerical array finally contains the numbers. The number 10 is stored in $arr[0]['a'][9] Link to comment https://forums.phpfreaks.com/topic/288947-access-array-element/#findComment-1481713 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.