使用数组PHP进行数组转换

时间:2014-04-12 06:58:23

标签: php

我需要转换array以下

Array
(
[Property] => Array
        (
            [S] => Built As Condominium
        )
)

Array
(
[property] => Built As Condominium
)

有什么办法。

5 个答案:

答案 0 :(得分:3)

您可以在implode

下使用foreach
<?php
$arr=Array ( 'Property' => Array ( 'S' => 'Built As Condominium' ) );
foreach($arr as $k=>$arr1)
{
    $arr[$k]=implode('',$arr1);
}
print_r($arr);

Demo

答案 1 :(得分:1)

您可以使用数组的键将值压缩到一行中,例如

$array['Property'] = $array['Property']['S']; 

结果

Array ( [property] => Built As Condominium )

答案 2 :(得分:0)

$data = array(
  "Property" => array( 
    "S" => "Built As Condominium"
   )
);

foreach($data as $key => $value) {
  if($key == "Property") {
    $normalized_data['Property'] = is_array($value) && isset($value['S']) ? $value['S'] : NULL;
  }
}

节目输出

array(1) {
  ["property"]=>
  string(20) "Built As Condominium"
}

Link

答案 3 :(得分:0)

不需要内爆或键,只需使用引用,即'&amp;'。这很简单。

$array = Array ( 'Property' => Array ( 'S' => 'Built As Condominium' ) );

foreach($array as &$value){
    $value=$value['S'];
}

答案 4 :(得分:0)

或....如果您不知道内部数组的键但只关心它的值(并假设您希望内部数组的第一个成员作为新值),那么像{{ foreach循环中的3}}可以工作:

$arr = array ('Property' => array( 'S' => 'Built As Condominium'));
$new = array();
foreach($arr as $key => $inner) {
    $new[$key] = reset($inner);
}
print_r($new);

<强>输出:

Array
(
    [Property] => Built As Condominium
)