Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

208
Views
Javascript/PHP: reordenar la matriz JSON al azar provoca un valor nulo y elimina todo

Tengo un problema aleatorio al intentar reordenar elementos en una matriz JSON a través de JS/PHP, esto ocurre cuando la matriz tiene más de 1000 elementos.

Ejemplo de mi archivo Users.json :

 [ { "ID_id": "123abc", "ST_username": "johndoe", "ST_email": "j@example.com", "ST_fullname": "John Doe", }, { "ID_id": "def345", "ST_username": "sarahdoe", "ST_email": "s@example.com", "ST_fullname": "Sarah Doe", }, { "ID_id": "fgh567g", "ST_username": "markdoe", "ST_email": "mark@example.com", "ST_fullname": "Mark Doe", } // + additional 1000 similar items in this array ]

Entonces, antes que nada, obtengo los datos del archivo JSON y los decodifico en una matriz PHP:

 $tableName = $_GET['tableName']; // Lets' say $tableName is 'Users' // fetch data from 'Users.json' $data = file_get_contents($tableName. '.json'); $data_array = json_decode($data, true);

Entonces llamo a esta función en JS:

 function reorderTableData() { var newOrderArray = []; var data_arrayStr = JSON.stringify(<?php echo json_encode($data_array) ?>); var jsonData = JSON.parse(data_arrayStr); var reorderedData = JSON.stringify(jsonData, newOrderArray); $.ajax({ url : "reorder-table-data.php", type: 'POST', data: 'tableName='+'<?php echo $tableName ?>'+'&reorderedData='+encodeURIComponent(reorderedData), success: function(data) { location.reload(); // error }, error: function(e) { }}); } }

Mi código reorder-table-data.php :

 $tableName = $_POST['tableName']; $reorderedData = $_POST['reorderedData']; $data_array = json_decode($reorderedData, true); // Encode back to JSON $data = json_encode(array_values($data_array), JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); file_put_contents($tableName. '.json', $data); // echo JSON object echo json_encode($data); ?>

A veces, este código borra todo y todo lo que ve es null en el archivo Users.json , todos los datos se perdieron.

¿Qué estoy haciendo mal?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!