/    Sign up×
Community /Pin to ProfileBookmark

Is This Valid Array ?

Folks,

Are either of these two are valid arrays or not ?

1.

[code]
$list_5 = array($eu = array(‘0’=>’uk’,’1’=>’france’),array($america = array(“0″=>”usa”,”1″=>”canada”)));
[/code]

If above code is valid then how to write code so it echoes ‘uk’ and ‘france’ (the values of the EU sub-array) ?

2.

[code]
$list_5 = array(
“eu” => array(‘0’=>’uk’,’1’=>’france’),
“america” =>array(“0″=>”usa”,”1″=>”canada”)
);
[/code]

If above code is valid then how to write code so it echoes ‘usa’ and ‘canada’ (the values of the America sub-array) ?

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJul 31.2021 — 3.
[code=php]
$list_5 = array(
"eu" => array('uk', 'france'), // automatically numerically indexed starting a 0
"america" => array("usa", "canada") // ditto
);
[/code]

Then if you want the america entries:
[code=php]
foreach($list_5['america'] as $country) {
echo "<li>$country</li>n";
}
[/code]
Copy linkTweet thisAlerts:
@developer_webauthorAug 02.2021 — @NogDog#1634909

Thanks.

Just curious, can you not have keys and values inside the sub-arrays ? Like ....

4.
<i>
</i>$globe = array(
"eu" =&gt; array('west eu'=&gt;uk, 'east eu'=&gt;poland),
"america" =&gt; array("north america"=&gt;usa, "south america"=&gt;brazil) <br/>
);


Is above valid code ? If so, then ...

How to echo all keys only from "america" array ?

How to echo all values only from "america" array ?

How to echo all keys & values from "america" array ?
Copy linkTweet thisAlerts:
@developer_webauthorAug 02.2021 — 5.
<i>
</i>$continents = array(
"europe" =&gt; array("west europe" =&gt;uk, "west europe" =&gt;france),
"america" =&gt; array("north america" =&gt;usa, "north america" =&gt;canada), array("south america" =&gt;argentina, "south america" =&gt;brazil))
);

Is above code fine aswell ? If so, then ...

How to echo all keys only from "america" array ?

How to echo all values only from "america" array ?

How to echo all keys & values from "america" array ?
Copy linkTweet thisAlerts:
@NogDogAug 02.2021 — > @developer_web#1634965 Is above code fine aswell ?

No, did you even try it? (Hint: PHP cannot even parse it as you entered it here.)

What you probably want is:
[code=php]
$continents = array(
"europe" => array(
"west europe" => array(
"uk",
"france"
)
),
"america" => array(
"north america" => array(
"usa",
"canada"
),
"south america" => array(
"argentina",
"brazil"
)
)
);
[/code]

Pro tip: use that type of indenting so that it's visually clear what the array organization is. Write code as if you may have to debug/modify it a year or more later, when you've forgotten why you wrote it that way in the first place.

In this example, you would access "argentina" directly as $continents['america']['south america'][0]. If you wanted to loop through all countries in South America:
[code=php]
foreach($continents['america']['south america'] as $country) {
// code using $country
}
[/code]
Copy linkTweet thisAlerts:
@developer_webauthorAug 07.2021 — @NogDog#1634969

So you do it in this format then to make it valid ?
<i>
</i>category label =&gt; array(
subcategory label =&gt; array(
sub-subcategory label =&gt; array(
//And so on to further subcategories.
<br/>
<i> </i>


And items only get included in the final subcategory ?

Ofcourse, the sub arrays are also items of arrays but you know what I mean. I mean items that are no longer further subcategories. Nests.

<i>
</i>$var = array(
"cat 1" =&gt; array(
"subcat 1" =&gt; array(
"item 0",
"item 1"
)
),
"cat 2" =&gt; array(
"subcat 2" =&gt; array(
"item 0",
"item 1"
),



Yes ?
Copy linkTweet thisAlerts:
@NogDogAug 07.2021 — You use whatever structure makes sense for the data being represented. There is no single one-size-fits-all structure.
×

Success!

Help @developer_web spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.26,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...