'PHP echo values of all sub keys [duplicate]

In the following array:

"options": {
                    "front-electric": {
                        "pt": "Vidros Eléctricos dianteiros",
                        "en": "Front electric"
                    },
                    "electric-diant-back": {
                        "pt": "Vidros Eléctricos diant. + trase.",
                        "en": "Electric diant. + Back."
                    },
                    "darkened": {
                        "pt": "Vidros Escurecidos",
                        "en": "Darkened"
                    },
                    "soundproofing-and-athermic": {
                        "pt": "Vidros Insonorizantes e Atérmicos",
                        "en": "Soundproofing and Athermic"
                    }
                }

How can i echo the value 'pt' of all sub keys from 'options' array?

I could try something similar to ['options']['pt'], but i don't understand how to refer to "front-electric", "electric-diant-back"... that are sub key from 'options' and all of them have different text.



Solution 1:[1]

This is looking more like a json string to me. If so, you will have to first, json_decode and then loop through the outputted array.

foreach($array['options'] as $key => $value) 
{ 
    echo $value['pt']."<br>"; 
}

Solution 2:[2]

Suppose you have this from json_decode():

$options = [
  "front-electric" => [
      "pt" => "Vidros Eléctricos dianteiros",
      "en" => "Front electric"
  ],
  "electric-diant-back" => [
      "pt" => "Vidros Eléctricos diant. + trase.",
      "en" => "Electric diant. + Back."
  ],
  "darkened" => [
      "pt" => "Vidros Escurecidos",
      "en" => "Darkened"
  ],
  "soundproofing-and-athermic" => [
      "pt" => "Vidros Insonorizantes e Atérmicos",
      "en" => "Soundproofing and Athermic"
  ]
];

Then simply doing : $output = array_column($options, 'pt'); will give you the needed array.

Check this fiddle for your use case : https://repl.it/repls/DefinitiveWavyProblem

Solution 3:[3]

Better to use one of the built-in functions, rather than a loop. Cleaner and shorter code, possibly more efficient.

array_walk($data, function($v, $k) {echo "$v[pt]\n";});

Solution 4:[4]

You can use a foreach loop (as @Jeff mentions)

foreach($array['options'] as $key => $val){
    echo $val['pt'];
}

Solution 5:[5]

Try this

$json = '{"options": {
            "front-electric": {
                "pt": "Vidros Eléctricos dianteiros",
                "en": "Front electric"
            },
            "electric-diant-back": {
                "pt": "Vidros Eléctricos diant. + trase.",
                "en": "Electric diant. + Back."
            },
            "darkened": {
                "pt": "Vidros Escurecidos",
                "en": "Darkened"
            },
            "soundproofing-and-athermic": {
                "pt": "Vidros Insonorizantes e Atérmicos",
                "en": "Soundproofing and Athermic"
            }
        }}';
$json_array = json_decode($json,true);  

foreach($json_array as $key=>$values){

    foreach($values as $subkeys=>$subvalues){

        print $subvalues['pt'] . "<br/>";   

    }

}

output

Vidros Eléctricos dianteiros
Vidros Eléctricos diant. + trase.
Vidros Escurecidos
Vidros Insonorizantes e Atérmicos

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 mrbm
Solution 3 miken32
Solution 4 Velimir Tchatchevsky
Solution 5 Ashfaque Ali Solangi