I have a query that returns data from a database and stores the data in an array called $array
.
Using a print_r($array)
I receive data like:
Array
(
[0] => stdClass Object
(
[Item1] => 1
[Item2] => 1
[Item3] => 1
[Item4] => 'Az'
[Item5] => 222
[Item6] => 223
[Item7] => 21
[Item8] => 22
)
)
I've found I can return the keys Item1
to Item8
using:
foreach ($array[0] as $key => $a){
echo '<pre>', $key, '</pre>';
}
However I'd only like to return from Item4 to the end of the array. (remove the first 3 items)
Note that query sometimes returns multiple rows i.e. $array[0]
, $array[1]
but $array[0]
will always exist if there's a result. I only need to get the keys from the array once as I'd like to use them as column headings in the table they are displayed in.
I would prefer a foreach()
situation as I would only like to output as many columns as are present.
I've tried looking at array_slice()
but I couldn't seem to get it to work, my efforts are displayed below:
$sliced = array_slice($prices[0], 3, $preserve_keys = true);
foreach ($sliced as $key => $value){
echo '<pre>'.$key.'</pre>';
}