Note that there are some explanatory texts on larger screens.

plurals
  1. POUnable to convert PHP multidimensional array to Javascript array
    text
    copied!<p>I have php multidimensional array which is generated dynamically</p> <pre><code>Array ( [0] =&gt; Array ( [cid] =&gt; 73 [type] =&gt; 2 [qrystr] =&gt; Qtest1 [trck_no] =&gt; (570) 244-3738 [trgt_no] =&gt; 1919674683063 [webpage] =&gt; Array ( [0] =&gt; Array ( [wid] =&gt; 40 [page_url] =&gt; www.ctest2.com ) [1] =&gt; Array ( [wid] =&gt; 41 [page_url] =&gt; www.ctest3.com ) ) ) [1] =&gt; Array ( [cid] =&gt; 75 [type] =&gt; 3 [qrystr] =&gt; Qtest6 [trck_no] =&gt; [trgt_no] =&gt; [webpage] =&gt; Array ( [0] =&gt; Array ( [wid] =&gt; 42 [page_url] =&gt; www.test1.com ) [1] =&gt; Array ( [wid] =&gt; 43 [page_url] =&gt; www.test1.com ) ) ) </code></pre> <p>)</p> <p>and I want to convert it in Javascript. For that I have already encoded it using <code>json_encode()</code> in php and here is the output for the same :</p> <pre><code>[{"cid":"73","type":"2","qrystr":"Qtest1","trck_no":"(570) 244-3738","trgt_no":"1919674683063","webpage":[{"wid":"40","page_url":"www.ctest2.com"},{"wid":"41","page_url":"www.ctest3.com"}]},{"cid":"75","type":"3","qrystr":"Qtest6","trck_no":"","trgt_no":"","webpage":[{"wid":"42","page_url":"www.test1.com"},{"wid":"43","page_url":"www.test1.com"}]}] </code></pre> <p>In javascript whenever I parse this using <code>JSON.parse()</code> I get an error message (i.e. SyntaxError: JSON.parse: unexpected character).</p> <p>Kindly suggest.</p> <p><strong>EDITED:</strong></p> <p>My php file is in separate server and where pogramatically I gather information from database based on "id" and store it in an array.</p> <p>Here is my php code:</p> <pre><code> foreach($campaigns as $campaign){ if(($campaign-&gt;type == 2) || ($campaign-&gt;type == 3)){ foreach($campaign-&gt;webpage as $webpage){ $webpages[] = array( 'wid'=&gt;trim($webpage-&gt;id), 'page_url'=&gt;trim($webpage-&gt;page_url) ); } }else{ $webpages = ""; } $campaign_details[] = array( 'cid'=&gt;trim($campaign-&gt;id), 'type'=&gt;trim($campaign-&gt;type), 'qrystr'=&gt;trim($campaign-&gt;qrystr), 'trck_no'=&gt;trim($campaign-&gt;trcknmb-&gt;frnd_name), 'trgt_no'=&gt;trim($campaign-&gt;trcknmb-&gt;trgtnmb-&gt;phone_no), 'webpage'=&gt;$webpages ); unset($webpages); } $cdetails = json_encode($campaign_details); echo $cdetails; </code></pre> <p>I make a cross domain request to get the array valu.</p> <p>Here is my Javascript code to parse the json string:</p> <pre><code> function handler(evtXHR) { if (invocation.readyState == 4) { if (invocation.status == 200) { response = invocation.responseText; var content = JSON.parse(response); alert(content); } else { alert("Invocation Errors Occured"); } } } </code></pre> <p>I put an alert function here to check the output result. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload