2
$ActionData = '{"1":"muk",
                    "2":"goy",
                    "3":"sag",
                    "formname":"leadform1",
                    "skip":"true"
                  }';
    $form_field = json_decode($ActionData);
    print_r($form_field);
    echo $form_field->1;

I have an error on echo $form_field->1; what am i making mistake there

kamal pal
  • 4,187
  • 5
  • 25
  • 40
Mukesh Goyal
  • 423
  • 5
  • 9

4 Answers4

4

Use curly braces.

echo $form_field->{1};

Total Program:

<?php
$ActionData = '{"1":"muk",
                    "2":"goy",
                    "3":"sag",
                    "formname":"leadform1",
                    "skip":"true"
                  }';
$form_field = json_decode($ActionData);
echo '<pre>';
print_r($form_field);
echo '</pre>';

echo '<pre>';
echo $form_field->{1};
echo '</pre>';
?>

Output:

stdClass Object
(
    [1] => muk
    [2] => goy
    [3] => sag
    [formname] => leadform1
    [skip] => true
)

muk
Pupil
  • 23,834
  • 6
  • 44
  • 66
3

just add true in your code like

$form_field = json_decode($ActionData, true);

it works for me

2

use json_deocde with true as second parameter and access the associative array.

$ActionData = '{"1":"muk",
                    "2":"goy",
                    "3":"sag",
                    "formname":"leadform1",
                    "skip":"true"
                  }';
    $form_field = json_decode($ActionData, true);
    echo $form_field[1]; //muk

Also you can do it using the curly braces as Pupil suggest.

Murad Hasan
  • 9,565
  • 2
  • 21
  • 42
0

Access the element as below.

echo $form_field['1'];
Imesha Sudasingha
  • 3,462
  • 1
  • 23
  • 34