0

Though this is a textbook problem, but I'm not able to parse from json object. I have a json object coming from a page, and I need to extract the ID,fieldText value so that I can update the table.

Here is how I'm capturing the json and converting it into array, not sure how to extract the values.

if(isset($_POST['postData'])){
 $json = json_decode($_POST['postData'],true);
foreach ($json as $key => $value) 
{

   print_r($key);
   //print_r($value);
   foreach ($value as $k => $val)   
  {
        //echo "$k | $val <br />";
   } } 

I need to update the table with [ID] and [fieldText] :

Result should like this::(1:Hello World), (2:The rising Star),(3: Terminator)

My JSON object is like this:

Array(

[fieldName] => Array
    (
        [0] => fieldText[1]
        [1] => fieldText[2]
        [2] => fieldText[3]
    )

[fieldText] => Array
    (
        [0] => HelloWorld
        [1] => The rising Star
        [2] => Terminator

    )

[ID] => Array
    (
        [0] => 1
        [1] => 2
        [2] => 3
    ))

3 Answers 3

1

Hi it seems that there are three arrays in your JSON, I think it would be better to change the way you generate the JSON to make it simple to understand.

// assumes $arr as the $_POST['postData'] in your case
$arr = array("1"=>"Hello World", "2"=>"The Rising Star", "3"=>"Terminator"); 
$j = json_encode($arr);
$json = json_decode($j,true);
foreach ($json as $key => $value) 
{
echo '('.$key.','. $value.')';
} 

The results are :

(1,Hello World)(2,The Rising Star)(3,Terminator)

Hope this can help you.

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

1 Comment

thank you for your attempt. changing json is not possible,
1

Hope you can do something like this.

$jsonData = Array(

'Name' => Array
(
    0 => 'Text1',
    1 => 'Text2',
    2 => 'Text3'
),

'Value' => Array
(
    0 => 'HelloWorld',
    1 => 'The rising Star',
    2 => 'Terminator'

),

'ID' => Array
(
    0 => 1,
    1 => 2,
    2 => 3
)
);

$length = count($jsonData['ID']);

for($j=0;$j<$length;$j++) {
    echo "(".$jsonData['ID'][$j].": ".$jsonData['Value'][$j].")<br/>";
}

7 Comments

in which line? Show me the code you are trying please.
if(isset($_POST['postData'])){ $jsonData = json_decode($_POST['postData'],true); $length = count($jsonData['ID']); //echo "$length"; for($j=0;$j<$length;$j++) { echo "(".$jsonData['ID'][$j].": ".$jsonData['Value'][$j].")<br/>"; }
and getting error in echo "(".$jsonData['ID'][$j].": ".$jsonData['Value'][$j]."
you are using the exact code I'm using, I have provided you a way, because I don't know what are you getting in your postdata, you have to modify the indexes I used to your ones. I can help with that if you can print the postdata array and show it to me. Try var_dump($_POST['postData']) inside your if condition and provide the output in your question
the array that I have provided is the same one that I'm getting after printing the postdata array.
|
1

In my opinion you should make your life easier by modifying the json array you receive with $_POST['data'];. If I were in your shoes my json would've been exactly as you need it:

{ 1:'Hello World', 2:'The rising Star',3:'Terminator' }

But if you are not able to change it for any reason I think you could use something like this (basing this example on your code):

$jsonData = json_encode($_POST['json'], true);

$id = $jsonData['ID'];
$fieldText = $jsonData['fieldText'];

$arr = array();

for ($i=0; $i < count($id); $i++) { 
    $arr[(int) $id[$i]] = $fieldText[$i];
}

var_dump($arr);

Hope this helps you!

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.