0

I'm currently developing an API using Laravel for my project, the concept:

  1. Retrieve JSON data from MySQL.
  2. Receive user input from the Front-end (string).
  3. Convert both JSON and string input into an array with similar structure. The array structure here is basically ["ObjectA", "ObjectA_quantity", "ObjectB", "ObjectB_quantity", ...].
  4. Basically, eliminate the quantity of every object of Database's Array, based on every object that User Input's Array got. For example, if the Database's Array got ["pizza", "1", "burger", "2"], and the User Input's Array got ["pizza", "1"], the output of the method is expected to be ["burger", "2"].

The method that I developed will give inconsistent and confusing output, like for some object, it works well, for other it doesn't eliminate anything and if the User Input's Array too big (> 1 object), it also doesn't eliminate anything. I really welcome different approach or anything else that will give the expected output as above. Thank you very much

Here's the source code of the method I've develop: (method's located on: else if ($transactionGetter->type == 'return'), Line 148 and so forth)

https://github.com/andre-nk23/packme-backend/blob/master/app/Http/Controllers/API/TransactionController.php

1 Answer 1

1

if it's a JSON you must decode the value before access

$transactionGetter=json_decode($transactionGetter);
Sign up to request clarification or add additional context in comments.

2 Comments

Yes I have, if you read the source code, I have decode and encode the JSON back and it's working well, but there's some error that I don't understand why does it happened
I have mentioned above, the program is working well only on some object. Like for example the array that I want to eliminate is: ["Pizza", "2", Burger, "1"] and the User Input : ["Pizza", 2], for this case it could be work (["Burger", "1"]). But for other case if the User Input let say ["Burger', "1"], the array is not going to be eliminated at all. So, it works on some array element, but not with the other. Also when the User Input array is too long, it's not gonna work.

Your Answer

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