iwato Posted September 11, 2010 Share Posted September 11, 2010 BACKGROUND: The following code is designed to find all of the pre-defined constants that begin with the same substring. What it is able to achieve is to find only the first constant containing the substring. While attempting to discover a way to remedy this problem I stumbled on the following riddle that I would like you to help me solve.Under normal circumstances a single foreach statement of the following structureforeach ($array as $key => $value) { echo $value;}returns only the key-name of the nested subarray and the value array.In contrast, following code returns the full key-value pairs for all elements of both the array and subarrays generated by the get_defined_constants() function.foreach (get_defined_constants() as $key => $value) { echo $key . ': ' . $value . '<br />';}Can you explain the above contradiction? <?php function returnConstants($prefix) { foreach (get_defined_constants() as $key=>$value) { if (substr($key,0,strlen($prefix))==$prefix) { $dump[$key] = $value; if(empty($dump)) { return "Error: No Constants found with prefix '". $prefix . "'"; } else { return $dump; } } } }?> Roddy Link to comment Share on other sites More sharing options...
justsomeguy Posted September 11, 2010 Share Posted September 11, 2010 I don't understand what you think the contradiction is. Both loops do the same thing, they loop over an array and set $key and $value to the keys and values in the array.The function you posted stops after the first one because the return statement is inside the loop instead of outside. Link to comment Share on other sites More sharing options...
iwato Posted September 11, 2010 Author Share Posted September 11, 2010 I don't understand what you think the contradiction is. Both loops do the same thing, they loop over an array and set $key and $value to the keys and values in the array.The contradiction is that the second loop does what the following should do:foreach (get_defined_constants() as $key => $value) { foreach($value as $constant name => $constant_value) echo $constant_name . ': ' . $constant_value . '<br />'; }} Once again, in order to get at the value of an element of a subarray an additional foreach loop is required. This is not so when the array is the one generated by the get_defined_constants() function.Roddy Link to comment Share on other sites More sharing options...
boen_robot Posted September 11, 2010 Share Posted September 11, 2010 My tests confirm otherwise, i.e. what the manual suggests: By default, the array returned by get_defined_constants() is one dimensional, so a single foreach loop should echo all constants and their values.If you supply the parameter to true, it should return a two dimensional array instead... this is testing on PHP 5.3.2 VC6 Thread Safe, Windows... if without the parameter you get the two dimensional version, you've found a bug on PHP's MAC version. Link to comment Share on other sites More sharing options...
iwato Posted September 12, 2010 Author Share Posted September 12, 2010 My tests confirm otherwise, i.e. what the manual suggests: By default, the array returned by get_defined_constants() is one dimensional, so a single foreach loop should echo all constants and their values.If you supply the parameter to true, it should return a two dimensional array instead... this is testing on PHP 5.3.2 VC6 Thread Safe, Windows... if without the parameter you get the two dimensional version, you've found a bug on PHP's MAC version.We read the same passage, but a phpversion() function call informed me that my version of PHP is 5.2.6. I have now performed the following two experiments and obtained the results indicated:<h3>The key-value Pairs Generated by a foreach-Statement with $categorize Set to True</h3> <?php foreach (get_defined_constants(true) as $key => $value) { echo $key . ': ' . $value . '<br />'; } ?> Result: A list of subarrays with their names listed as keys and their values as array. No constants and no constant values appeared. <h3>The key-value Pairs Generated by a foreach-Statement with $categorize Not Set</h3> <?php foreach (get_defined_constants() as $key => $value) { echo $key . ': ' . $value . '<br />'; } ?> Result: A very long uninterrupted list of constants and their respective value.In short, no difference between PHP Version 5.2.6 (on Mac) and 5.3.2 (on Windows).As always, many thanks.Now, I have only to take justsomeguys suggestion and fix the way my loop functions.Have a great day!Roddy Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.