2

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.

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.

$arr = array();
while($row = mysql_fetch_assoc($result)) {
  $arr[] = $row; 
}
echo json_encode($arr);
1
  • No that would not work dynamic Keys within the 2d array. Thank for the response. Commented Sep 13, 2014 at 13:57

1 Answer 1

1

I found out how to remove the keys from the 2d array once converted to a json. Create functions to do this.

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;
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.