Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8142925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:57:19+00:00 2026-06-06T12:57:19+00:00

In the code below I would expect t2: [{value:…..}, {}, {}] but instead I

  • 0

In the code below I would expect

t2: [{"value":"".....}, {}, {}]

but instead I get

t2: {"0":{"value":"",.. , "1":{}, .. }

even casting

json_encode( (array) $t2);

doesn’t make it work
thx

function filter($var) {
    return($var->id < 4);
}

$t1=json_decode('[{"value":"","id":"0","key":"algo"},{"value":"0.8","id":"-1","key":"cout"},{"value":"2012-07-21","id":"3","key":"date"},{"value":"","id":"0","key":"europe"},{"value":"","id":"0","key":"inria"},{"value":"1","id":"4","key":"ndOfRates"},{"value":"part","id":"0","key":"offre"},{"value":"","id":"0","key":"paris"},{"value":"Ut enim quisque sibi plurimum confidit et ut quisque maxime virtute et sapientia sic munitus est, ut nullo egeat suaque omnia in se ipso posita iudicet, ita in amicitiis expetendis colendisque maxime excellit. Quid enim? Africanus indigens mei? Minime hercule! ac ne ego quidem illius; sed ego admiratione quadam virtutis eius, ille vicissim opinione fortasse non nulla, quam de meis moribus habebat, me dilexit; auxit benevolentiam consuetudo. Sed quamquam utilitates multae et magnae consecutae sunt, non sunt tamen ab earum spe causae diligendi profectae. Hae duae provinciae bello quondam piratico catervis mixtae praedonum a Servilio pro consule missae sub iugum factae sunt vectigales. et hae quidem regiones velut in prominenti terrarum lingua positae ob orbe eoo monte Amano disparantur. Ultima Syriarum est Palaestina per intervalla magna protenta, cultis abundans terris et nitidis et civitates habens quasdam egregias, nullam nulli cedentem sed sibi vicissim velut ad perpendiculum aemulas: Caesaream, quam ad honorem Octaviani principis exaedificavit Herodes, et Eleutheropolim et Neapolim itidemque Ascalonem Gazam aevo superiore exstructas. Post hoc impie perpetratum quod in aliis quoque iam timebatur, tamquam licentia crudelitati indulta per suspicionum nebulas aestimati quidam noxii damnabantur. quorum pars necati, alii puniti bonorum multatione actique laribus suis extorres nullo sibi relicto praeter querelas et lacrimas, stipe conlaticia victitabant, et civili iustoque imperio ad voluntatem converso cruentam, claudebantur opulentae domus et clarae. Et quoniam apud eos ut in capite mundi morborum acerbitates celsius dominantur, ad quos vel sedandos omnis professio medendi torpescit, excogitatum est adminiculum sospitale nequi amicum perferentem similia videat, additumque est cautionibus paucis remedium aliud satis validum, ut famulos percontatum missos quem ad modum valeant noti hac aegritudine colligati, non ante recipiant domum quam lavacro purgaverint corpus. ita etiam alienis oculis visa metuitur labes.","id":"4","key":"text"},{"value":"on","id":"0","key":"type"}]');

$t2 = array_filter($t1, "filter");

echo 't2: '.json_encode($t2);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T12:57:21+00:00Added an answer on June 6, 2026 at 12:57 pm

    In PHP, there is no difference between associative and numeric arrays. Therefore, when converting to JSON, json_encode has to infer from the array’s keys whether it should be encoded as an array or an object. It does this by testing to see if (a) the keys are numeric and (b) if they are sequential, starting with 0; if both tests pass, it will be encoded as an array.

    When you use array_filter, you remove some of the keys. The others are not renumbered, so the keys are no longer sequential. This means that json_encode infers that this should be an object rather than an array when encoded in JSON.

    You can correct this by renumbering the keys. Use array_values to do this:

    echo 't2: '.json_encode(array_values($t2));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The code below doesn't behave as I would expect. ; given a function name,
The code below works fine. But it seem clunky. How would you combine the
In the code sample below, I would expect the output to be as follows,
Code example below. Calling append on a hash value returns correctly but the hash
I'm wondering why there is an inconsistency with the code below. I would expect
I understand that the code below would result segmentation fault because at the cstr
I am trying to implement does_key_exist, I thought the code below would do it.
if f = g + h then where in the below code would I
Consider the Java code below, what would happen if there were no paintComponent method
Given the code below, how would you create/implement SR.h so that it produces the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.