douzhi0107 2012-05-16 11:13
浏览 222
已采纳

PHP - 删除一个数组层

I have a simple PHP function that will grab information from a database based on a unique ID:

function oracleGetGata($query, $id="id") {
    global $conn;

    $results = array();
    $sql = OCI_Parse($conn, $query);
    OCI_Execute($sql);
    while ( false!==($row=oci_fetch_assoc($sql)) ) {
        $results[ $row[$id] ] = $row;
    }
    return $results;
}

So for example $array = oracleGetData('select * from table') would return:

[1] => Array
(
    [Title] => Title 1
    [Description] => Description 1
)
[2] => Array
(
    [Title] => Title 2
    [Description] => Description 2
)

This is fine, however, if I just want to return one record $array = oracleGetData('select * from table where id = 1') it returns the data like:

[] => Array
(
    [Title] => Title 1
    [Description] => Description 1
)

Not only am I unsure of how to reference this (there is nothing identifying the array) I'd also like to somehow change my function so if it just returns one record, it will just be a simple one dimensional array.

I've looked into the usual PHP array functions but can't find anything that'll help, nor an easy way of identifying how deep the array is.

Would really appreciate any help, thank you

  • 写回答

3条回答 默认 最新

  • dotif64826 2012-05-16 11:18
    关注

    Use array_pop():

    $array = array_pop(oracleGetData('select * from table where id = 1'));
    

    You will then get one single array:

    Array
    (
        [Title] => Title 1
        [Description] => Description 1
    )
    

    Instead of an array embedded in another one:

    Array
    (
        [] => Array
        (
            [Title] => Title 1
            [Description] => Description 1
        )
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记