Page 1 of 1

Copying PHP array to JavaScript array

Posted: Fri Dec 11, 2009 12:33 pm
by code_worm
Hi, was wondering if anyone could assist me in a problem i am having.

What i want to do is copy my PHP array to JavaScript array so i can access this array in my external JavaScript file]
Below is the arrays i wish to copy

Code: Select all

$module_name['CM2013'] = "Professional Development in Computing";
$module_name['CM3004'] = "Systems Development";
$module_name['CM3006'] = "Internet Based Programming";
$module_name['CM3008'] = "Object Oriented Programming";
$module_name['CM3017'] = "Database Systems";
$module_name['CM3032'] = "Project Management in a Computing Enviroment";
$module_name['CM3056'] = "Interactive Multimedia";
$module_name['CM3ELECTIVE'] = "Elective 1";
$module_name['CM4003'] = "Intranet Systems Development";
$module_name['CM4008'] = "Human Computer Interaction";
$module_name['CM4018'] = "Honours Individual Project (Part 1)";
$module_name['CM4ELECTIVE(1)'] = "Elective 1";
$module_name['CM4002'] = "Information Strategy Planning";
$module_name['CM4018'] = "Honours Individual Project (Part 1)";
$module_name['CM4062'] = "Multimedia Programming";
$module_name['CM4ELECTIVE'] = "Elective 2";
 
$module_desc['CM2013'] = "To provide a broad range of professional knowledge and skills needed for a career in the Computing and IT industries.";
$module_desc['CM3004'] = "To provide the student with the ability to assess the different theories and methods of analysis and design that are utilised in the development of computer systems for industry.";
$module_desc['CM3006'] = "To enable the student to explore the key concepts of internet systems development.To develop the students' skill in the practical design, development and management of internet systems.";
$module_desc['CM3008'] = "To provide the student with the ability to explain, design, develop and test simple object-oriented programming applications.";
$module_desc['CM3017'] = "To provide the student with the ability to evaluate modern database architectures as well as analysing and applying techniques used in Database Management Systems. To provide the student with the ability to analyse and apply relational algebra operations. To provide the student with the ability to implement database applications.";
$module_desc['CM3032'] = "To provide the student with the ability to complete an investigation into a Computing topic and to undertake the associated project management activities as a member of a project group.";
$module_desc['CM3056'] = "To provide the student with the ability to research,plan, evaluate and create a multimedia application, with emphasis on common industry practice and the various media types involved.";
$module_desc['CM3ELECTIVE'] = "Module description is entirely based on the elective choice made";
$module_desc['CM4004'] = "To enable the student to research a problem area and undertake a substantial information technology (IT) or information systems project which will integrate the student’s knowledge and understanding from a variety of appropriate sources and disciplines. To enable the student to go through the stages from requirements gathering to post-implementation evaluation, and develop a solution to professional standards following a set of prescribed deadlines.";
$module_desc['CM4008'] = "To provide the student with a knowledge of the conceptual and theoretical aspects of HCI required to support future technological developments and the practical skills currently required to develop interfaces to interactive computer systems.";
$module_desc['CM4018'] = "To enable the student to undertake a substantial professional (or equivalent) information technology (IT) or software engineering project in order to acquire a comprehensive understanding of the problem and its domain. To enable the student to develop a solution from specification through to implementation and report on the results within a fixed time frame.";
$module_desc['CM4ELECTIVE'] = "Module description is entirely based on the elective choice made";
$module_desc['CM4002'] = "To provide the student with the ability to assess and support business goals through Information Strategy Planning.";
$module_desc['CM4018'] = "To enable the student to undertake a substantial professional (or equivalent) information technology (IT) or software engineering project in order to acquire a comprehensive understanding of the problem and its domain. To enable the student to develop a solution from specification through to implementation and report on the results within a fixed time frame.";
$module_desc['CM4062'] = "To provide the student with the ability to evaluate and apply programming techniques to multimedia systems using appropriate standards.";
$module_desc['CM4ELECTIVE'] = "Module description is entirely based on the elective choice made";

Re: Copying PHP array to JavaScript array

Posted: Fri Dec 11, 2009 1:42 pm
by AbraCadaver
Several ways to do it, but here is the first one I thought of:

Code: Select all

echo '<script type="text/javascript">';
echo '    var module_name = new Array();';
 
foreach($module_type as $key => $value) {
    echo '    module_name["' . $key . '"] = "' . $value . '";';
}
echo '</script>'; 
There is probably a better way to do it with json_encode(), but I'm not sure how to get that to the javascript.

Re: Copying PHP array to JavaScript array

Posted: Fri Dec 11, 2009 1:51 pm
by requinix
Personally, I'd go with json_encode.

Re: Copying PHP array to JavaScript array

Posted: Fri Dec 11, 2009 1:57 pm
by AbraCadaver
tasairis wrote:Personally, I'd go with json_encode.
Yeah, that was my second suggestion, but I've never used it. Just a quick glance showed me this may work:

Code: Select all

echo '<script type="text/javascript">';
echo '    var module_name = ' . json_encode($module_name) . ';';
echo '</script>';  
Does this look correct?