PHP Question

Appending arrays on JSON

Could any one tell me how to append arrays in an existing json data. I tried this code :

$response = new JsonResponse();
$article = new ArticlesDevis();
foreach ($articlesDevis as $articleArray) {

$article = $articleArray;
$response->setData(array('image' => $article->getArticle()->getImage()->getId(),array('nom' => $article->getArticle()->getName())));

}

return $response;


but it returns only the last data from the
$articlesDevis
array

the $articlesDevis array contains 2 arrays (I can get the value of the null values) :

object(Proxies\__CG__\ArticleBundle\Entity\Article)[1695]
public '__initializer__' =>
object(Closure)[1761]
public '__cloner__' =>
object(Closure)[1760]
public '__isInitialized__' => boolean false
private 'id' (ArticleBundle\Entity\Article) => int 1
private 'name' (ArticleBundle\Entity\Article) => null
private 'description' (ArticleBundle\Entity\Article) => null
private 'createdAt' (ArticleBundle\Entity\Article) => null
private 'prix' (ArticleBundle\Entity\Article) => null
private 'prixSolde' (ArticleBundle\Entity\Article) => null
private 'isSolde' (ArticleBundle\Entity\Article) => null
private 'percent' (ArticleBundle\Entity\Article) => null
private 'isVisible' (ArticleBundle\Entity\Article) => null
private 'enterprise' (ArticleBundle\Entity\Article) => null
private 'category' (ArticleBundle\Entity\Article) => null
private 'visit' (ArticleBundle\Entity\Article) => null
private 'image' (ArticleBundle\Entity\Article) => null

object(Proxies\__CG__\ArticleBundle\Entity\Article)[1693]
public '__initializer__' =>
object(Closure)[1761]
public '__cloner__' =>
object(Closure)[1760]
public '__isInitialized__' => boolean false
private 'id' (ArticleBundle\Entity\Article) => int 2
private 'name' (ArticleBundle\Entity\Article) => null
private 'description' (ArticleBundle\Entity\Article) => null
private 'createdAt' (ArticleBundle\Entity\Article) => null
private 'prix' (ArticleBundle\Entity\Article) => null
private 'prixSolde' (ArticleBundle\Entity\Article) => null
private 'isSolde' (ArticleBundle\Entity\Article) => null
private 'percent' (ArticleBundle\Entity\Article) => null
private 'isVisible' (ArticleBundle\Entity\Article) => null
private 'enterprise' (ArticleBundle\Entity\Article) => null
private 'category' (ArticleBundle\Entity\Article) => null
private 'visit' (ArticleBundle\Entity\Article) => null
private 'image' (ArticleBundle\Entity\Article) => null


It returns only the last one which is id=2

{"id":2,"image":14,"0":{"nom":"Table"}}

Answer

Let me guess a solution

return new JsonResponse(array_map(function ($article) {
    return [
        'image' => $article->getArticle()->getImage()->getId(),
        'nom' => $article->getArticle()->getName()
    ];
}, $articlesDevis));