Question

By using odbc_fetch_array, I am getting a query and storing it into an array

$result = odbc_exec($idconex,$consulta) or die("Query failed(-1)");
$rawdata= array();
$i=0;

while($row = odbc_fetch_array($result))
{
     $rawdata[$i] = $row;
     $i++;
}

$myArray = $rawdata;
foreach ($rawdata as $sub){
     $myArray = array_merge($myArray, $sub);
}

The resulting array is like this:

 Array
 (

    [0] => Array
        (
        [Part Number] => 51555K
        [Lote] => TJ14-K001
        [Description] => Metal Ring
        [UM] => EA
        [Location] => TEMP
        [Quantity] => 12600
        [Boxes] => 42
        [Pallet] => 1
        [PO] => 
        [SO] => 
        [status] => G
        [palletid] => 193375
    )

    [1] => Array
        (
        [Part Number] => 51555K
        [Lote] => TJ14-K001
        [Description] => Metal Ring
        [UM] => EA
        [Location] => TEMP
        [Quantity] => 12600
        [Boxes] => 42
        [Pallet] => 1
        [PO] => 
        [SO] => 
        [status] => G
        [palletid] => 193376
    )
)

With array keys from 0 to 170. I want to get the array keys from the second level. Using this example:

Find all second level keys in multi-dimensional array in php

I am able to get the second level keys, but merged with the first level keys like this:

[...first 160 array keys...]
...
[160] => 160
[161] => 161
[162] => 162
[163] => 163
[164] => 164
[165] => 165
[166] => 166
[167] => 167
[168] => 168
[169] => 169
[170] => 170
[171] => Part Number
[172] => Lote
[173] => Description
[174] => UM
[175] => Location
[176] => Quantity
[177] => Boxes
[178] => Pallet
[179] => PO
[180] => SO
[181] => status
[182] => palletid

And finally using this code:

foreach($myArray as $key => $value)
{
     if(is_numeric($key))
     {
          unset($myArray[$key]);
     }
}

I am getting the second level keys only into an array:

 Array
 (
     [0] => Part Number
     [1] => Lote
     [2] => Description
     [3] => UM
     [4] => Location
     [5] => Quantity
     [6] => Boxes
     [7] => Pallet
     [8] => PO
     [9] => SO
     [10] => status
     [11] => palletid
 )

Is there any way to get the second level keys only more efficiently? This is my actual code:

$result = odbc_exec($idconex,$consulta) or die("Query failed(-1)");
$rawdata= array();
$i=0;

while($row = odbc_fetch_array($result))
{
     $rawdata[$i] = $row;
     $i++;
}
$myArray = $rawdata;
foreach ($rawdata as $sub){
     $myArray = array_merge($myArray, $sub);
}

foreach($myArray as $key => $value){
     if(is_numeric($key))
     {
          unset($myArray[$key]);
     }
}

echo "<PRE>";
print_r (array_keys($myArray));
echo "</PRE>";
Was it helpful?

Solution

Not sure I follow all of that, but this seems like it would work instead of all of those foreachs:

$result = odbc_exec($idconex,$consulta) or die("Query failed(-1)");

while($row = odbc_fetch_array($result))
{
     $rawdata[] = $row;
}
$myArray = array_keys($rawdata[0]);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top