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条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度