Monday, October 21, 2013

Php Array and Javascript Array difference examples

php Array
PHP Array can be created using the array()  construct. PHP Array takes any number of comma-separated(,) key => value pairs as arguments.

array(
    key  => value,
    key2 => value2,
    key3 => value3,
    ...
)



Php Example
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Php Array </TITLE>
</HEAD>

<BODY>
<?php
$options = array(
'16-10-2013'  => '16 October 2013',
'17-10-2013'  => '17 October 2013',
'18-10-2013'  => '18 October 2013',
'19-10-2013'  => '19 October 2013',
'25-10-2013'  => '25 October 2013',
'27-10-2013'  => '27 October 2013',
'30-10-2013'  => '30 October 2013',

                );
                ?>

<select id="date_value" name="date_value" class="dte">
<option value="">--Select Date --</option>
<?php
foreach($options as $key => $value)
{?>
<option value="<?php echo $key; ?>"  <?php echo $s; ?> ><?php echo $options[$key]; ?></option>
<?php } ?>
</select>
</BODY>
</HTML>


JavaScript Array
JavaScript Array object is used to store multiple values in a single variable.

JavaScript Example
<HTML>
<HEAD>
<TITLE> Javascript Array</TITLE>
</HEAD>

<BODY>
<script language="javascript">
var dt_arr = Array(
Array('16 October 2013','16-10-2013'),
Array('17 October 2013','17-10-2013'),
Array('18 October 2013','18-10-2013'),
Array('19 October 2013','19-10-2013'),
Array('25 October 2013','25-10-2013'),
Array('27 October 2013','27-10-2013'),
Array('30 October 2013','30-10-2013')
);

///////////Function for country
function ShowDateOption()
{
    for(var x=0;x<dt_arr.length;x++)
    {

        document.write('<option value="' + dt_arr[x][1] + '">' + dt_arr[x][0] + '</option>\n');
   
    }

}

</script>

<select size="1" name="pdate" class="fldsn">
<option value=""  >------Select Travel Date------
<script type="text/javascript">
ShowDateOption();
</script>

</select>

</BODY>
</HTML>

No comments:

Post a Comment