Php 如何获得数组的正确html彩色索引

Php 如何获得数组的正确html彩色索引,php,html,arrays,Php,Html,Arrays,这是我的简单源代码 <?php $index1 = 1; $index2 = "<span style='color:red'>".$index1."</span>"; $index3 = intval("<span style='color:red'>".$index1."</span>"); $array = array(0=>"Apple", 1=>"Orange"); print_r($array[$index1]

这是我的简单源代码

<?php

$index1 = 1;

$index2 = "<span style='color:red'>".$index1."</span>";
$index3 = intval("<span style='color:red'>".$index1."</span>");

$array = array(0=>"Apple", 1=>"Orange");

print_r($array[$index1]);//output will be "Orange"
print_r($array[$index3]);//output "Apple", this should be "Orange"
print_r($array[$index2]);//Notice: Undefined index: 1 


?>

您需要一种比intval()更可靠的数字提取方法。Intval()只会将任何不明显的数字转换为“0”。例如,我建议您尝试使用preg_match()。尝试以下体验:

preg_match('|>([0-9]+)<|', $index2, $matches);
print_r($matches);

数组索引不是这样工作的,伙计,开始吧,是的,你在这里走的是一条非常错误的道路。有没有办法使数组的html标记索引,这样输出就会正确?这不起作用。我只需要知道,
$index2
如何与
$index1
相同,但颜色不同。我需要
打印\u r
索引。请检查:(按“运行”)对于这种特定情况,strip\u tags()是最简单的解决方案。
echo strip_tags($index2);