doubanduo7620 2014-08-20 06:57
浏览 10
已采纳

通过匹配键组合多个不相等的数组并获取匹配键的值

i want to combine 6 indexed array by matching them by keys then insert the value into my tables how can i do it?

[typeSex] => Array
    (
        [0] => broiler
        [1] => layer
    )    

[noheads] => Array
    (
        [0] => 123
        [1] => 321
    )    

[origin] => Array
    (
        [0] => asd
        [1] => sda
        [2] => 
        [3] => 
    )    

[live] => Array
    (
        [0] => 123
        [1] => 222
    )    

[carcass] => Array
    (
        [0] => 312
        [1] => 341
        [2] => 
        [3] => 
    )

[totalcarcass] => Array
    (
        [0] => 231
        [1] => 321
        [2] => 
        [3] => 
    )

i can combine only two arrays here's the code that i got in another question

$typeSex = //the typeSex array;   
$noheads = //the noheads array;
$temp_array = array();
foreach($typeSex as $k => $v){
    if(array_key_exists($k, $noheads)){
        $temp_array[$k] = array($v, $noheads[$k]);
    }
}

please help me with this, thanks.

  • 写回答

3条回答 默认 最新

  • drk7700 2014-08-20 07:34
    关注

    Based on your question, is your desired output is?:

    array[
          [typeSex=>'broiler', noheads=>123, origin=>'asd', live=>123, carcass=>312, totalcarcass=>231],
          [typeSex=>'layer', noheads=>321, origin=>'sda', live=>222, carcass=>341, totalcarcass=>321],
          [typeSex=>null, noheads=>null, origin=>null, live=>null, carcass=>null, totalcarcass=>null],
          [typeSex=>null, noheads=>null, origin=>null, live=>null, carcass=>null, totalcarcass=>null],
    ]
    

    If yes, then this might be the answer:

            $number_of_items = [];
            $result = [];
            //get the number of items in every array and put it in an array
            array_push($number_of_items, count($typeSex), count($noheads), count($origin), count($live), count($carcass), count($totalcarcass));
    
            //get the largest number of items
            $max = max($number_of_items);
    
            //add the result variable $result
            for($i=0; $i < $max; $i++){
                array_push($result, array(
                     "typeSex" => isset($typeSex[$i])? $typeSex[$i] : null,
                     "noheads" => isset($noheads[$i])? $noheads[$i] : null,
                     "origin" => isset($origin[$i])? $origin[$i] : null,
                     "live" => isset($live[$i])? $live[$i] : null,
                     "carcass" => isset($carcass[$i])? $carcass[$i] : null,
                     "totalcarcass" => isset($totalcarcass[$i])? $totalcarcass[$i] : null,
                ));
            }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!