John Davidson

php - Print items of array if it has values?

0 comments
Message:


I have this array


Array
(
[jeans] => Array
(
[blue] => 3
[pink] => 1
[red] => 0
)

[shirts] => Array
(
[blue] => 5
[pink] => 0
[red] => 0
)
[pijama] => Array
(
[blue] => 0
[pink] => 0
[red] => 0
)
)

How can I print if jeans or shirts have items of color?
For example:
"Jeans has 3 blue items."
"Jeans has 1 pink item."
"shirts has 5 red items."
I'm not interested in obtain the ones with 0. I was thinking in a foreach, but I don't know how to.



My Answer:
"blue",
[shirt] = "",
[shoes] = "black",
[hat] = ""
)

foreach ($array as $key => $value) {
if ($value != "") {
echo "$key: $value\n";
}
}

This code will output:
jeans: blue
shoes: black

Rate this post

4 of 5 based on 1566 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us