How do I echo only certain values from given array?

Hi,

I want to know how to use only certain values of an array in a function like array_values.
The array contains over 180 values, but I only need to display six. It’s for a project where I’m exporting an SQL query to Excel. Here’s some code to give you all some context.

[php]
$result = odbc_exec($conn, $query);
$row = odbc_fetch_array($result);

array_values($row); // I’ve tried array_values($row[‘example’]) and array_values($row, ‘example’)

// There is obviously much more to the code, but I thought I’d only show the area where I’m having a problem

[/php]

Maybe look at
if in array function
if you know the values you want.

array_values will always just give you an array of all the values, essentially it just replaces the array keys with numeric indexes. Do you just need to get the first 6? Or are there particular columns you are interested in?

If you just need to grab first 6 then try this:

[php]
// This will just give you the first 6 values
$values = array();
$counter = 0;
foreach($row as $value)
{
$values[] = $value;
if(++$counter == 6) break;
}

// Now $values contains the first 6 values
[/php]

Hi aaronszy, I need particular collumns. The problem is that the data base I’m pulling from has over 180 collumns and I need collumns that are spread out within that 180. Is there any way to acheive this?

if your exporting from sql is excel in your query specify which columns you want from the database.

Wow, I feel so stupid for not even thinking about that, thank you very much daveismyname. That makes my entire job a ton easier!

sometimes just takes a fresh pair of eyes :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service