2012-10-10 28 views
1

我需要一些帮助来启动一个将通过数组解析的函数,检查某些值,如果这些值存在,则使用这些值创建一个新数组,然后array_push它们全部一起。解析一个巨大的mulltidimensional数组并从中构建一个新数组

我穿过阵列如此:

Array 
(
    [0] => Array 
     (
      [id] => 86 
      [34] => 695 
      [39] => 0 
      [40.1] => Yes 
      [36.1] => Yes 
      [35.4] => Card 
      [33.3] => Dekalb 
      [33.4] => Illinois 
      [33.5] => 60115 
      [33.6] => United States 
      [35.1] => 1143 
      [33.1] => 5555 Write Rd 
      [33.2] => Write School 
      [32.6] => John 
      [32.3] => Smith 
      [28] => [email protected] 
      [27] => 5555556554 
      [25] => NIUSN 
      [14.3] => Jane 
      [14.6] => Doe 
      [11.2] => 695 
      [12] => 1 
      [11.1] => In-Person 
      [3] => 0 
      [2.2] => 595 
      [2.1] => Online 
     ) 
    [1] => Array 
     (
     ...same stuff as before 
     ) 

我犯了一个函数parseArray到我通过上述阵列。我通过每个键/值组合,如果某个键存在,我将其设置为一个变量。然后,如果存在的所有合适的变数,我联想是一个数组,然后把它推到一个最终的阵列(其中所有的值将举行):

function parseArray($arry) { 

    $results = array(); 
    $current_result = array(); 

    foreach($arry as $a) { 
     foreach($a as $k => $v) {  
      if ($k == '14.3') { 
       $attendee_first_name = $v; 
      } 
      if ($k == '14.6') { 
       $attendee_last_name = $v; 
      } 
      if ($attendee_first_name && $attendee_last_name) { 
       $full_name = $attendee_first_name . ' ' . $attendee_last_name; 
      } 
     } 
     $current_result['attendee_name'] = $full_name; 
    } 

    array_push($results, $current_result); 

    return $results; 
} 

现在他们这样,我已经做了,已经非常程序而且非常笨重。我很想了解如何为遍历数组和分配值生成更清晰/漂亮的代码。

理想我很想得到这样的事情:

Array 
(
    [0] => Array 
     (
      [attendees_name] = John Smith 
      [attendees_email] = [email protected] 
      [purchasing_name] = Jane Doe 
      ...etc 

所以我可以通过一个foreach和输出简单的传递输出所需的信息很容易。

回答

1

代码:

# initialize your test array 

$arry = array(
    0 => array(
      'id' => 86, 
      '34' => 695, 
      '39' => 0, 
      '40.1' => 'Yes', 
      '36.1' => 'Yes', 
      '35.4' => 'Card', 
      '33.3' => 'Dekalb', 
      '33.4' => 'Illinois', 
      '33.5' => '60115', 
      '33.6' => 'United States', 
      '35.1' => '1143', 
      '33.1' => '5555 Write Rd', 
      '33.2' => 'Write School', 
      '32.6' => 'John', 
      '32.3' => 'Smith', 
      '28' => '[email protected]', 
      '27' => '5555556554', 
      '25' => 'NIUSN', 
      '14.3' => 'Jane', 
      '14.6' => 'Doe', 
      '11.2' => '695', 
      '12' => '1', 
      '11.1' => 'In-Person', 
      '3' => 0, 
      '2.2' => 595, 
      '2.1' => 'Online' 
     ), 
1 => array(
     'id' => 86, 
     '34' => 695, 
     '39' => 0, 
     '40.1' => 'Yes', 
     '36.1' => 'Yes', 
     '35.4' => 'Card', 
     '33.3' => 'Dekalb', 
     '33.4' => 'Illinois', 
     '33.5' => '60115', 
     '33.6' => 'United States', 
     '35.1' => '1143', 
     '33.1' => '5555 Write Rd', 
     '33.2' => 'Write School', 
     '32.6' => 'Douglas', 
     '32.3' => 'Adams', 
     '28' => '[email protected]', 
     '27' => '5555556554', 
     '25' => 'NIUSN', 
     '14.3' => 'Frank', 
     '14.6' => 'Wright', 
     '11.2' => '695', 
     '12' => '1', 
     '11.1' => 'In-Person', 
     '3' => 0, 
     '2.2' => 595, 
     '2.1' => 'Online' 
    ) 

); 

# How to do your function elegantly 

foreach($arry as $a) { 
    $results[] = array(
     'attendees_name' => $a['32.6'] . " " . $a['32.3'], 
     'attendees_email' => $a['28'], 
     'purchasing_name' => $a['14.3'] . " " . $a['14.6'] 
    ); 
} 

# print results 

var_dump($results); 

结果:

array(2) { 
    [0]=> 
    array(3) { 
    ["attendees_name"]=> 
    string(10) "John Smith" 
    ["attendees_email"]=> 
    string(16) "[email protected]" 
    ["purchasing_name"]=> 
    string(8) "Jane Doe" 
    } 
    [1]=> 
    array(3) { 
    ["attendees_name"]=> 
    string(13) "Douglas Adams" 
    ["attendees_email"]=> 
    string(23) "[email protected]" 
    ["purchasing_name"]=> 
    string(12) "Frank Wright" 
    } 
} 
+0

我觉得像这样的笨蛋的时候。非常感谢 ;) – tr3online

相关问题