title: PHP: json_encode - Using foreach() and encoding html elements for json
body: <p>I am trying to parse articles from mysql and encode the data in json with php.</p>
  
  <p>Currently publishing articles I use:</p>
  
  <pre><code>&lt;?php if ($success): ?&gt;
      &lt;?php foreach($article-&gt;get_items() as $item): ?&gt;
  
      &lt;?php echo $item-&gt;get_content(); ?&gt;
  
  
      &lt;?php endforeach; ?&gt;
  &lt;?php endif; ?&gt;
  </code></pre>
  
  <p>and I am trying to <strong>encode this into json</strong>.</p>
  
  <p>I have tried this:</p>
  
  <pre><code>&lt;?php if ($success): ?&gt;
      &lt;?php foreach($feed-&gt;get_items() as $item): ?&gt;
  
      &lt;?php
      $data = array(
          'id'    =&gt;    "1",
          'result'       =&gt;    array(
                              'title'    =&gt;   'This is the title',
                              'publish'   =&gt;  'John Doe',
                              'content'    =&gt;  $item-&gt;get_content()
                       )
      );
  
      echo json_encode($data);
  ?&gt;
  &lt;?php endforeach; ?&gt;
  &lt;?php endif; ?&gt;
  </code></pre>
  
  <p>Also, <strong>I'm not sure how I would use foreach()</strong> so that I could parse and encode all the content.</p>
  
  <p>UPDATE:</p>
  
  <p>The content parsed from <strong>$item->get_content()</strong> has HTML elements such as <strong></strong>, <strong></strong> etc. so they should be encoded into json or strings ?</p>
  
  <p><strong>UPDATE 2:</strong></p>
  
  <p>The problem is that currently I end up with this:</p>
  
  <pre><code>[
  {"id":"1","result":{"title":"This is the title","publish":"John Doe","content":"content 1"}},
  {"id":"1","result":{"title":"This is the title","publish":"John Doe","content":"content 1"}},
  {"id":"1","result":{"title":"This is the title","publish":"John Doe","content":"content 1"}},
  {"id":"1","result":{"title":"This is the title","publish":"John Doe","content":"content 1"}},
  {"id":"1","result":{"title":"This is the title","publish":"John Doe","content":"content 1"}}
  ]
  </code></pre>
  
  <p>because <strong>I am not using foreach() properly</strong> and I want to end up with this:</p>
  
  <pre><code>[
  {"id":"1","result": {"title":"This is the title","publish":"John Doe","content":"content 1"},
              {"title":"This is the title","publish":"John Doe","content":"content 1"},
              {"title":"This is the title","publish":"John Doe","content":"content 1"},
              {"title":"This is the title","publish":"John Doe","content":"content 1"},
              {"title":"This is the title","publish":"John Doe","content":"content 1"}
  ]
  </code></pre>
  
  <p>and also the <strong>content sometimes it contains html elements</strong> which destroys the json encoding so I would imagine I have to encode it into json or string?</p>