1

I have a Variable $deliveryname Array with the following information

    [
   {
      "id":7,
      "name":"Delivery2",
      "email":"delibery2@hotmail.com",
      "email_verified_at":null,
      "password":"$2y$10$SID2yPM.hNsgHCCFh3BeXuk1Bylqsxl39DXPdYaXKZ00FvzSXCJju",
      "remember_token":null,
      "created_at":"2021-06-15 05:07:16",
      "updated_at":"2021-06-15 05:07:16",
      "auth_token":null,
      "phone":"456465465",
      "default_address_id":0,
      "delivery_pin":"Y11FN",
      "delivery_guy_detail_id":3,
      "avatar":null,
      "is_active":1,
      "tax_number":null
   }
]

I need to get the key "name" value that would be "Delivery2"

I have this code to create the variable in php but it doesn't work

$deliveryname2 = $deliveryname["name"];

Use Laravel

John Lobo
  • 14,355
  • 2
  • 10
  • 20

2 Answers2

0

You have to decode json to get array or object

 $deliveryname='[
   {
      "id":7,
      "name":"Delivery2",
      "email":"delibery2@hotmail.com",
      "email_verified_at":null,
      "password":"$2y$10$SID2yPM.hNsgHCCFh3BeXuk1Bylqsxl39DXPdYaXKZ00FvzSXCJju",
      "remember_token":null,
      "created_at":"2021-06-15 05:07:16",
      "updated_at":"2021-06-15 05:07:16",
      "auth_token":null,
      "phone":"456465465",
      "default_address_id":0,
      "delivery_pin":"Y11FN",
      "delivery_guy_detail_id":3,
      "avatar":null,
      "is_active":1,
      "tax_number":null
   }
]';

   $result=json_decode($deliveryname);
   dd($result[0]->name);

To get array then you have pass additional param to json_decode to true

 $result=json_decode($deliveryname,true);
   dd($result[0]['name']);
John Lobo
  • 14,355
  • 2
  • 10
  • 20
  • First of all I thank you too much !! It worked only missing the [0] What does that [0] mean? Why is it there? – Eduardo Rafael Jun 15 '21 at 11:43
  • you have multidimensional array so . usually multidimensional array retrieved using loop . check for php array types.so you get basic idea – John Lobo Jun 15 '21 at 11:45
0

You can use json_decode

<?php
 $deliveryname='[{
      "id":7,
      "name":"Delivery2",
      "email":"delibery2@hotmail.com",
      "email_verified_at":null,
      "password":"$2y$10$SID2yPM.hNsgHCCFh3BeXuk1Bylqsxl39DXPdYaXKZ00FvzSXCJju",
      "remember_token":null,
      "created_at":"2021-06-15 05:07:16",
      "updated_at":"2021-06-15 05:07:16",
      "auth_token":null,
      "phone":"456465465",
      "default_address_id":0,
      "delivery_pin":"Y11FN",
      "delivery_guy_detail_id":3,
      "avatar":null,
      "is_active":1,
      "tax_number":null
   }]';

    $result=json_decode($deliveryname);
    echo '<pre>';
    print_r($result);
  
?>

DEMO

VIKAS KATARIYA
  • 5,867
  • 3
  • 17
  • 34