PHP echo values of all sub keys

PHP echo values of all sub keys



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.






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



foreach($array['options'] as $key => $obj) echo "$key: $obj['pt']";


$obj


$obj['pt']






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 Answers
5



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.


json


foreach($array['options'] as $key => $value)

echo $value['pt']."<br>";



Suppose you have this from json_decode():


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.


$output = array_column($options, 'pt');



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






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


array_column($json['options'], 'pt');


array_column






Yup, definitely a good approach, just thought it was worth mentioning.

– miken32
Sep 17 '18 at 21:19



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";);



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


foreach


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



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



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)