0

I'm grabbing data out of an API, and every once in a while, a value comes back in one of the fields I'm capturing that has a comma in it, which is causing my results to be offset by 1 each time it happens.

after I've gotten the results from the API, I put it into $answerSearch, then I json_decode it.

$json = json_decode($answerSearch, true);
foreach ($json['Result']['a']['b'] as $i) {

  $y = "{$i['number']},{$i['Type']},{$i['c']},{$i['d']},{$i['e']}"; 
  $x = explode("," , $y);
  array_push($output, $x);
}

One of these {$i['number']} values may be "1234,123", and because I use comma as a delimiter, it's causing the entire array to screw up.

How do I tell PHP that the , in the middle of this string is part of the string and not a delimeter?

Update

I can't just change $x = explode("," , $y); to use another delimiter because ,, /, ;, etc are all used in the actual strings that are being returned, I was just using , as an example.

12
  • why not just change your delimiter to something the api wont return ~^ ? Commented Feb 18, 2015 at 20:50
  • 1
    That code is utterly pointless. $i is already an array, so why convert that array to a string, just to explode it back to an array? If you're doing this to copy only the keys you want, there's better ways of doing this... Commented Feb 18, 2015 at 20:51
  • actully skip y and x and write directly to an array Commented Feb 18, 2015 at 20:51
  • @MarcB, it's not pointless, it does exactly what I want it to. The original array that comes over is massive, and I'm looking to just take 5 fields out of it (number, type, c, d, and e), and this code does that. Commented Feb 18, 2015 at 20:53
  • 1
    "How do I tell PHP that the , in the middle of this string is part of the string and not a delimeter?" It's hard to say without knowing exactly what data you get, but if one comma is required and one not, use regex to identify one of the commas. "eg" Remove commas which are not next to a number (preg_replace/whatever). But this depends on how the data is formatted. A comma delimiter when you expect comma in the string is not an ideal approach. Can't you set another delimiter? Or pass the data from the API in a more useful structure - ie already in an array? Commented Feb 18, 2015 at 20:56

1 Answer 1

1

There is no for the conversion from array to string back to an array, to add the selected values to the $output array use:

$output[]=array({$i['number']},{$i['Type']},{$i['c']},{$i['d']},{$i['e']});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.