Home > Enterprise >  How do i geth the full_name from this json response with php
How do i geth the full_name from this json response with php

Time:11-02

I am trying to get all the values in the custom_fields array in this json

{
  "event": "charge.success",
  "data": {
    "id": 1421960724,
    "domain": "test",
    "status": "success",
    "reference": "A94M810260502831",
    "amount": 250000,
    "message": null,
    "gateway_response": "Successful",
    "paid_at": "2021-11-01T06:40:54.000Z",
    "created_at": "2021-11-01T06:40:48.000Z",
    "channel": "card",
    "currency": "NGN",
    "ip_address": "204.14.73.41",
    "metadata": {
      "custom_fields": [{
        "display_name": "Full Name",
        "variable_name": "full_name",
        "value": "Kevil Udoh"
      }, {
        "display_name": "School Name",
        "variable_name": "school_name",
        "value": "Kelvin School"
      }, {
        "display_name": "Mobile Number",
        "variable_name": "mobile_number",
        "value": "7888384838"
      }],
      "referrer": "http://localhost/bibire/prep-class.php?msg1=register"
    }
    

i have tried the following in order to retrieve values in the metadata object. It works works fine getting from that data object. There is something Im missing?

<?php 
$obj = json_decode(myjsonabove);

$status = $obj->data->status; //this works fine
$ref = $obj->data->metadata->custom_fields->full_name; //this is not working
$amount = $obj->data->amount/100; //this works fine
?>

what really am i getting wrong. please help. So much thanks

CodePudding user response:

$ref = $obj->data->metadata->custom_fields->full_name - this won't work because you do not have a property called full_name, it's a value. Also, custom fields is an array. So as a simple access to a variable you would do $ref = $obj->data->metadata->custom_fields[0]->variable_name. But in reality, because it is an array of objects you will probably want to use array_filter() or array_map(), depending on the context.

CodePudding user response:

The json is invalid, but if you add two braces at the end, it's good.

$ php input-full-name.php 

Kevil Udoh

$ cat input-full-name.php
  <?php
  $obj = json_decode(file_get_contents("input-full-name.json"));
  foreach ($obj->data->metadata->custom_fields as $e) {
      if ($e->variable_name == "full_name") {
          print "{$e->value}\n";
      }
  }

Pro tip:


$ php -r 'print_r(json_decode(file_get_contents("input-full-name.json")));' 
stdClass Object
(
    [event] => charge.success
    [data] => stdClass Object
        (
            [id] => 1421960724
            [domain] => test
            [status] => success
            [reference] => A94M810260502831
            [amount] => 250000
            [message] => 
            [gateway_response] => Successful
            [paid_at] => 2021-11-01T06:40:54.000Z
            [created_at] => 2021-11-01T06:40:48.000Z
            [channel] => card
            [currency] => NGN
            [ip_address] => 204.14.73.41
            [metadata] => stdClass Object
                (
                    [custom_fields] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [display_name] => Full Name
                                    [variable_name] => full_name
                                    [value] => Kevil Udoh
                                )

                            [1] => stdClass Object
                                (
                                    [display_name] => School Name
                                    [variable_name] => school_name
                                    [value] => Kelvin School
                                )

                            [2] => stdClass Object
                                (
                                    [display_name] => Mobile Number
                                    [variable_name] => mobile_number
                                    [value] => 7888384838
                                )

                        )

                    [referrer] => http://localhost/bibire/prep-class.php?msg1=register
                )

        )

)

  • Related