Jump to content

Recommended Posts

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);

?>

Link to comment
https://forums.phpfreaks.com/topic/185723-noob-help/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/185723-noob-help/#findComment-980665
Share on other sites

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);

Link to comment
https://forums.phpfreaks.com/topic/185723-noob-help/#findComment-980666
Share on other sites


<?

$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.

Link to comment
https://forums.phpfreaks.com/topic/185723-noob-help/#findComment-980670
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/185723-noob-help/#findComment-980676
Share on other sites

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";

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/185723-noob-help/#findComment-980678
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.