Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get object of specific integer element [duplicate]

Tags:

arrays

object

php

$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

like image 590
Mukesh Goyal Avatar asked Feb 15 '26 23:02

Mukesh Goyal


2 Answers

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
like image 127
Pupil Avatar answered Feb 17 '26 11:02

Pupil


just add true in your code like

$form_field = json_decode($ActionData, true);

it works for me

like image 43
Vijay Pratap Singh Avatar answered Feb 17 '26 11:02

Vijay Pratap Singh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!