dongying3744 2019-01-08 14:55
浏览 27
已采纳

将动态数组与动态键组合到单个数组中

I have this dynamic multiple arrays that I need to combine in one array and serialized them. The problem is I need to keep both key and value.

$arr = array($bet_option_id => $bet_option_name);

Here i need to keep both bet_option_id AND bet_option_name. Then this result output:

Array ( [997650802] => Over 2.5 ) 
Array ( [997650807] => Yes )

This need to be simply

Array
(
  [997650802] => Over 2.5
  [997650807] => Yes
)

As it's dynamic, sometimes not comes with just single array so apparently I couldn't get it working. I need to retrieve both bet_option_id & bet_option_name. Tried something like this:

 $arr = array($bet_option_id => $bet_option_name); //This is where all array keys, values are stores
 $result = array();
 foreach ($arr as $array) {
     $result = array_merge($result, $array);
  } 

Any inputs will be nice.

  • 写回答

2条回答 默认 最新

  • douyi7055 2019-01-08 15:11
    关注

    Rather than create individual arrays like...

    $arr = array($bet_option_id => $bet_option_name);
    

    If you first create an empty array ( like you do with $result)

    $arr = array();
    

    and then add each item in using

    $arr[$bet_option_id] = $bet_option_name;
    

    Then you don't need to manipulate the array after - just create it as you want it in the first place.

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

报告相同问题?

悬赏问题

  • ¥15 为啥画版图在Run DRC会出现Connect Error?可我Calibre的hostname和计算机的hostname已经设置成一样的了。
  • ¥20 网站后台使用极速模式非常的卡
  • ¥20 Keil uVision5创建project没反应
  • ¥15 mmseqs内存报错
  • ¥15 vika文档如何与obsidian同步
  • ¥15 华为手机相册里面的照片能够替换成自己想要的照片吗?
  • ¥15 陆空双模式无人机飞控设置
  • ¥15 sentaurus lithography
  • ¥100 求抖音ck号 或者提ck教程
  • ¥15 关于#linux#的问题:子进程1等待子进程A、B退出后退出(语言-c语言)