doushan6692 2015-12-07 06:26
浏览 266

如何从多维数组中仅获取数组值

I am doing project in laravel. I am fetching a value from database in user1 variable.

foreach($users1 as $us){
                    $uss[$i] = $us->city_id;
                    $users2[$i] = DB::table('provider_city')->select('provider_id')->where('city_id','=', $uss[$i])->get();
                    $i++;
                }
return $users2;

when I return user2, I am getting [[{"provider_id":"14785"}],[{"provider_id":"125478"}]] such values.

I want only values like ['14785','125478'].

I know this may be very simple. Give suggestions.

  • 写回答

3条回答 默认 最新

  • dongxin0031 2015-12-07 06:30
    关注

    This can be achieved with json_decode, like so

    $json= '[
        {
          "provider_id": "14785"
        },
        { 
          "provider_id": "125478"
        }
      ]';
    
    $jsonDecoded = json_decode($json);
    foreach($jsonDecoded as $item)
    {
        //Do something with it, e.g
        echo $item->provider_id;
    }
    

    Edit: Upon finding out that this is a multidimensional array

    This question here should point you in the right direction.

    评论

报告相同问题?

悬赏问题

  • ¥15 用C语言输入方程怎么
  • ¥15 网站显示不安全连接问题
  • ¥15 github训练的模型参数无法下载
  • ¥15 51单片机显示器问题
  • ¥20 关于#qt#的问题:Qt代码的移植问题
  • ¥50 求图像处理的matlab方案
  • ¥50 winform中使用edge的Kiosk模式
  • ¥15 关于#python#的问题:功能监听网页
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题