5

I am not sure if this is doable or if its a bigger problem with in my code but I have a webpage where a user selects some values from a infinite amount. So for example there select a1 and b2 from my form.

The form then combines them so they become like this: a1b1 or a1b1d1e1 as another example. I will then split these values up using an explode function. here is an fulll example

$v1 = a1**b1
$v2 = explode("**", $v1);

Now if the user only select one value, the explode doesnt seem to work as I dont get a value for $v2. EG

$v1 = a1
$v2 = explode("**", $v1);

Is there a way that I can get this to work? Im not sure why I don't even get a value coming out in the second example, just want to knwo if its a limitation with explode or wheter it may be something else.

Thank in advance for an help or guidance.

Ian

snookian
  • 863
  • 6
  • 13
  • 35
  • do you want `$v2` to always equal an array, even if there is only one result? – Chris Nov 10 '15 at 16:59
  • 2
    The explode will still work, it'll simply return an array with a single element - https://3v4l.org/HGlVB – Mark Baker Nov 10 '15 at 17:00
  • Not true. Explode will return an empty array set... – Chris Nov 10 '15 at 17:00
  • Do I get this right, that you get that data from a client side form and want to convert it to an array to work with it on the server side? _Why?_ Simply hand it over as an array and all is good... – arkascha Nov 10 '15 at 17:01
  • @Chris - only if $v1 is actually empty.... if it contains any value, then it will be a single array element containing that value – Mark Baker Nov 10 '15 at 17:01
  • Hi @Chris, yeah I guess i need it to return an array. Basically my code after this bit refers to $v2[0], $v2[1] and so on for however many things get exploded if that makes sense – snookian Nov 10 '15 at 17:02
  • Gotcha. The docs state it will return an empty array if the delimiter is not found... – Chris Nov 10 '15 at 17:02
  • why don't you just use square brackets `[]` on your `select`'s `name` attribute like `name="inputname[]"`? it will produce array and you no more need to `explode()` the submitted data. – Ari Nov 10 '15 at 17:14

1 Answers1

5

Sorry, not sure what the actual issue here is...

First:

<?php
var_dump(explode('**','A'));

This clearly shows that the explode() call works as expected, the output is:

array(1) {
  [0] =>
  string(1) "A"
}

Second:

If I get that right, then you get that data from a client side form where you invest effort into creating a string and then want to convert that into an array on the server side to work with it? Why? Simply hand it over as an array and all is good:

You can simply use a notation like myvalue[] on the client side and the form submission will result in an array $_POST['myvalue'] getting prepared on the server side you can use like any other array without the need to explode something first.

For ajax requests this is even easier, since you then will typically use JSON as transport format in your POST requests which supports arrays natively.

arkascha
  • 41,620
  • 7
  • 58
  • 90