select column value from row by column index – laravel php

I want to select column value from a row by column index. I am trying to fetch data from a mysql database by using laravel php framework. I am able to select column value by its name but I didn’t find a way to select by column index.

<?php
    namespace AppHttpControllers;
    
    use IlluminateHttpRequest;
    
    use IlluminateSupportFacadesDB;
    
    class HomeController extends Controller
    {
        public function sample_name()
        {
            $page_title = 'Page1';
            $page_description = 'Some description for the page';
            
            $action = __FUNCTION__;
    
            $table = DB::select("my select query");
    
            foreach ($table as $value) {

               // I want to select first column value from this row
               $first_column_value = .....

            }
    
            return view('home.page', compact('page_title', 'page_description','action'));
        }
    }

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

why you can’t access with index because $value is associative array, to access by index you can use array_value($array) which return the index array

foreach($table as $value){
  $value=array_values((array)$value);
  $first_column_value =$value[0];
  $second_column_value =$value[1];
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x