仅查找二级数组键



通过使用odbc_fetch_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);
}

得到的数组如下:

 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
    )
)

使用从0到170的数组键。我想从第二级获得数组键。使用此示例:

在php 中查找多维数组中的所有二级密钥

我可以获得第二级密钥,但与第一级密钥合并如下:

[...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

最后使用这个代码:

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

我只将二级密钥放入一个数组:

 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
 )

有什么方法可以更有效地获得二级密钥吗这是我的实际代码:

$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>";

不确定我是否遵循了所有这些,但这似乎可以代替所有foreach s:

$result = odbc_exec($idconex,$consulta) or die("Query failed(-1)");
while($row = odbc_fetch_array($result))
{
     $rawdata[] = $row;
}
$myArray = array_keys($rawdata[0]);

相关内容

  • 没有找到相关文章

最新更新