仅查找二级数组键

时间:2014-04-28 21:12:52

标签: php arrays multidimensional-array key

通过使用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的数组键。我想从第二级获取数组键。使用这个例子:

Find all second level keys in multi-dimensional array in 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>";

1 个答案:

答案 0 :(得分:1)

不确定我是否遵循了所有这些,但这似乎可以起作用而不是所有foreach

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

while($row = odbc_fetch_array($result))
{
     $rawdata[] = $row;
}
$myArray = array_keys($rawdata[0]);