ProNoob13 Posted March 22, 2012 Share Posted March 22, 2012 So, I wrote a little function the other day to fetch an array of elements out of character-separated-data (like "value1|value2|value3", but then fetched into an array using explode()). The only thing is just that it isn't working at all[/]. Every time I run the function, and catch it as an variable to use it as an array, it simply outputs the word "media", with each character separated into one array-element. Could anybody figure out what's wrong with my code? (Aside from my style, I know it's horrible. I'm halfway learning OOP) Any help would be greatly appreciated. if(!function_exists('getText')) { function getText($item) { $text = explode(" | ", $data["text"][$item]); $text = Array(0=>"")+$text; return Array(1=>$data["text"][$item]); //outputs Array(0=>"", "m", "e", "d", "i", "a") } } 17852_.zip Quote Link to comment https://forums.phpfreaks.com/topic/259499-weird-explode-error/ Share on other sites More sharing options...
.josh Posted March 22, 2012 Share Posted March 22, 2012 $data does not exist within your getText() function. You either need to pass it as an argument or declare it as a globally scoped variable within your function. Quote Link to comment https://forums.phpfreaks.com/topic/259499-weird-explode-error/#findComment-1330223 Share on other sites More sharing options...
Muddy_Funster Posted March 22, 2012 Share Posted March 22, 2012 This works for me: <?php if(!function_exists('getMyText')) { function getMyText($item) { $text = explode(" | ", $item); return $text; //outputs array(4) { [0]=> string(5) "hello" [1]=> string(3) "out" [2]=> string(5) "there" [3]=> string( "everyone" } } } $m = getMyText("hello | out | there | everyone"); var_dump($m); ?> I changed the function name because getText is already a PHP function name : http://uk3.php.net/manual/en/function.gettext.php whych is why I assume you are using the if(!function_exists(...)). Please try to avoid any atempts to overwrite php stored functions at runtime, nothing good will come from it. Quote Link to comment https://forums.phpfreaks.com/topic/259499-weird-explode-error/#findComment-1330226 Share on other sites More sharing options...
ProNoob13 Posted March 22, 2012 Author Share Posted March 22, 2012 This works for me: <?php if(!function_exists('getMyText')) { function getMyText($item) { $text = explode(" | ", $item); return $text; //outputs array(4) { [0]=> string(5) "hello" [1]=> string(3) "out" [2]=> string(5) "there" [3]=> string( "everyone" } } } $m = getMyText("hello | out | there | everyone"); var_dump($m); ?> I changed the function name because getText is already a PHP function name : http://uk3.php.net/manual/en/function.gettext.php whych is why I assume you are using the if(!function_exists(...)). Please try to avoid any atempts to overwrite php stored functions at runtime, nothing good will come from it. That has done the trick. Thank you so much! I didn't notice that the function already existed, and, indeed, the function_exists was a override for the error it threw up when I tried to use getText as my own function. Still, thanks a bunch! Quote Link to comment https://forums.phpfreaks.com/topic/259499-weird-explode-error/#findComment-1330229 Share on other sites More sharing options...
Muddy_Funster Posted March 22, 2012 Share Posted March 22, 2012 no problem, glad I could help. Just remember - error messages are there for a reason Quote Link to comment https://forums.phpfreaks.com/topic/259499-weird-explode-error/#findComment-1330232 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.