I displayed the data from database using the following code
$mani = DB::select('select id from employee');
Output :
Array (
[0] => stdClass Object (
[id] => 1
)
[1] => stdClass Object (
[id] => 2
)
[2] => stdClass Object (
[id] => 3
)
[3] => stdClass Object (
[id] => 4
)
)
I need the following Exact output :
Array (
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
Please share the solution. Thank you
Change your code to use pluck() instead, so change
$mani = DB::select('select id from employee');
to
$mani = DB::table('employee')->pluck('id');
If you are using laravel-5.2, then change
$mani = DB::table('employee')->lists('id');
But if you are using laravel 5.2 and greater version
$mani = DB::table('employee')->plunk('id');
Related
I have $result variable assigned with collection of array objects like this :
// below is the $result value i.e. output of print_r($result);
Illuminate\Support\Collection Object ( [items:protected] => Array ( [0] => stdClass Object ( [email] => test#test.com ) [1] => stdClass Object ( [email] => test#test.com ) [2] => stdClass Object ( [email] => test#test.com ) [3] => stdClass Object ( [email] => test#test.com ) [4] => stdClass Object ( [email] => test#test.com ) [5] => stdClass Object ( [email] => test#test.com ) ) )
In order to covert above to simple array I am using loop like below :
print_r($result);
$user_emails = array();
foreach ($result as $key => $value) {
array_push($user_emails,$value->email);
}
print_r($user_emails);
and I get $user_emails value as below :
Array ( [0] => test#test.com [1] => test#test.com [2] => test#test.com [3] => test#test.com [4] => test#test.com [5] => test#test.com )
I want to know if there is better and faster way to achieve what I have done above, using any of the laravel collection method.
You can try that:
$result->pluck('email')->all();
finally this worked for me :
$result->pluck('email')->toArray();
I have Laravel 6 code below displays the array with the object format. So I like to display instead of object-based array, just array as I have mentioned below.
$ins_affiliations = DB::table('ins_affiliations as af')
->select('af.id','af.name')
->where(['af.ins_category_id' => $ins_category_type[0]->category_id])
->get()->toArray();
my code shows
Array
(
[0] => stdClass Object
(
[id] => 12
[name] => NSDC
)
[1] => stdClass Object
(
[id] => 13
[name] => NCVT
)
[2] => stdClass Object
(
[id] => 14
[name] => AICTE
)
[3] => stdClass Object
(
[id] => 15
[name] => Others
)
)
what I need
Array
(
[12] => NSDC
[5] => NCVT
[8] => AICTE
[11] => Others
)
How can I achieve this?
You can remove ->toArray() when you retrieve data and then use mapWithKeys which is collection method
$ins_affiliations->mapWithKeys(function ($item) { return [$item->id => $item->name]; })
Then you can use ->all() if you want to get plain array
My whole moto in this question is to make a new array from existing array and to reverse the first and second index of array.
I have an array
Array
(
[0] => Array
(
[0] => Array
(
[field] => Array
(
[name] => name
[tabindex] => 0
)
[colspan] => 3
)
)
[1] => Array
(
[0] => Array
(
[field] => Array
(
[name] => sequence
[tabindex] => 0
)
[colspan] => 3
)
)
[2] => Array
(
[0] => Array
(
[field] => Array
(
[name] => description
[tabindex] => 0
)
[colspan] => 3
)
)
[3] => Array
(
[0] => Array
(
[field] => Array
(
[name] => status
[tabindex] => 0
)
[colspan] => 3
)
)
[4] => Array
(
[0] => Array
(
[field] => Array
(
[name] => modified_by_name
[customCode] => {$fields.date_modified.value} {$APP.LBL_BY} {$fields.modified_by_name.value}
[label] => LBL_DATE_MODIFIED
[tabindex] => 0
)
[colspan] => 3
)
)
[5] => Array
(
[0] => Array
(
[field] => Array
(
[name] => created_by_name
[customCode] => {$fields.date_entered.value} {$APP.LBL_BY} {$fields.created_by_name.value}
[label] => LBL_DATE_ENTERED
[tabindex] => 0
)
[colspan] => 3
)
)
[6] => Array
(
[0] => Array
(
[field] => Array
(
[name] =>
)
)
[1] => Array
(
[field] => Array
(
[name] =>
)
)
)
)
Now i have to make a new array with first index on second and second on first index like this But have to do only in smarty not in php etc
Array
(
[0] => Array
(
[0] => Array
(
[field] => Array
(
[name] => name
[tabindex] => 0
)
[colspan] => 3
)
[1] => Array
(
[field] => Array
(
[name] => sequence
[tabindex] => 0
)
[colspan] => 3
)
[2] => Array
(
[field] => Array
(
[name] => description
[tabindex] => 0
)
[colspan] => 3
)
[3] => Array
(
[field] => Array
(
[name] => status
[tabindex] => 0
)
[colspan] => 3
)
[4] => Array
(
[field] => Array
(
[name] => modified_by_name
[customCode] => {$fields.date_modified.value} {$APP.LBL_BY} {$fields.modified_by_name.value}
[label] => LBL_DATE_MODIFIED
[tabindex] => 0
)
[colspan] => 3
)
[5] => Array
(
[field] => Array
(
[name] => created_by_name
[customCode] => {$fields.date_entered.value} {$APP.LBL_BY} {$fields.created_by_name.value}
[label] => LBL_DATE_ENTERED
[tabindex] => 0
)
[colspan] => 3
)
)
)
I think the following might help you.
You could loop the array you have and fill a new array with the values. Lets call the original $firstArray and the new one $newArray. I see that the first item from the subarrays are what you need(and there are no other items apart from 0) so you could do the following:
{$newArray = []}
{foreach $firstArray as $item}
{$newArray[] = $item.0}
{/foreach}
$item.0 selects the first subarray(there are no other). You might have to use $item[0] depending on your smarty version.
If there is more than 1 subarray, then loop $items first before you add an array to the $newArray.
It looks as if there's another array encapsuling your firstArray, so perhaps you need to select the subarray from the main one first....
note: I used smarty 3. Perhaps you use a different version, be sure to check the syntax for your version.
Array
(
[0] => stdClass Object
(
[id] => 16
[department_id] => 4
[employee_status] => 1
)
[1] => stdClass Object
(
[id] => 17
[department_id] => 8
[employee_status] => 1
)
)
i found a solution that sort object array
function cmp($a, $b) {
return $b->department_id - $a->department_id;
}
$sirtedArray=usort($employeelist, "cmp");
I am getting the result below. i want to print the question and cat_id but don't know how??? Actually i'm new in codeigniter. Any help will be appreciated .
Array
(
[data] => Array
(
[0] => Array
(
[records] => Array
(
[0] => stdClass Object
(
[qst_id] => 1
[question] => Describe a major change that occurred in a job that you held. How did you adapt to this change?
[cat_id] => 1
)
[1] => stdClass Object
(
[qst_id] => 2
[question] => Tell about a situation in which you had to adjust to changes over which you had no control. How did you handle it?
[cat_id] => 1
)
)
[cat_id] => 1
)
[1] => Array
(
[records] => Array
(
)
[cat_id] => 4
)
[2] => Array
(
[records] => Array
(
[0] => stdClass Object
(
[qst_id] => 3
[question] => Give an example of a situation where others were intense but you were able to maintain your composure.
[cat_id] => 6
)
)
[cat_id] => 6
)
)
)