将数组转换为另一个数组

时间:2011-03-29 06:07:19

标签: php

你好朋友我有这个数组

    Array
(
    [fieldset_name] => Personal Details
    [field_name] => applicant_name
    [field_label] => Your Name
    [field_type] => text
    [css_classes] => required
    [minlength] => 4
    [maxlength] => 10
    [default_value] => 
    [help_text] => 
)
Array
(
    [fieldset_name] => Personal Details
    [field_name] => applicant_address
    [field_label] => Address
    [field_type] => textarea
    [css_classes] => required
    [minlength] => 4
    [maxlength] => 10
    [default_value] => 
    [help_text] => 
)

我希望结果应该是

Array(
 "Personal Details" => array(
   array(
     'fieldset_name' => "Personal Details"
     field_name => applicant_name
     field_label => Your Name
   ),
   array(
     'fieldset_name' => "Personal Details"
     field_name => applicant_address
     field_label => Address
   ),

 )
)

那怎么做呢

3 个答案:

答案 0 :(得分:0)

我相信这就是你所追求的(未经测试):

$resultArray = array();
foreach( $originalArray as $value )
{
    $key = $value[ 'fieldset_name' ];
    if( !isset( $resultArray[ $key ] ) )
    {
        $resultArray[ $key ] = array();
    }

    $resultArray[ $key ][] = array(
        'fieldset_name' => $key,
        'field_name' => $value[ 'field_name' ],
        'field_label' => $value[ 'field_label' ]
    );
}

var_dump( $resultArray );

答案 1 :(得分:0)

让我们看看我们有两个阵列,你想要的是将这两个阵列合二为一。

   $a= array(
   [fieldset_name] => Personal Details
   [field_name] => applicant_name
   [field_label] => Your Name
   [field_type] => text
   [css_classes] => required
   [minlength] => 4
   [maxlength] => 10
   [default_value] => 
   [help_text] => 
);

   $b=array(
[fieldset_name] => Personal Details
[field_name] => applicant_address
[field_label] => Address
[field_type] => textarea
[css_classes] => required
[minlength] => 4
[maxlength] => 10
[default_value] => 
[help_text] => 
);

$c=array();
$c[]=$a;
$c[]=$b;

$ c是必需的数组

答案 2 :(得分:0)

试试以下内容:

PHP部分:

<?php
$array_name = array('fieldset_name'=>'Personal Details',
          'field_name'=>'applicant_name',
          'field_label'=>'Your Name',
          'field_type'=>'text',
          'css_classes'=>'required',
          'minlength'=>'4',
          'maxlength'=>'10',
          'default_value'=>'',
          'help_text'=>'');

$array_address = array('fieldset_name'=>'Personal Details',
          'field_name'=>'applicant_address',
          'field_label'=>'Address',
          'field_type'=>'textarea',
          'css_classes'=>'required',
          'minlength'=>'4',
          'maxlength'=>'10',
          'default_value'=>'',
          'help_text'=>'');

$array_personal_details = array('Personal Details'=> array($array_name,$array_address));

echo '<pre>';
print_r($array_personal_details);          
echo '</pre>';

?>

我认为这可以帮助您解决问题。