Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,693 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 . '">';
          }
        }