0

So I return JSON data and now I need to parse data from image I provided so I can post it in my database.

enter image description here

I need to parse it so every letter-number combination (A1, A2, A3 etc.) can be in separate column row...

 public function generateSeats(SeatMap $seatMap)
{
    $layout = $seatMap->getSeatLayout();

    $layoutArray = json_decode($layout, true);

    foreach($layoutArray as $result)
    {
        dump($result);die;
        }

}

I don't know how to select every letter-number combination individually.

2 Answers 2

2

It's not so complicated.

You need to do only two steps:

$result = 'A1A2A3_A4A5A6';

1) Remove the underscore sign _:

$result = str_replace('_', '', $result);
// Now $result is string 'A1A2A3A4A5A6'

2) Split the entire string by two characters long:

$result = str_split($result, 2);
// Now $result is array('A1', 'A2', 'A3', 'A4', 'A5', 'A6')
Sign up to request clarification or add additional context in comments.

3 Comments

While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. A good answer will always have an explanation of what was done and why it was done in such a manner, not only for the OP but for future visitors to SO.
Is it better now?
Thank you so much!
0

Sergej's answer definitely solves the problem and is useful. I have up-voted it too. However, it's still not capable of handling the following:

  • Invalid seat number pattern LetterNumber. Ex: 1A
  • Invalid sequence of seats. Ex: 1AA1A2A3A4A5A6AZ

If you go with simple regex and just consider the matched groups, you are saved from validating seat numbers further.

preg_match_all ('/[A-Za-z][0-9]/', '1AA1A2A3A4A5A6AZ', $matches);
print_r($matches); 
// output: Array([0]=>Array([0]=>A1[1]=>A2[2]=>A3[3]=> A4[4]=>A5[5]=>A6))

3 Comments

So this definitely solves my problem. Thanks a lot! Just one question.. How can I make php to recognize second row to get B letter instead of A and so on?
you don't have to do that. Your API will return you the rows, thereafter it does not matter which row it is. Only the input (row seats) will change. Rest of it will be handled by regex. If I have not understood you, please show how API returns row wise data? Even if it returns A1A2B1B2, what's the problem?
I am satisfied with the solution you provided for me. Thank you.

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.