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

时间:2014-08-20 06:57:44

标签: php arrays

我希望通过按键匹配它们来组合6个索引数组,然后将值插入到我的表中我该怎么做?

[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] => 
    )

我只能组合两个数组 这是我在另一个问题中得到的代码

$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]);
    }
}

请帮助我,谢谢。

3 个答案:

答案 0 :(得分:1)

根据您的问题,您想要的输出是?:

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],
]

如果是,那么这可能就是答案:

        $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,
            ));
        }

答案 1 :(得分:0)

试试这个

$arr_temp = array();
foreach($your_array as $key=>$arr)
{ 
    foreach($arr as $k=>$v)
    { 
       if($v!="")
       {
            $arr_temp[$k][] = $v;     
       }
    }    
}

print_r($arr_temp);

答案 2 :(得分:0)

是否需要。

$typeSex = array("typeSex" => array("broiler", "layer"));
$noheads = array("noheads" => array(123,321));
$origin  = array("origin" => array("asd","sda","",""));
$live    = array("live" => array(123,222));
$carcass =array("carcass" =>  array(312,341,'',''));
$totalcarcass = array("totalcarcass" => array(231,321));

$final = array_merge($typeSex, $noheads, $origin, $live, $carcass, $totalcarcass);


foreach ($final as $key=>$val)
{

        $res[] = $val[0];
        $ab[] = $val[1]; 
}

$aa = array($res,$ab);

echo "<pre>";
print_r($aa);