Running a cycle within an array?

0

How can I add more items in the "items" array? I have a table with several items and I want to run a foreach inside to load each one of them, but when I do it I throw an error (syntax error, unexpected 'foreach'). The project is done in Laravel 5.5

$preferenceData = array(
    "items" => array(
        array(
            "id" => "Code",
            "title" => "Title of what you are paying for",
            "currency_id" => "ARS",
            "picture_url" =>"img/logomp3.gif",
            "description" => "Description",
            "category_id" => "Category",
            "quantity" => 1,
            "unit_price" => 1.0
        ),
    ),
  ),
    
asked by MarianoV 05.12.2017 в 05:13
source

2 answers

1

You can start the variable as an array, then you just have to add elements like this:

<?php
$preferenceData = array(
    "items" => array()
);

foreach ('condición aquí') {
    $preferenceData['items'][] = array('tu array aquí');
}
    
answered by 05.12.2017 в 09:55
-1

You could put the items you have in your table in a type arrangement

$tabe_items = array(
   [
    "id" => "Code",
    "title" => "some title"
    ...
   ],
   [
    "id" => "Code",
    "title" => "some title"
    ...
   ],
 );

and finally join them to your arrangement

array_push(preferenceData['items'],$table_items);
    
answered by 05.12.2017 в 17:31