-2

I want to make multiple array by php (Laravel 5). There are two arrays as follows.

$tags=['en' =>[]];
$TAGS = ['test1','test2','test3',...]

I want to make this array as a return value in certain code like this:

return [
    'tags' => [
        'en' => [
            'test1' => 'test1',
            'test2' => 'test2',
            'test3' => 'test3',
            ...
        ]
    ]
]

I tried the following, but it did not work.

return [
    'tags' => [
        'en' => [
            foreach($TAGS as $TT)
                array_push($tags['en'], $TT);
        ]
    ]
]

Is there any other way?

halfer
  • 19,824
  • 17
  • 99
  • 186
User8392
  • 113
  • 3
  • 13

2 Answers2

0

Try like this :

<?php
$mainArray = array("EN","IT","SP"....);
$returnArray = array();
foreach($mainArray as $key => $value){
    //Create the sub array here as you want it.
    $subArray = [
        "Test1" => "test1",
        "Test2" => "test2",
        "Test3" => "test3"
    ];        
    array_push($returnArray[$value],$subArray);
}

return $returnArray;
?>
Igor Kamyshev
  • 149
  • 1
  • 9
Ketan Solanki
  • 697
  • 5
  • 13
0

Try this -

$json = array();
$langs = ['en','fr'];
$tags = ['test1','test2','test3'];
 foreach ($langs as $lang) {
    $json[$lang] = [];
    foreach ($tags as $tag) {
        $json[$lang][] = $tag;
    }
 }
return $json;
waseem asgar
  • 664
  • 8
  • 20