php - How to create multiple array in Laravel 5?

463

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?

846

Answer

Solution:

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;
?>
99

Answer

Solution:

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;

People are also looking for solutions to the problem: php - Make action of on click select box list item laravel

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.