PHP中的数组重组

时间:2013-03-10 15:42:18

标签: php arrays structure

我有一个看起来像这样的php数组:

    Array
    (
       [0] => Array
              (
                [0] => name_first
                [1] => name_second
                [2] => age
              )

       [1] => Array
              (
                [0] => firstname1
                [1] => surname1
                [2] => age1
              )

       [2] => Array
              (
                [0] => firstname2
                [1] => surname2
                [2] => age2
              )
    )

我希望使用前三个值(它们确实发生了变化),如果有一个强调的值(比如本例中的name_first和name_second),请将数组重新构造成:

    Array
    (
      [0] => Array
             (
               [name] => Array
                         (
                           [first] => firstname1
                           [second] => surname1
                         )
               [age] => age1
             )

      [1] => Array
             (
               [name] => Array
                         (
                           [first] => firstname2
                           [second] => surname2
                         )
               [age] => age2
             )
    )

我已经为此工作了2天但是我无法找到解决方案,所以我的问题是:如何以有效的方式实现这一目标?

2 个答案:

答案 0 :(得分:1)

这是你要找的吗?

$result[]=array('name'=>array('firstname'=>$firstName,'lastname'=>$lastName),'age'=>$age);

因此,我们假设我们有一系列名称和年龄,如:

$persons=array(
    [0]=>array('name'=>'Fredrick cheeseton','age'=>'23')
    [1]=>array('name'=>'Bob Scandal','age'=>'102')
    ['random key to show this doesn\'t matter']=array('name'=>'Uncle Jeff','age'=>'1')
);

现在让我们像你指定的那样将$result数组处理成:

foreach($persons as $person){
    //leaving the array key blank suggests an auto acceding integer.
    $name=explode(" ",$person['name']);//spliting the name into two parts using the ' '(space)
    $result[]=array('name'=>array('firstName'=>$name[0],'lastName'=>$name[1]),'age'=>$person['age']);
}
if(isset($result))?var_dump($result):'Result was never declared';

答案 1 :(得分:0)

由于您的评论更新了答案...这是工作代码:

$input = array(
    array(
        "name_first",
        "name_second",
        "age"
    ),
    array(
        "firstname1",
        "surname1",
        "age1"
    ),
    array(
        "firstname2",
        "surname2",
        "age2"
    )
);

function restructure($input) {
  $output = array();
  $a_length = count($input);
  for ($a = 0; $a < $a_length; $a++) { // Loop through child arrays
    $b_length = count($input[$a]);
    for ($b = 0; $b < $b_length; $b++) { // Loop through child array's properties
      if ($a) {
        setValue($output[$a - 1], $input[0][$b], $input[$a][$b]); // See function below
      } else {
        // First child array of $input
        $input[$a][$b] = explode("_", $input[$a][$b]); // Create an array of indexes out of each property.
      }
    }
  }
  return $output;
}

function setValue(&$arr, $indices, $value) { //$arr passed by reference
    $count = count($indices);
    if ($count == 1) { // Check if more than one index (value needs to be nested)
        $arr[$indices[0]] = $value; // Apply value to array
    }
    else {
        $index = array_shift($indices); // Remove first index from array
        if (!is_array($arr[$index])) { // Create an array at this index if it doesn't exist
          $arr[$index] = array();
        }
        setValue($arr[$index], $indices, $value); // Recursively call function passing the current index as reference.
    }
}

print_r(restructure($input));

输出:

Array ( 
  [0] => Array ( 
    [name] => Array ( 
      [first] => firstname1 
      [last] => surname1 
    ) 
    [age] => age1 
  ) 
  [1] => Array ( 
    [name] => Array ( 
      [first] => firstname2 
      [last] => surname2 
    ) 
    [age] => age2 
  ) 
)

它将您的第一个子数组作为索引名称的映射。要使值嵌套,请在其名称中添加下划线,例如

array(
  array(
    "one_two_three",
    "hello"
  ),
  array(
    "first val",
    "second val"
  )
)

将创建以下结构:

Array(
  [0] => Array(
    [one] => Array(
      [two] => Array(
        [three] => "first val"
      )
    )
    [hello] => "second val"
  )
)