APD Toronto
asked on
PHP to JavaScript Array
Hi Experts,
In PHP I have the following array:
My question is, how can I copy this into- a JavaScript Array?
Thank you
In PHP I have the following array:
Array
(
[0] => Array
(
[id] => 1625
[0] => 1625
[fName] => A.J.:Woodbine Hotel and Suites
[1] => A.J.:Woodbine Hotel and Suites
[lName] =>
[2] =>
[phone] =>
[3] =>
[archived] => 0
[4] => 0
[comments] =>
[5] =>
)
[1] => Array
(
[id] => 813
[0] => 813
[fName] => Aaron
[1] => Aaron
[lName] =>
[2] =>
[phone] =>
[3] =>
[archived] => 0
[4] => 0
[comments] =>
[5] =>
)
...
...
}
My question is, how can I copy this into- a JavaScript Array?
Thank you
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>my JS eliminates the string keys, and just uses the number keys
A better suggestion would be to eliminate the redundant data on the server before using json_encode().
Instead of using :
use:
OR
A better suggestion would be to eliminate the redundant data on the server before using json_encode().
Instead of using :
$row = $result->fetch_array();
which will give you duplicate data with numeric and string keys (corresponding to the column names of your executed query)use:
$row = $result->fetch_row();
if you only want numeric keys.OR
$row = $result->fetch_assoc();
if you only want string keys. You can still use the json_encode() function I suggested in my first post.
ASKER
What do you mean by I have duplicate data?
If you referring for example
As in [id] and [0] both equaling 1625 I thought that this was PHP's representation of element zero which can also be accessed by id?
If you referring for example
[id] => 1625
[0] => 1625
[fName] => A.J.:Woodbine Hotel and Suites
[1] => A.J.:Woodbine Hotel and Suites
As in [id] and [0] both equaling 1625 I thought that this was PHP's representation of element zero which can also be accessed by id?
>> As in [id] and [0] both equaling 1625
Yes, that's what I meant by "duplicate". However, this is not "PHP's representation". In other words, if you declare:
you will not "auto-magically" have another element with index 0 in it set to the same value as "fName". The duplicate values in your array structure is the doing of your DB driver. Very rarely do you need "enumerated" AND "string" keys after querying a DB. If you don't need both, my suggestion is to use either enumerated keys ( use fetch_row() ) OR string keys ( use fetch_assoc() ).
Yes, that's what I meant by "duplicate". However, this is not "PHP's representation". In other words, if you declare:
$data = Array("fName"=>"A.J.:Woodbine Hotel and Suites");
you will not "auto-magically" have another element with index 0 in it set to the same value as "fName". The duplicate values in your array structure is the doing of your DB driver. Very rarely do you need "enumerated" AND "string" keys after querying a DB. If you don't need both, my suggestion is to use either enumerated keys ( use fetch_row() ) OR string keys ( use fetch_assoc() ).
ASKER
Thhanks
Open in new window
This PHP code is set up for your specific PHP array, and outputs in Javascript a Two Level array, But In Javascript you can NOT HAVE string keys for arrays, so my JS eliminates the string keys, and just uses the number keys, but you have duplicate data, which is just a waste of array space.