Divide the pair of arrays that are transmitted via JSON

I combined two arrays and passed them through JSON, as shown below:

$result = array_merge($json, $json1);
echo json_encode($result);

Now, to retrieve the data, I am using the following code:

$.getJSON('./tarefasaad52', function (data) {
}

However, I now need to separate the arrays again.

Data received:

0: (3) ["T", 57, 22] 1: (3) ["T", 50, 23] 2: (3) ["M", 17, 22] 3: (3) ["M", 50, 23] 4: (3) ["T", 42, 23]

Answer №1

Need a helping hand? An Associative Array could be the solution.

$output = array();
$output['json'] = array('A','B','C');
$output['json1'] = array('D','E','F');

echo json_encode($output);

When it comes to retrieving the response, simply decode the JSON and access the values by specifying the index and name of the array you provided.

$.getJSON('./tarefasaad52', function (data) {
    const item1 = data.json[0];
    const item2 = data.json1[1];
}

Answer №2

If you don't have a fixed length or any clear way to determine where the data should be separated, you will have to handle it differently:

One approach could be to merge the two arrays into a single array:

$mergedResult = array_merge($jsonArray1, $jsonArray2);
echo json_encode($mergedResult);

then on the client side

$.getJSON('./tasksasd32', function (data) {
    const mergedData = data;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Unable to locate the specified script using node.js

Recently, I've started working with Javascript and Node.js. My current project is utilizing OpenMCT (https://github.com/nasa/openmct) and I'm facing an issue when trying to integrate a script as a plugin in an index.html file. Upon starting the N ...

Exploring the possibilities of handling multidimensional numeric arrays with jQuery's each function

I have a multidimensional numeric array that I need to process in AJAX-jQuery using the jQuery each function. I've been trying to use parseJSON but the alert always shows undefined as the result. So, I haven't encoded this array in order to proce ...

Fetching data from an array object in a JSON API using React.js

I am facing the challenge of retrieving data from a JSON API where the data is stored in an Array with object types, and it contains timestamps. My goal is to convert these timestamps into time format using JavaScript. Take a look at the code snippet belo ...

Is there a way to incorporate a JavaScript variable as the value for CSS width?

I created a scholarship donation progress bar that dynamically adjusts its width based on the amount donated. While I used CSS to handle the basic functionality, I am now trying to implement JavaScript for more advanced features. My goal is to calculate ...

Unable to simultaneously execute TypeScript and nodemon

Currently, I am in the process of developing a RESTful API using Node.js, Express, and TypeScript. To facilitate this, I have already installed all the necessary dependencies, including nodemon. In my TypeScript configuration file, I made a modification to ...

Accelerating web data extraction using selenium

I have developed a small application that retrieves prices of Reserved Instances from the AWS website and then displays the name of the instance along with its price (specifically targeting Convertible 3-year terms). The app is functional, but it runs ve ...

Update a variable in one component class using another component

My Hue class in a component contains a variable that I'm trying to modify: export default class Hue extends Component { state = { toggleState : false, toggleWhite : false } render(){...} ... } Within this component, I can chang ...

Avoid unnecessary re-renders in ReactJS Material UI tabs when pressing the "Enter

I've created a user interface with tabs using material-ui in reactJS. The issue I'm facing is that every time a tab is selected, the content under that tab reloads, causing performance problems because there's an iFrame displayed in one of t ...

PhpStorm alerts users to potential issues with Object methods within Vue components when TypeScript is being utilized

When building Vue components with TypeScript (using the lang="ts" attribute in the script tag), there is a warning in PhpStorm (version 2021.2.2) that flags any methods from the native JavaScript Object as "Unresolved function or method". For exa ...

What is the process for renaming folders with files in node.js?

The current method is effective for renaming a single folder with no files, but it fails when trying to rename a folder containing one or more files. const handleRenameFile = () => { const oldPath = `./${directory}/${fileName}`; const newPath = ...

connect a column from a separate array in pdfmake

In my current project, I am looking to link the values of an array that is different from the one present in the initial two columns. Is this achievable? (The number of partialPrice values aligns with the number of code entries). Despite several attempts ...

When attempting to read data from a JSON file using a BufferedReader, it consistently outputs only

Currently immersing myself in the world of android development with the help of a book titled "Android Programming - The Big Nerd Ranch Guide". As part of a learning project, we are creating a Json serializer to save and load data. When writing the file, e ...

What is the proper way to add an object to an array within an object in TypeScript?

import {Schedule} from './schedule.model'; export class ScheduleService{ private schedules:Schedule[]=[ new Schedule("5:00","reading"), new Schedule("6:00","writing"), new Schedule("7:00","cleaning") ]; getSchedule(){ ret ...

Guide on importing a Blender scene into three.js with textures, lighting, and camera

I successfully exported a single object (box.json) and scene (box2.json) from Blender using io_three. Additionally, I was able to load a single object (box.json) with textures using JSONLoader() in the modelWithTexture.html file. My goal is to load an ent ...

What is the purpose of Python creating a JSON file from a string?

I am trying to create a json file by combining data from an api request and another json file. However, I am facing issues with the generated json file as it contains double quotes around the braces and escape characters like "\n" and "\r" scatte ...

Executing <script> tags inside <template> on vue js

I have encountered a problem while trying to add an Iframe from a service I am using. The content comes within script tags, but when I try to insert them into Vue 3 framework, I encounter an error. I have searched for a solution and came across a thread o ...

Unable to successfully remove item by ID from mongoDB within a Next.js application

Currently, I am developing an application using NextJS for practice purposes. I'm facing challenges in deleting single data from the database with the findByIdAndDelete function. Error encountered: CastError: Cast to ObjectId failed for value "undef ...

There was an issue with the origin null not being permitted by Access-Control-Allow-Origin

Possible Duplicate: XmlHttpRequest error: Origin null is not allowed by Access-Control-Allow-Origin I am currently working on a weather application that runs smoothly in browsers. However, when I try to deploy it on my Android phone, it encounters iss ...

JsonConverting, JObjecting, JPropertying

I have a code snippet here that generates NewtonSoft Jobjects JObject o = new JObject(new JProperty("DataSources", new JArray(from p in repDatasource.DataSources select JObject( JProperty("Columns", new JArray (from q in p.columns select new JObject(new J ...

Laravel 4 - Error: Uncaught TypeError - Unable to access property 'post' as it is undefined

Here is the script I am using: <script> function selectModSetProd(prodId,setId,objControl){ function ifOK(r){ objControl.style.background="'"+r.color+"'"; } function ifError(e){ alert( ...