Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个数组($my_array),我希望它应该print只是它的值是真实的,即在给定的数组中它应该打印I have a cow and I have a cat。我想登录类似 if cow=== truethen print if dog=== truethen print ...
$my_array
print
cow
true
dog
PHP实现这一目标的最佳方法是什么?
PHP
$my_array =[["cow", true ], ["dog", false ], ["cat", true ] ];
根据您的问题,您有一个数组
并且您只想打印第一个索引为ture.
ture
所以你可以这样做,0th如果1st索引值为真,它将只打印索引值$my_array.
0th
1st
$my_array.
array_map(function($arr){ if($arr[1]) print "I have a " . $arr[0] . "\n"; }, $my_array);
输出将是
I have a cow I have a cat