0

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.

André Castro
  • 1,527
  • 6
  • 33
  • 60
  • 1
    `foreach($array['options'] as $key => $obj) { echo "$key: {$obj['pt']}"; }` - $key will be "front-electric", "electric...",... - `$obj` is the object inside "front-electric" - `$obj['pt']` the value you want! – Jeff Sep 17 '18 at 21:10
  • 1
    by the way the PHP array, this is in JSON format which is used by Javascript – evgpisarchik Sep 17 '18 at 21:16
  • All the answers were great, but in the end the foreach loop was the one tto go. Apologies for the delayy and many thanks to all. – André Castro Sep 24 '18 at 20:01

5 Answers5

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>"; 
}
Chukwuemeka Inya
  • 2,575
  • 1
  • 17
  • 23
1

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

mrbm
  • 2,164
  • 12
  • 11
  • Their JSON is another level deeper than that though. – miken32 Sep 17 '18 at 21:17
  • That makes it `array_column($json['options'], 'pt');` I guess, the idea is to simply use `array_column` and avoid looping. – mrbm Sep 17 '18 at 21:18
  • Yup, definitely a good approach, just thought it was worth mentioning. – miken32 Sep 17 '18 at 21:19
  • [`echo implode("\n", array_column($options, 'pt'));`](https://3v4l.org/II2Cr) is IMO the cleanest approach because there is no trailing newline on the last value. – mickmackusa May 01 '22 at 08:45
1

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";});
miken32
  • 42,008
  • 16
  • 111
  • 154
0

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

foreach($array['options'] as $key => $val){
    echo $val['pt'];
}
Velimir Tchatchevsky
  • 2,812
  • 1
  • 16
  • 21
0

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

Ashfaque Ali Solangi
  • 1,883
  • 3
  • 22
  • 34