0

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.

AmarjaPatil4
  • 1,640
  • 3
  • 28
  • 41
  • what are you using to get this data ? – lagbox Dec 07 '15 at 06:28
  • In these kind of problems you need to be aware of the variable's type that you are passing to your array, in this case check the return of the ->get() method and see what you have. Probably not a string, thats why you dont have your expected result. Use dd or var_dump to check that. This is just a tip, another guy already gave you the correct answer bellow. – cbcaio Dec 07 '15 at 10:52

3 Answers3

0

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.

Community
  • 1
  • 1
James
  • 15,754
  • 12
  • 73
  • 91
  • Is there any other way to do this only using array function? – AmarjaPatil4 Dec 07 '15 at 06:34
  • This is an object, not an array. How were you wanting to do it? – James Dec 07 '15 at 06:34
  • Actually I want to compaire these values with the databse values one by one – AmarjaPatil4 Dec 07 '15 at 06:36
  • You could achieve these by putting the values here and the values retrieved from the database in to arrays and looping through them to check the values. However you know your project better than us and so would need to write this yourself. Assuming the values coming out of your database would come out in the same order then you can loop through them. – James Dec 07 '15 at 06:39
  • I tried this but I am getting error as, json_decode() expects parameter 1 to be string, array given – AmarjaPatil4 Dec 07 '15 at 07:12
  • Well that is because whatever you are providing is an array and not a string. Based on your question I assumed you were using a string, can you provide code as to how you get the json? – James Dec 07 '15 at 07:21
  • Just FYI, the "array" is likely in JSON format because Laravel automatically converts to JSON if you return a Collection from a controller method to the browser. – Joel Hinz Dec 07 '15 at 08:54
0

There's actually an Eloquent method for that - lists():

$users2[$i] = DB::table('provider_city')
    ->where('city_id','=', $uss[$i])
    ->lists('provider_id')
    ->all();

This will give you an array of just the provider ids.

Whether you need the all() call at the end or not depends essentially on what version of Laravel you're using.

Joel Hinz
  • 24,719
  • 6
  • 62
  • 75
  • I guess that all() part is placed wrong, the lists() returns an array so you won't be able to call all() to that, need to use one or another, not both. Or am I wrong? :) – cbcaio Dec 07 '15 at 10:56
  • If you get an error from the `all()`, just remove it. :) It depends on what version you're using, in later ones you do need it. If the `lists()` returns an array that should be exactly what you wanted, right? :) – Joel Hinz Dec 07 '15 at 13:39
0

Maybe something like

return DB::table('provider_city')
    ->whereIn('city_id', array_pluck($users1, 'city_id')
    ->lists('provider_id'); 
lagbox
  • 48,571
  • 8
  • 72
  • 83