【问题标题】:Obtain KEY Value Array in variable PHP获取变量PHP中的KEY值数组
【发布时间】:2021-09-01 19:43:19
【问题描述】:

您好,我有这个数组代码,我需要得到:user_id": 4 我只需要从user_id获取值 4

我有这个代码,但它不起作用

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

{
    "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczovL3NpdGUudGVzdC9wdWJsaWMvYXBpL2xvZ2luIiwiaWF0IjoxNjIzODExMTE1LCJuYmYiOjE2MjM4MTExMTUsImp0aSI6Ind2bExlTlJHdkdpU2xmVUYiLCJzdWIiOjIsInBydiI6Ijg3ZTBhZjFlZjlmZDE1ODEyZmRlYzk3MTUzYTE0ZTBiMDQ3NTQ2YWEifQ.-Q56kEsHZgVEtaMzSS9Ub11imYjWjFbJMAug3Wx7WTg",
    "user": {
        "success": true,
        "data": {
            "id": 2,
            "auth_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczovL3NpdGUudGVzdC9wdWJsaWMvYXBpL2xvZ2luIiwiaWF0IjoxNjIzODExMTE1LCJuYmYiOjE2MjM4MTExMTUsImp0aSI6Ind2bExlTlJHdkdpU2xmVUYiLCJzdWIiOjIsInBydiI6Ijg3ZTBhZjFlZjlmZDE1ODEyZmRlYzk3MTUzYTE0ZTBiMDQ3NTQ2YWEifQ.-Q56kEsHZgVEtaMzSS9Ub11imYjWjFbJMAug3Wx7WTg",
            "name": "Leo",
            "email": "ventas@nikostore.net",
            "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
}

【问题讨论】:

  • 如果您希望我们回答您的问题,请至少花时间以可读的方式格式化您的数据。
  • 我该如何做和订购我的阵列?
  • 好吧,您可以将该 JSON 字符串粘贴到 JSONLint 并单击 Validate JSON。它会检查语法并将字符串重新格式化为更易读的格式,您可以将其复制到您的问题中。

标签: php arrays laravel


【解决方案1】:

使用foreach 循环。

假设您已将数组存储在变量 $array 中。

代码:

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

你会得到user_id为4的整个对象,你可以通过

$user_id = $user['user_id];

希望这会有用。

【讨论】:

  • 您好,谢谢!我已经创建了一个 foreach,我可以在另一个 foreach 中创建一个 foreach 吗?
  • user_id 表中的值“4”根据用户,所以它的 foreach 无效:(
  • 值 4 变化取决于用户登录 :( 只需要数组中的搜索键 user_id
  • 什么数组?此 JSON 返回一个对象。即使您将数据强制放入数组中,这也不会让您将结构降低到user_id
【解决方案2】:

使用您在此处发布的 JSON 字符串,您需要先使用 json_decode() 对其进行解码。假设未设置任何标志,这将解码为 JSON 字符串中表示的对象和数组的混合。

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

$request = json_decode($str);

从那里到user_id 的路径是:

$request->order[0]->addon_categories[0]->user_id  // 4

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多