PHP Unleashed: Unraveling the Mysteries

What is the Best Way to Extract Data from This Array? I Need to display the First Object of My Array, Specifically the id field in the 3rd Row.

 <?php
    $con=mysqli_connect("localhost","root","","arrayy");
    // Check connection
    if (mysqli_connect_errno())
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }
 $sql="SELECT `survey_answers` FROM `user_survey_start`";

if ($result=mysqli_query($con,$sql)){

 while ($row = mysqli_fetch_row($result)){

    $json = $row[0];
    $jason_array  = json_decode($json,true);
    foreach ($jason_array as $data){
        $id[] = $data['id'];
        //$answer[] = $data['answer'];
      // $type[] = $data['type'];

        // code here for inserting/updating values in database column
    }

    echo implode(',',$id)."</br>";
   // echo implode(',',$answer);
  //  echo implode (',',$type);
  }

}
mysqli_close($con);
?>

Please also refer to this Image for the Output https://i.stack.imgur.com/GRrl0.jpg

Answer №1

Make sure to reinitialize the $id array before using it. Give this a try:

<?php
    $con=mysqli_connect("localhost","user","","database");
    
    // Check connection
    if (mysqli_connect_errno()) {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
 
    $sql="SELECT `survey_answers` FROM `user_survey_data`";

    if ($result=mysqli_query($con,$sql)){
        
        while ($row = mysqli_fetch_row($result)){
            $json = $row[0];
            $jason_array  = json_decode($json,true);
            $id = array();
            
            foreach ($jason_array as $data){
                $id[] = $data['id'];
                //$answer[] = $data['answer'];
                // $type[] = $data['type'];

                // Insert/update values in database column
            }

            echo implode(',',$id)."</br>";
           // echo implode(',',$answer);
           // echo implode (',',$type);
      }
}
mysqli_close($con);
?>

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

Consolidate all data connected to the specified key from a JSON dataset

Looking at the json string presented below [{"_id":"9/17/2015","amt1":0,"amt2":13276.5},{"_id":"9/18/2015","amt1":8075,"amt2":6445.5}] The expected outcome is: [{"_id": ["9/17/2015", "9/18/2015"], "amt1": [0, 8075], "amt2": [13276.5, 6445.5]}] Is there ...

How can I retrieve the Google Maps URL containing a 'placeid' using AJAX?

I have a specific URL that I can access through my browser to see JSON data. The URL appears as follows: https://maps.googleapis.com/maps/api/place/details/json?placeid=ChIJZeH1eyl344kRA3v52Jl3kHo&key=API_KEY_HERE However, when I attempt to use jQuer ...

The eBay Trading API's AddFixedPriceItem function is triggering an error message stating, "Invalid input data. The data provided for the tag <Item.ShippingDetails> is not valid."

Can you help me with listing shipping details in either UK or France using the eBay AddFixedPriceItem API call? I am currently encountering an error with my code: <?xml version="1.0" encoding="utf-8"?> <AddFixedPriceItemRequest xmlns="urn:ebay ...

A guide on linking a jQuery dialog to an HTML element once the data has been updated with an AJAX response

On my PHP page, I have implemented a jQuery script to open a dialog window. The code for this is as follows: <script type="text/javascript"> $(document).ready(function() { var $loading = $('<img src="loading.gif" ...

What is causing the failure of success function commands to be executed?

Currently, I am facing some inconsistencies between my locally hosted app on WAMP (working perfectly) and the deployed version of my app. In an attempt to resolve this issue, I am working with CodeIgniter and jQuery AJAX. As part of my troubleshooting proc ...

Searching for specific values within a JSON field that contains an array of objects can be accomplished using Eloquent in Laravel

I am attempting to search within a JSON field using Eloquent, but unfortunately, the search is not yielding any results. This setup is on an Ubuntu server with PostgresSQL, Laravel 5.8, and Apache 2. [{ "value": "1", "label": "number" },{ "va ...

Utilizing the bootstrap grid system, we can arrange three tables horizontally in

Can someone help me figure out how to print 3 tables per row on a page using the bootstrap grid layout? Currently, all tables are being printed vertically. Here is the code snippet: foreach($allmonths as $ind){ echo "<table>"; //echo "res ...

How to decode a JSON object in Golang that contains both trait and data?

I have a dictionary data structure containing words and their meanings in the format {{"word name":"word meaning"},{"word name":"word meaning"},...}. I am attempting to convert this into a map of the words, using the interface{} type. However, I am havin ...

easy method for creating a hyperlink that triggers a "download" pop-up box

Is there a simple and efficient way to have some of my links trigger the 'save file as' prompt (similar to right-clicking) immediately after they are clicked for the first time? ...

Modifying CSS using jQuery in a PHP While Loop

I've been racking my brain trying to solve this issue, experimenting with different approaches but so far, no luck. Question: How can I dynamically change the color of a specific div within a PHP while loop using jQuery after receiving an AJAX respon ...

`AJAX jQuery for efficient file uploads`

I am struggling to upload a file input using jQuery ajax without causing the page to refresh. Here is my HTML form: <form name="uploadform" id="uploadform" method="post" enctype="multipart/form-data"> <div name="profileBiodata" id="profileBioda ...

Issues encountered with jQuery Form Plugin failing to pass a string value to PHP

I'm currently experimenting with the jQuery form plugin (http://jquery.malsup.com/form) to handle photo uploads and previews. However, I'm encountering an issue where the data strings expected to be sent to PHP using the ajaxForm function are com ...

Decipher a complicated JSON object

Encountered an issue at work involving parsing a JSON file to populate a table. Struggling with extracting specific data from the JSON. Here's a snippet of the code: NSData *jsonData = [NSData dataWithContentsOfURL:url]; NSDictionary* json = [NSJSONS ...

Making changes to a JSON File

I've been attempting to integrate a jQuery gantt chart into my WordPress website as a plugin. However, I've hit a roadblock when it comes to editing the data.json file. My approach involves using a PHP form to add a new item. Upon form submission ...

When using PHP's `json_encode()`, don't forget to append a "1" at the

While utilizing json_encode in my project, I have encountered an issue that is perplexing. On one particular page where I make an ajax call, the resulting json seems to mysteriously add a 1 to the end of the string. The output of my return string appears ...

How can I load a specific div region using ajax for submitting a form?

I'm not sure if my approach is correct. On my main page, I have some information displayed. When you click a button, the form will load from a separate file: $('#viewport').load('/views/myView.php #targetDiv') This works fine ...

Is it better to DeepCopy the List<T> instead of instantiating new instances from JSON?

Having a List of objects such as List<Employee>, each employee in the list is loaded from a .JSON file. I also need to generate new instances based on List<Employee> and only modify the properties of the new instance for the new employee creati ...

What is the best way to retrieve the JSON data from a POST request made through AJAX to a PHP file and save it in an array variable?

My ajax request sends JSON data to a PHP file named 'receive.php'. user_name , user_id, etc. are defined at the beginning of my script but can be changed to anything else. Below is the JavaScript code I am using: const data = { name: user_na ...

AJAX responses sans the use of jQuery

Similar Question: How can I achieve this through AJAX? After my previous attempt at asking this question, where I was not clear enough, I am making another effort to be as specific as possible. To start with, I have my data encoded using the json_enc ...

Unraveling JSON data in AngularJS

After receiving a response from an OData service in JSON format, the data looks like this: [ { "id":1, "ProductName":"Surface Pro 2" }, { "id":2, "ProductName":"iPad" }, ] When att ...