我有Assoc_arr,数组中的值是: -
Array
(
[SIZE] => 8.5x11
[FOLDING] => HalfFold To 4.25x11
[PAPER] => 100lb Gloss Book with Aqueous Coating (C2S)
[COLOR] => full/full
[TURNAROUND] => Standard
)
当我打印它时,它会使用以下代码打印所有值: -
echo "<table>";
foreach ($final_array as $key => $value)
{
echo "<tr>";
echo "<td>";
echo $key;
echo "</td>";
echo "<td>";
echo $value;
echo "</td>";
echo "</tr>";
}
echo "</table><br><br>";
我怎样才能从assoc_arr中获取[SIZE] => 8.5x11
记录。
答案 0 :(得分:0)
检查:
echo "<table>";
foreach ($final_array as $key => $value)
{
if('SIZE' == $key) {
echo "<tr><td>";
echo $key;
echo "</td><td>";
echo $value;
echo "</td></tr>";
} else {
// do something else
}
}
echo "</table><br><br>";
如果只想要SIZE
键的值,则不需要任何循环。你可以得到它$ final_array [&#39; SIZE&#39;],否则上面的东西会帮助你。
答案 1 :(得分:0)
试试此代码
echo "<table>";
echo "<tr>";
echo "<td>";
echo key($arr);
echo "</td>";
echo "<td>";
echo $arr[key($arr)];
echo "</td>";
echo "</tr>";
echo "</table><br><br>";
答案 2 :(得分:0)
您可以尝试以下代码
echo "<table>";
foreach ($array as $key => $value)
{
if('SIZE' == $key) {
echo "<tr><td>";
echo $key;
echo "</td><td>";
echo $value;
echo "</td></tr>";
}
}
echo "</table>";
答案 3 :(得分:0)
您可以使用array_keys
获取第一个键和值:
$keys = array_keys($final_array);
$key = $keys[0];
$value = $final_array[$key];