Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Danny Massa
Danny Massa
27,856 Points

How to display certain item from dynamic Array

Hey Guys,

I have an array:

Array
(
    [0] => Array
        (
            [lang] => English
            [langorig] => En
            [flag] => us
            [isocode] => en
            [url] => /
            [active] => 1
        )

    [1] => Array
        (
            [lang] => Thai
            [langorig] => th
            [flag] => th
            [isocode] => th
            [url] => /th/
            [active] => 
        )

)

What do I need to write for it to say "if active = 1" then echo "isocode" ?

I'm trying to only display isocode only if that array item 'active' is set to 1

This array is dynamic and can add more languages or different ones so I can't just do an if else statement

Please help

1 Answer

Simon Coates
Simon Coates
28,694 Points

I'm not sure i understand the problem. I tried to write a demo:

<?php

$outerArray = [
];
$outerArray[]= [
    "active"=>1,
    "isocode"=> "isocode1"
];
$outerArray[]= [
    "active"=>0,
    "isocode"=> "isocode2"
];

$innerArray = $outerArray[1];
if($innerArray['active']===1){
echo "isocode";
}
Danny Massa
Danny Massa
27,856 Points

Hey Simon, sorry I must of been unclear however your example got me closer to the answer - this worked for me

foreach ($args as $langrecord) { 
          if ($langrecord['active'] == 1) { 
            echo '<dt><a href="#"><span><img class="flag" src="' . "$plugpath/img/flags/{$langrecord['flag']}" . '.png" alt="' . $langrecord['langorig'] . '"/>' . $langrecord['isocode'] . '</span></a></dt><dd><ul class="' . NO_TRANSLATE_CLASS . '">';
          }
        }