0

I want to remove an element from an array (converted from json), but with unset, and reconvert in json, the array become indexed.

Source array:

{"rows":
[{"c":[{"v":"Date(1409052482000)"},{"v":22},{"v":22},{"v":22},{"v":null}]},
{"c":[{"v":"Date(1409052614000)"},{"v":22},{"v":22},{"v":22},{"v":null}]},
{"c":[{"v":"Date(1409052782000)"},{"v":22},{"v":22},{"v":22},{"v":null}]}
]}

Result:

{"rows":
"2":{"c":[{"v":"Date(1409052614000)"},{"v":22},{"v":22},{"v":22},{"v":null}]},
"3":{"c":[{"v":"Date(1409052782000)"},{"v":22},{"v":22},{"v":22},{"v":null}]}
}}

the problem is the "2" and "3" keys. I don't want this keys, because I use the data for google chart, and is sensible for this index key.

PHP code:

$tempdata = json_decode($jsonTempLog, TRUE);
foreach ($tempdata['rows'] as $key => $row) {
    if ( $logtime < $showtime) {
        unset($tempdata['rows'][$key]);
    }
}
echo json_encode($tempdata);

How can I remove element from array, keep the original json syntax?

1
  • What is $logtime and where is it coming from? Commented Aug 27, 2014 at 10:03

3 Answers 3

4

Just do this:

$tempdata["rows"] = array_values($tempdata["rows"]);
echo json_encode($tempdata);

Otherwise JSON thinks you're sending an associative array rather a numeric one

Sign up to request clarification or add additional context in comments.

Comments

0

this is how i work with :

unset($infos[$i]);
$infos = array_values($infos);

1 Comment

Thanks, it works: $tempdata['rows'] = array_values($tempdata['rows']);
0

maybe like this:

foreach($tempdata as $row){
    $tempdata[$rows['keyfield']] = $row;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.