skip to Main Content

Hi I have this array code and I need to get: user_id": 4
I just need to get the value 4 from user_id

I have this code but it doesn’t work

$myvar = $request['order'][0]['user_id'];

{
    "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczovL3NpdGUudGVzdC9wdWJsaWMvYXBpL2xvZ2luIiwiaWF0IjoxNjIzODExMTE1LCJuYmYiOjE2MjM4MTExMTUsImp0aSI6Ind2bExlTlJHdkdpU2xmVUYiLCJzdWIiOjIsInBydiI6Ijg3ZTBhZjFlZjlmZDE1ODEyZmRlYzk3MTUzYTE0ZTBiMDQ3NTQ2YWEifQ.-Q56kEsHZgVEtaMzSS9Ub11imYjWjFbJMAug3Wx7WTg",
    "user": {
        "success": true,
        "data": {
            "id": 2,
            "auth_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczovL3NpdGUudGVzdC9wdWJsaWMvYXBpL2xvZ2luIiwiaWF0IjoxNjIzODExMTE1LCJuYmYiOjE2MjM4MTExMTUsImp0aSI6Ind2bExlTlJHdkdpU2xmVUYiLCJzdWIiOjIsInBydiI6Ijg3ZTBhZjFlZjlmZDE1ODEyZmRlYzk3MTUzYTE0ZTBiMDQ3NTQ2YWEifQ.-Q56kEsHZgVEtaMzSS9Ub11imYjWjFbJMAug3Wx7WTg",
            "name": "Leo",
            "email": "[email protected]",
            "phone": "+51943363373",
            "default_address_id": 3,
            "default_address": {
                "address": "mi dirección",
                "house": null,
                "latitude": "-1.327080900000001",
                "longitude": "-10.6348053",
                "tag": null
            },
            "delivery_pin": "51972",
            "wallet_balance": 0,
            "avatar": null,
            "tax_number": null
        },
        "running_order": null,
        "delivery_details": null
    },
    "order": [{
        "selectedaddons": [{
            "addon_category_name": "Comida Marina Ingredientes",
            "addon_id": "2",
            "addon_name": "Cangrejo Adicional",
            "price": "32.00"
        }],
        "id": 5,
        "restaurant_id": 12,
        "item_category_id": 2,
        "name": "Producto 1 Loquillo",
        "price": "423.00",
        "old_price": "0.00",
        "image": "https://localhost/phpmyadmin/themes/pmahomme/img/logo_left.png",
        "is_recommended": 1,
        "is_popular": 1,
        "is_new": 1,
        "desc": "<p>321312</p>",
        "placeholder_image": null,
        "is_active": 1,
        "is_veg": null,
        "order_column": null,
        "addon_categories": [{
            "id": 2,
            "name": "Comida Marina Ingredientes",
            "type": "SINGLE",
            "user_id": 4,
            "created_at": "2021-06-10 20:18:37",
            "updated_at": "2021-06-10 20:39:18",
            "description": null,
            "pivot": {
                "item_id": 5,
                "addon_category_id": 2
            },
            "addons": [{
                "id": 2,
                "name": "Cangrejo Adicional",
                "price": "32.00",
                "addon_category_id": 2,
                "user_id": 4,
                "created_at": "2021-06-10 20:20:30",
                "updated_at": "2021-06-10 20:38:51",
                "is_active": 1
            }, {
                "id": 3,
                "name": "Pene",
                "price": "95.00",
                "addon_category_id": 2,
                "user_id": 4,
                "created_at": "2021-06-10 23:52:53",
                "updated_at": "2021-06-10 23:52:53",
                "is_active": 1
            }]
        }],
        "quantity": 1
    }],
    "coupon": null,
    "location": {
        "lat": "-5.187080900000001",
        "lng": "-80.6348053",
        "address": "midreccion",
        "house": null,
        "tag": null
    },
    "order_comment": null,
    "total": {
        "productQuantity": 1,
        "totalPrice": 455
    },
    "method": "COD",
    "payment_token": "",
    "delivery_type": 1,
    "partial_wallet": false,
    "dis": 0,
    "pending_payment": false,
    "tipAmount": null
}

2

Answers


  1. Use foreach loop.

    Assuming that you have stored array in variable $array.

    Code :

    foreach($array as $arr)
    {
       if($arr['user_id'] == 4)
       {
           $user = $arr; 
       }
    }
    

    You will get the entire object which has user_id 4, you can get related column by

    $user_id = $user['user_id];
    

    Hope this will be useful.

    Login or Signup to reply.
  2. Working from the JSON string you posted here, you need to decode it first with json_decode(). Assuming no flags are set, this will decode to a mix of objects and arrays as represented in the JSON string.

    $str = '{"token": "ey...WTg",
             "user": {
              ...
             }
            }'; //The JSON string you posted in the question
    
    $request = json_decode($str);
    

    From there the path to the user_id is:

    $request->order[0]->addon_categories[0]->user_id  // 4
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search