2

i have an array inside another array like this in php

Array
(
    [common_search] => Array
        (
            [BusinessName] => Sun Shine Vision
            [Address] => Amulia St Madhava Pharmacy Jn
            [Phone] => 
        )

    [cache_table] => Array
        (
            [Details] => Speedtrax,Ample's Bldg Off Banerji Rd., Amulia St,
        )

)

And i have to iterate these as seperate arrays in my result page using Jquery ,as i want to get these arrays separately to display in different divs to set as html ,How can i get the data from two arrays (common_search , cache_table) separately . And in my html i want to set data like $("#common_searchdiv").html(common_search) and $("#cache_tablediv").html(cache_table) . Thanks in advance

3 Answers 3

2

When json-encoding named arrays they are going to be parsed as objects in javascript, so you can just do like this:

<script>
   var data = <?=json_encode($data);?>
</script>

since now data in javascript will be an object, so you can directly access properties like

<script>
data["common_search"]["BusinessName"];
</script>

or iterate over it with

$.each

Sign up to request clarification or add additional context in comments.

Comments

1

You can not directly pass data as array. Do json_encode of array. And then on jquery side parse the json and then run each/for loop. On the match of key you can add if condition.

Do something like this

var result = $.parseJSON(jsonstring);//jsonstring is your json encoded string.

for(var k in result) {
   if (k == 'common_search') {
       $("#common_searchdiv").html(result[k]); 

   } 
   if (k == 'cache_table') {
       $("#cache_tablediv").html(result[k]);
   }
}

For your reference it works

var jsonstring = {"common_search":{"strBusinessName":"Sun Shine Vision","strAddress":"Amulia St Madhava Pharmacy Jn","intPhone":""},"cache_table":{"Details":"Speedtrax,Ample's Bldg Off Banerji Rd., Amulia St,"}};
var jsonstring = JSON.stringify(jsonstring).replace(/\'/, "/'");
var result = $.parseJSON(jsonstring);//jsonstring is your json encoded string.

for(var k in result) {

   if (k == 'common_search') {
       alert(result[k]['strBusinessName'])
       $("#common_searchdiv").html(result[k]['strBusinessName']); 

   } 
   if (k == 'cache_table') {
       $("#cache_tablediv").html(result[k]);
   }
}

12 Comments

After encoding data look like this : {"common_search":{"strBusinessName":"Sun Shine Vision","strAddress":"Amulia St Madhava Pharmacy Jn","intPhone":""},"cache_table":{"Details":"Speedtrax,Ample's Bldg Off Banerji Rd., Amulia St,"}} But your code seems not working
SyntaxError: JSON.parse: unexpected character , i think have to include json library in script ?
I think the problem is here Speedtrax,Ample's Bldg Off Banerji Rd., Amulia St you have Ample's the single qoutes is causing the problem can u escape it ?
Do this to replace it, var jsonstring = JSON.stringify(jsonstring).replace(/\'/, "/'");
Can you upvote and mark as tick if you feel it was helpful, so that future user can refer this answer
|
0

please try this.

<?php
$arr_temp = Array
(
    'common_search' => Array
        (
            'BusinessName' => "Sun Shine Vision",
            'Address' => "Amulia St Madhava Pharmacy Jn",
            'Phone' => ''
        ),

    'cache_table' => Array
        (
            'Details' => "Speedtrax,Ample's Bldg Off Banerji Rd., Amulia St,"
        )

);

?>
<script>
$(document).ready(function() {
    var data_arr = <?php echo json_encode($arr_temp); ?>;

    $.each(data_arr,function(item){     
        $.each(data_arr[item],function(myitem){
            console.log(myitem);
        });
    });
});
</script>

2 Comments

How come <?php echo json_encode($arr_temp); ?>; used in javascript ?? its php code right
if you put this code in a php page inside script tag then it will work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.