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.

2
  • what are you using to get this data ? Commented Dec 7, 2015 at 6: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. Commented Dec 7, 2015 at 10:52

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Is there any other way to do this only using array function?
This is an object, not an array. How were you wanting to do it?
Actually I want to compaire these values with the databse values one by one
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.
I tried this but I am getting error as, json_decode() expects parameter 1 to be string, array given
|
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.

2 Comments

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? :)
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? :)
0

Maybe something like

return DB::table('provider_city') ->whereIn('city_id', array_pluck($users1, 'city_id') ->lists('provider_id'); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.