-3

it's my first question here. I hope I will not be annoying.

I have to read array and put the data to JS script.

This is var_dumped data from array:

array(2406) {
    [0]=>
    array(2) {
    ["brand"]=>
    string(4) "Mazda"
    ["model"]=>
    string(9) "CX-4"
    }
    [1]=>
    array(2) {
    ["brand"]=>
    string(4) "Mazda"
    ["model"]=>
    string(19) "CX-5"
    }

    [...]

    [120]=>  
    array(2) {
    ["brand"]=>
    string(12) "Mercedes"
    ["model"]=>
    string(10) "E-class"
    }
    [121]=>  
    array(2) {
    ["brand"]=>
    string(12) "Mercedes"
    ["model"]=>
    string(10) "S-class"
    }
    [122]=>
    array(2) {
    ["brand"]=>
    string(9) "Volkswagen"
    ["model"]=>
    string(10) "Golf"
    }
    [123]=>
    array(2) {
    ["brand"]=>
    string(9) "Volkswagen"
    ["model"]=>
    string(10) "Polo"
    }
    [124]=>
    array(2) {
    ["brand"]=>
    string(9) "Volkswagen"
    ["model"]=>
    string(10) "Touareg"
    }
}

And I have to return script like this:

"Mazda" : { // the brand
        "CX-4",// the list of brand’s models
        "CX-5",
    },
    "Mercedes" : {
        "E-class",
        "S-class",
    },    
    "Volkswagen" : {
        "Golf",
        "Polo",
        "Touareg",
    },

I really need this and I don't have any ideas to solve it. Please help me.

I've tried to use:

foreach($row as $brand => $key){
            echo "\"$brand\" : {\n";
                print $key[model];  

            echo "},\n";
            }

But unfortunately it returns only

"Mazda" : {
CX-4},
"Mercedes" : {
E-class},
"Volskwagen" : {
Golf},

Only one model for one brand :-/

4
  • 4
    Welcome. Unfortunately SO is not a free coding service. You have to have attempted something. If you have a specific issue with what you have coded, we are more than happy to help.
    – RiggsFolly
    Aug 28, 2015 at 12:34
  • I've tried so much ways to print this data, i.e. foreach in foreach, foreach for array, but every try ends with no results :(
    – gklinek
    Aug 28, 2015 at 12:38
  • 2
    @gklinek Then show your work and your code. Edit your question and add your best or your last attempt.
    – Rizier123
    Aug 28, 2015 at 12:40
  • And you dont have long. One more close vote and this question is dead.
    – RiggsFolly
    Aug 28, 2015 at 12:42

1 Answer 1

0

Maybe this :

foreach($row as $brand => $innerArray){
    echo "\"$brand\" : {\n";
    foreach($innerArray as $innerRow => $value){
       echo $value . "<br/>";
    }
 }
1
  • It's the same :( Is there posibility to filter arrays before? For example to find every model of the brand?
    – gklinek
    Aug 28, 2015 at 13:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.