dsdf64562672 2016-07-21 19:13 采纳率: 0%
浏览 232
已采纳

PHP array_column多个键的顺序是一样的吗?

Im wondering if array_column keeps the array elements order so when i call the function twice in same array, i can trust the resulting arrays will match. I need to work this way because in the code I have to make many operations with only specific data (one of the keys, in the example, the id), and after all, use both of them (in example id and qty) to insert in db.

Example:

QUERY: SELECT id, qty FROM items ...
CONVERTED INTO ASSOCIATIVE ARRAY:
$array = [
  ['id' => 1, 'qty' = 2],
  ['id' => 2, 'qty' = 4]
];

$itemsIds = array_column($array, 'id');
$itemsQty = array_column($array, 'qty'); 

Can i do?:

for($i = 0; $i < count($array); $i++) {
  echo $itemsIds[$i] . $itemsQty[$i];
}

And i will always get the correct data for each item?

  • 写回答

3条回答 默认 最新

  • douyao2529 2016-07-24 09:56
    关注

    Since the DB result is row-based (usually), every column will have some value (even if it's NULL). So unless you mess with the result somehow, you should be safe. I do practically the same in some of my code.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员