-3

我有一个看起来像这样的 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 天,但我无法提出解决方案,所以我的问题是:我怎样才能以有效的方式实现这一目标?

4

2 回答 2

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';
于 2013-03-10T15:57:19.450 回答
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"
  )
)
于 2013-03-10T16:06:10.290 回答