MetalSmith Posted December 19, 2009 Share Posted December 19, 2009 Hi all! How can I get the result of $c out of this function? I know the code is odd looking but it does mean something I just need result $c. Thanks! <?php $extensions_file = (file("extensions_additional.conf")); function search_array($value) { $a = "exten => 56201,n,GotoIf(\$[x\${PIN} = x"; $b = substr($value,0,37); if ($a == $b) { $c = 56201; return ($c); } } #array_walk($extensions_file,"search_array"); echo ($c); ?> Quote Link to comment https://forums.phpfreaks.com/topic/185723-noob-help/ Share on other sites More sharing options...
George Botley Posted December 19, 2009 Share Posted December 19, 2009 Could you try calling the function within an echo.. Try <? echo search_array($value); ?> Ensuring you pass the value of $value where $value is within the call.. whether this is within a POST variable or otherwise. Quote Link to comment https://forums.phpfreaks.com/topic/185723-noob-help/#findComment-980656 Share on other sites More sharing options...
MetalSmith Posted December 19, 2009 Author Share Posted December 19, 2009 That is not working either. Quote Link to comment https://forums.phpfreaks.com/topic/185723-noob-help/#findComment-980658 Share on other sites More sharing options...
teamatomic Posted December 19, 2009 Share Posted December 19, 2009 Maybe you have an extra set of parens in line 2 and unmatched parens in line 5? HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/185723-noob-help/#findComment-980661 Share on other sites More sharing options...
MetalSmith Posted December 19, 2009 Author Share Posted December 19, 2009 The string comparison does work becuase if I put echo $c; after $c = 56201; I see 56201 on my screen. So for some reason I am not extracting the result $c out of that function correctly. Quote Link to comment https://forums.phpfreaks.com/topic/185723-noob-help/#findComment-980662 Share on other sites More sharing options...
George Botley Posted December 19, 2009 Share Posted December 19, 2009 I don't understand why this isn't working for you, but when I write functions I return a value without any brackets.... Could this be why? - I wouldn't know as I don't write functions with brackets, I write them echoing values that I then call in later. Quote Link to comment https://forums.phpfreaks.com/topic/185723-noob-help/#findComment-980665 Share on other sites More sharing options...
MetalSmith Posted December 19, 2009 Author Share Posted December 19, 2009 I simplified things for testing. Where am I going wrong with getting $c out of this function? It keeps returning the number 1 $x = array("a"=>"Dog","b"=>"Cat","c"=>"Horse"); function search_array($value) { $a = "Horse"; $b = substr($value,0,5); if ($a == $b) { $c = 56201; echo ($c); return ($c); } } $d = array_walk($x,"search_array"); echo ($d); Quote Link to comment https://forums.phpfreaks.com/topic/185723-noob-help/#findComment-980666 Share on other sites More sharing options...
George Botley Posted December 19, 2009 Share Posted December 19, 2009 <? $x = array("a"=>"Dog","b"=>"Cat","c"=>"Horse"); function search_array($value) { $a = "Horse"; $b = "Horse"; if ($a == $b) { $c = 56201; return $c; } } $d = array_walk($x,"search_array"); echo "$d"; $return = search_array($value); echo "$return"; ?> Take a look a that adaptation, I have tweaked a few things for testing purposes, removed all brackets over echos and returns... Re-add your substr strings back in to this new code and see if it still works, you have to ensure you are setting the value of $value in your script somewhere. Quote Link to comment https://forums.phpfreaks.com/topic/185723-noob-help/#findComment-980670 Share on other sites More sharing options...
MetalSmith Posted December 20, 2009 Author Share Posted December 20, 2009 Well that is not giving me what I want either lol. All I want is to walk through each array value and compare the value with a string. If they are equal then give me a result. Maybe the array_walk is not the right choice. Functions are hard to understand for me for some reason. Quote Link to comment https://forums.phpfreaks.com/topic/185723-noob-help/#findComment-980676 Share on other sites More sharing options...
George Botley Posted December 20, 2009 Share Posted December 20, 2009 Sorry for not being of help, I neglected to understand the issue being with the walk function. I can't help you there i'm afraid as I have not used this function in the past. Quote Link to comment https://forums.phpfreaks.com/topic/185723-noob-help/#findComment-980677 Share on other sites More sharing options...
MetalSmith Posted December 20, 2009 Author Share Posted December 20, 2009 Ok I used you code and got it to work with replacing $value with $d Is $return a global variable? Hows does $return spit out $c? $x = array("a"=>"Dog","b"=>"Cat","c"=>"Horse"); function search_array($value) { $a = "Horse"; $b = "Horse"; if ($a == $b) { $c = 56201; return $c; } } $d = array_walk($x,"search_array"); $return = search_array($d); echo "$return"; Quote Link to comment https://forums.phpfreaks.com/topic/185723-noob-help/#findComment-980678 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.