jquery - PHP Array building with foreach loop -
I am trying to create an array that I can output using JSON. For any reason, it will only add one element to the array, even if many email addresses return with an object. I know this because the loop implements the correct amount of time. Although new values are not added to the array. I tried array_push, but it creates a head array and implements jQuery's habitual process correctly.
$ load_model = $ this-> LoadModel ("LoadModel"); $ X = $ load_model- & gt; Updateload ($ load rate, $ carride, $ comment, $ temporary, $ loadstats, $ contact ID, $ load_id); If ($ x == true) {$ arr = array (); Foreach ($ load_model-> loadEmailAddresses ($ load_id) as $ val) {$ arr ['email'] = $ val- & gt; E-mail; $ Arr ['ContactId'] = $ val- & gt; Contact id; Echo json_encode ($ arr); } Any thoughts anyone?
You are setting the same loop as the maximum element every time it is loop, it Will set $ arr ['email'] and $ arr ['ContactId'] . In the end you will only find the last time that went through the loop. You will need to create an array for email and contact ID, then add that code to $ arr . $ load_model = $ this-> Load model ("LoadModel"); $ X = $ load_model- & gt; Updateload ($ load rate, $ carride, $ comment, $ temporary, $ loadstats, $ contact ID, $ load_id); If ($ x == true) {$ arr = array (); Foreach ($ load_model-> loadEmailAddresses ($ load_id) $ val $ $ arr [] = array ('email' => $ val- & gt; email, 'ContactId' = & gt; $ Val- & gt; Contact ID); Echo json_encode ($ arr); }
Comments
Post a Comment