This question already has an answer here:
这个问题已经有了答案:
- How do I encode a PHP array to a JSON array, not object? 4 answers
- 如何将PHP数组编码为JSON数组而不是对象?4答案
I'm trying to remove numbers that is being display in my json 2d array. I first had an 2d array that is dynamic and deletes elements from that array. Then it converts the array to a json 2d array. The problem is I don't want the numbers at the start.
我试图删除json 2d数组中显示的数字。我首先有一个二维数组,它是动态的,可以从数组中删除元素。然后将数组转换为json 2d数组。问题是,一开始我不想要这些数字。
Like below
像下面的
[
"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"}
]
I would like it to be like this
我希望它是这样的
[
{"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"}
]
With out the numbers. How can I do this?
与数字。我该怎么做呢?
I tried mysql_fetch_assoc but it doesn't work it.
我尝试了mysql_fetch_assoc,但它不起作用。
$arr = array();
while($row = mysql_fetch_assoc($result)) {
$arr[] = $row;
}
echo json_encode($arr);
1 个解决方案
#1
1
I found out how to remove the keys from the 2d array once converted to a json. Create functions to do this.
我发现一旦转换成json,如何从2d数组中删除键。创建函数来实现这一点。
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;
}
#1
1
I found out how to remove the keys from the 2d array once converted to a json. Create functions to do this.
我发现一旦转换成json,如何从2d数组中删除键。创建函数来实现这一点。
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;
}