0

下面是我的 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
            )

    )

提前致谢 !

4

6 回答 6

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

?>
于 2013-09-11T06:38:38.333 回答
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')
?>
于 2013-09-11T06:29:57.977 回答
0

您可以使用带有闭包(php 5.3+)的 array_map 来转换您的数组。

$array["PaymentOption"]["PaymentOption"] = 
    array_map(function($dat){ 
                  return $dat["paymentoption_id"]; 
              }, $array["PaymentOption"]["PaymentOption"][0]); 
于 2013-09-11T06:17:04.570 回答
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);

于 2013-09-11T06:09:01.420 回答
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);
于 2013-09-11T06:10:37.177 回答
0

这是一个 PHP5.5 示例:

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

也可以看看:array_column()

于 2013-09-11T06:11:09.063 回答