Processing PHP array with json jquery $.each() get key value

A simple start will be, example array

<?php $arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5); ?>
<script type="text/javascript">
    var arr = <?php echo json_encode(  $arr  );?>;
    alert(obj['a'])
<script> 

Output: “1″

Another example given array:

<?php
     $arr  =  Array ( [136438349186fwjbr] => test3 [136444493417krous] => sports4 [136444501982ununp] => Sports3 [136444538415upydf] => Sports6 ) ;
 ?>
 <script type="text/javascript">
     var subArray = <?php echo json_encode(  $arr  );?>;
     $.each(subArray, function (i, elem) {
         alert(i + ' is ' + elem);
     }
 );
 </script>

Output: “136438349186fwjbr is test3″ etc….

By Keenlio, December 11, 2013

What do you think?

Leave a Reply

Your email address will not be published. Required fields are marked *


seven + = 9

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>