PHP多维数组转换

时间:2013-09-11 05:54:18

标签: php multidimensional-array type-conversion

下面是我的PHP数组:

[Paymentoption] => Array
    (
        [Paymentoption] => Array
            (
                [0] => Array
                    (
                        [0] => Array
                            (
                                [paymentoption_id] => 1
                            )

                        [1] => Array
                            (
                                [paymentoption_id] => 2
                            )

                        [2] => Array
                            (
                                [paymentoption_id] => 3
                            )

                        [3] => Array
                            (
                                [paymentoption_id] => 4
                            )

                    )

            )

    )

如何将其转换为:

[Paymentoption] => Array
    (
        [Paymentoption] => Array
            (
                [0] => 1
                [1] => 2
                [2] => 3
                [3] => 4
            )

    )

提前致谢!

6 个答案:

答案 0 :(得分:0)

请检查以下代码,

foreach ($result as $key => $res) {
    foreach ($res as $key1 => $re) {
        foreach ($re[0] as $key2 => $r) {
            $data[$key][$key1][$key2] = $r['paymentoption_id'];
        }
    }
}

请使用echo "<pre>";print_r($data);

检查输出

答案 1 :(得分:0)

试试这个

function makeNonNestedRecursive(array &$out, $key, array $in){
    foreach($in as $k=>$v){
        if(is_array($v)){
            makeNonNestedRecursive($out, $key . $k . '_', $v);
        }else{
            $out[] = $v;
        }
    }
}

function makeNonNested(array $in){
    $out = array();
    makeNonNestedRecursive($out, '', $in);
    return $out;
}

// Example
    $fooCompressed = makeNonNested($foo);

   print_r($fooCompressed);

答案 2 :(得分:0)

这是一个PHP5.5示例:

$data['Paymentoption']['Paymentoption'] = array_column(
    $data['Paymentoption']['Paymentoption'][0], 
    'paymentoption_id'
);

另请参阅:array_column()

答案 3 :(得分:0)

你可以使用array_map和一个闭包(php 5.3+)来转换你的数组。

$array["PaymentOption"]["PaymentOption"] = 
    array_map(function($dat){ 
                  return $dat["paymentoption_id"]; 
              }, $array["PaymentOption"]["PaymentOption"][0]); 

答案 4 :(得分:0)

试试这个

<?php
$a = array(1 => 'one', 2 => 'two', 3 => 'three');


$b = array_values($a);//taking values from array
// Now $b is array(1 => 'one', 2 => 'two', 3 => 'three')
?>

答案 5 :(得分:0)

使用:

<?php
$main_array["Paymentoption"] = array(
    "Paymentoption" => array(
        "0" => array(
                    "0" => array("paymentoption_id" => 1 ),
                    "1" => array("paymentoption_id" => 2 ),
                    "2" => array("paymentoption_id" => 3 ),
                    "3" => array("paymentoption_id" => 4 )
                )
        )
);

print_r($main_array); // previous array


foreach($main_array as $key1=>$second_array) {
    foreach($second_array as $key2=>$third_array) {
        foreach($third_array[0] as $key3=>$fourth_array) {
            foreach($fourth_array as $key4=>$fifth_array) { 
            }
            $third_array[$key3] = $fifth_array;
        }
        $second_array[$key2] = $third_array;
    }
    $main_array[$key1] = $second_array;
}

var_dump($main_array); // new array

?>