robkar32 Posted September 12, 2016 Share Posted September 12, 2016 (edited) <?php function func($text) { $array = explode(' ', $text); for($i = 0; $i < count($array, COUNT_RECURSIVE); $i++){ $output = substr($array[$i], strpos($array[$i], "=") + 1); echo $output, '<br>'; } } func("arg1=Test1 arg2=test1 arg3=test3"); ?> So this code obviously outputs test1, test2 and test3, which i want it to. Now how do i get the arg's as well? This is what i want to do: if(exists('arg1' || 'arg2' || 'arg3') { echo "args exists"; } else { echo "args don't exist"; } Hope you understand, thanks! Edited September 12, 2016 by robkar32 Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted September 12, 2016 Share Posted September 12, 2016 Where does this weird input string come from? Why not use a proper data format like JSON? 1 Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted September 12, 2016 Share Posted September 12, 2016 Now how do i get the arg's as well? You could use explode() again. <?php function func($text) { $array = explode(' ', $text); foreach($array as $currArg){ $currArg = explode('=', $currArg); print '<pre>' . print_r($currArg, true) . '</pre>'; } } func("arg1=Test1 arg2=test1 arg3=test3"); ?> Quote Link to comment Share on other sites More sharing options...
maxxd Posted September 12, 2016 Share Posted September 12, 2016 Or you can use func_get_args() and not force the function parameter to be a randomly formatted string. Or, pass in an array of values and simply loop through them. Point being, unless you're tied to this data format for the function parameter, there are far easier ways to handle the situation. 1 Quote Link to comment Share on other sites More sharing options...
ginerjm Posted September 12, 2016 Share Posted September 12, 2016 Despite the previous concerns about why you are using this strange data format, if you simply stop using the substr function on the input you will get the arg values as well. Of course that is too easy so you muat want something else, but you didn't make that clear in your question. Quote Link to comment Share on other sites More sharing options...
robkar32 Posted September 12, 2016 Author Share Posted September 12, 2016 (edited) Despite the previous concerns about why you are using this strange data format, if you simply stop using the substr function on the input you will get the arg values as well. Of course that is too easy so you muat want something else, but you didn't make that clear in your question. Yes. I just want to split up the arg values, and the values assigned to them, or the "test" values in different arrays and know where they are. Only thought of a way to get the values, and not the args. Yeah, i'm pretty new to this, and by the way i am not using this code for something big that i even need to worry about the data format. Edited September 12, 2016 by robkar32 Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted September 12, 2016 Share Posted September 12, 2016 With a proper data format, this would be a 30-seconds job and not take 6 hours (and counting). So maybe you should care about your decisions. If you're interested in learning PHP, I'm sure we can figure out a more reasonable approach after you've described the actual problem (where does the data come from etc.). Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted September 12, 2016 Share Posted September 12, 2016 Yes. I just want to split up the arg values, and the values assigned to them, or the "test" values in different arrays and know where they are. Only thought of a way to get the values, and not the args. Yeah, i'm pretty new to this, and by the way i am not using this code for something big that i even need to worry about the data format. If you haven't already, you could try Reply #3. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted September 12, 2016 Share Posted September 12, 2016 You have the 'arg' value and the true value ('test') available to you in your function. If you can't figure out what to do from there, you need to stop working on this and go back to the book and do some learning. Quote Link to comment 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.