0

After several hours of Picasso coding (my term), I am stuck on how to efficiently convert this JavaScript array of objects into PHP (sent via AJAX).

In JavaScript, the array of objects looks like this after JSON encoding:

[
    {"pid":"282","seller_id":"3","qty":"5"},
    {"pid":"284","sn":"1234","seller_id":2,"qty":"1"},
    {"pid":"284","sn":"2345","seller_id":2,"qty":"1"},
    {"pid":"284","sn":"3456","seller_id":2,"qty":"1"},
    {"pid":"111","sn":"987","seller_id":2,"qty":"1"}
]

There are two data groupings - note that the first transfer lacks a serial number and is to an external vendor, making it a sale. The next 4 are all to "myself" (user is seller_id: 2), so these are internal transfers of product.

Anyway, the two types must be handled differently, so it would be immensely helpful if they were in separate arrays.

Therefore, I wish to end up with two PHP arrays:

arrSale[0] = "pid":"282","seller_id":"3","qty":"5"

and

arrXfer[0] = "pid":"284","sn":"1234","seller_id":2,"qty":"1"
arrXfer[1] = "pid":"284","sn":"2345","seller_id":2,"qty":"1"
arrXfer[2] = "pid":"284","sn":"3456","seller_id":2,"qty":"1"
arrXfer[3] = "pid":"111","sn":"987","seller_id":2,"qty":"1"

I know that I must use $arrPHP = json_decode(jsonItems); to decode the JSON, but what happens to the objects? How would I break them into the two groups? I can't even find website resources that discuss this.

1
  • 1
    loop over the array in either language and check if a row has sn property or not Commented Jan 23, 2015 at 0:42

1 Answer 1

2

Try this

$JSON = '[
    {"pid":"282","seller_id":"3","qty":"5"},
    {"pid":"284","sn":"1234","seller_id":2,"qty":"1"},
    {"pid":"284","sn":"2345","seller_id":2,"qty":"1"},
    {"pid":"284","sn":"3456","seller_id":2,"qty":"1"},
    {"pid":"111","sn":"987","seller_id":2,"qty":"1"}
]';

$array = json_decode($JSON);

$arrSale = $arrXfer = array();

foreach($array as $obj)
    if(isset($obj->sn)) $arrXfer[] = (array)$obj;
        else $arrSale[] = (array)$obj;

print_r($arrSale);
print_r($arrXfer);

It gives us:

Array(
    [0] => Array([pid] => 282[seller_id] => 3[qty] => 5)
)

Array(
    [0] => Array([pid] => 284[sn] => 1234[seller_id] => 2[qty] => 1)
    [1] => Array([pid] => 284[sn] => 2345[seller_id] => 2[qty] => 1)  
    [2] => Array([pid] => 284[sn] => 3456[seller_id] => 2[qty] => 1)  
    [3] => Array([pid] => 111[sn] => 987[seller_id] => 2[qty] => 1)  
)
7
  • I don't mind! Take good care of it, please! It likes cream cheese Commented Jan 23, 2015 at 0:54
  • I'm usually a procedural guy, but getting some odd results which might have to do with the PHP ver on my host? Got error syntax error, unexpected "[" on line 2, but it worked after change to $arrSale = $arrXfer = array(); That is just a poss symptom. Prob is here: At end of code block, I put: $xcnt = count($arrXfer); $scnt = count($arrSale); and echoed result, but both were zero..? (Used same data as above) Commented Jan 23, 2015 at 1:01
  • 1
    That's because of your PHP version. Square brackets array notation added at version 5.4, before that only array(...) worked. Commented Jan 23, 2015 at 1:05
  • Zero? Do var_dump($array) after the json_decode(..) to see if the data is there, there maybe some JSON error or something? Commented Jan 23, 2015 at 1:06
  • Yeah, you're right Oleg, I think you've given me the answer. I just have to work out how to make it work for me. Thanks tons... and enjoy your awesome brain. Commented Jan 23, 2015 at 1:17

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.