我试图删除我的json 2d数组中显示的数字。我首先有一个动态的2d数组,并从该数组中删除元素。然后它将数组转换为json 2d数组。问题是我一开始并不想要这些数字。
如下所示
[
"1": {"StateName":"Alaska","StateAbbr":"AK"},
"2": {"StateName":"Alabama","StateAbbr":"AL"},
"3": {"StateName":"Arkansas","StateAbbr":"AR"},
"4": {"StateName":"Arizona","StateAbbr":"AZ"},
"5": {"StateName":"California","StateAbbr":"CA"},
"6": {"StateName":"Colorado","StateAbbr":"CO"},
"7": {"StateName":"Connecticut","StateAbbr":"CT"}
]
我希望它像这样
[
{"StateName":"Alaska","StateAbbr":"AK"},
{"StateName":"Alabama","StateAbbr":"AL"},
{"StateName":"Arkansas","StateAbbr":"AR"},
{"StateName":"Arizona","StateAbbr":"AZ"},
{"StateName":"California","StateAbbr":"CA"},
{"StateName":"Colorado","StateAbbr":"CO"},
{"StateName":"Connecticut","StateAbbr":"CT"}
]
没有数字。我怎么能这样做?
我尝试了mysql_fetch_assoc,但它没有工作。
$arr = array();
while($row = mysql_fetch_assoc($result)) {
$arr[] = $row;
}
echo json_encode($arr);
答案 0 :(得分:1)
我发现一旦转换为json,如何从二维数组中删除键。创建函数来执行此操作。
function remove_json_keys($array) {
foreach ($array as $k => $v) {
if (is_array($v)) {
$array[$k] = $this->remove_json_keys($v);
} //if
} //foreach
return $this->sort_numeric_keys($array);
}
function sort_numeric_keys($array) {
$i=0;
foreach($array as $k => $v) {
if(is_int($k)) {
$rtn[$i] = $v;
$i++;
} else {
$rtn[$k] = $v;
} //if
} //foreach
return $rtn;
}