多维数组输出。请帮我提取并循环通过这个烂摊子

时间:2012-03-23 19:30:05

标签: php mysql arrays multidimensional-array

所以我有这个非常棒的MySQL查询,它给我一个很棒的数组>

$join_sql = "SELECT $detail_table.`lead_id`, $detail_table.`field_number`, $detail_table.`value` FROM $lead_table JOIN $detail_table ON $lead_table.`id` = $detail_table.`lead_id` WHERE $lead_table.`date_created` BETWEEN '2012-03-18 00:00:00' AND '2012-03-19 23:59:59'";
$join_query = $wpdb->get_results($join_sql, ARRAY_A);

这将返回一个(在我看来)复杂的关联数组,看起来像这样 - http://pastebin.com/kpXrK9Ra

(我不明白数据存储方法,但对于我来说,在每个记录中都有一些数据,而不是每条记录的一行数据,这对我来说似乎过于复杂。但无论如何,这就是我所需要的。处理。)

所以这里是我需要的一小部分样本>

[23] => Array ( [lead_id] => 3 [field_number] => 6.3 [value] => Jane )

因此,在我脑海中代表“记录”的关键是[lead_id]。我需要能够创建var $ first_name。我的结果集可能有50个唯一的[lead_id},但对于名字,[field_number]将始终为“6.3”。

那么我如何遍历pastebin并为每个元素创建变量,以便所有匹配数字的[lead_id]输出类似的内容 -

$first_name, $last_name, $whatev, $foo, $bar, $etc

我甚至不确定这是否有意义......:\

编辑:我要做的是访问'field_number',它对应于该字段的内容,即:名字,名字等,以及相应的'值',并输出所有这些与'lead_id'匹配的特定订单。因此[2]的所有lead_id将输出到CSV的一行,然后输出到下一个lead_id等。

提前致谢!

1 个答案:

答案 0 :(得分:1)

根据$xs中的输入,以下内容:

$ret = array_reduce($xs, function($acc,$x) {
    $acc[$x['lead_id']][$x['field_number']] = $x['value'];
    return $acc;
}, array());

产生输出:

array (
  2 => 
  array (
    1 => '37',
    2 => '051000549',
    3 => '1050000605321',
    4 => '1050000605321',
    15 => '250.00',
    '6.3' => 'John',
    '6.6' => 'Doe',
    7 => 'Microscope',
    '8.1' => '1222 McDowell Ave NE',
    '8.3' => 'Roanoke',
    '8.4' => 'Virginia',
    '8.5' => '24012',
    '8.6' => 'United States',
    9 => '(540)265-3216',
    10 => 'marty@microscope.com',
    11 => 'John Doe',
    16 => '987654321',
    '14.1' => 'Authorized',
  ),
  3 => 
  array (
    1 => '27',
    2 => '987654321',
    3 => '1050000123456',
    4 => '1050000123456',
    15 => '350.00',
    '6.3' => 'Jane',
    '6.6' => 'Doe',
    7 => 'Acme Inc',
    '8.1' => '6724 Laban Rd',
    '8.3' => 'Roanoke',
    '8.4' => 'Virginia',
    '8.5' => '24019',
    '8.6' => 'United States',
    9 => '(540)362-2773',
    10 => 'm@seoserpent.com',
    11 => 'Acme Inc.',
    16 => '987654321',
    '14.1' => 'Authorized',
  ),
)

更新:我使用的输入是:

$xs = array ( 
 0 => array ( 'lead_id' => '2', 'field_number' => '1', 'value' => '37', ),
 1 => array ( 'lead_id' => '2', 'field_number' => '2', 'value' => '051000549', ),
 2 => array ( 'lead_id' => '2', 'field_number' => '3', 'value' => '1050000605321', ),
 3 => array ( 'lead_id' => '2', 'field_number' => '4', 'value' => '1050000605321', ),
 4 => array ( 'lead_id' => '2', 'field_number' => '15', 'value' => '250.00', ),
 5 => array ( 'lead_id' => '2', 'field_number' => '6.3', 'value' => 'John', ),
 6 => array ( 'lead_id' => '2', 'field_number' => '6.6', 'value' => 'Doe', ),
 7 => array ( 'lead_id' => '2', 'field_number' => '7', 'value' => 'Microscope', ),
 8 => array ( 'lead_id' => '2', 'field_number' => '8.1', 'value' => '1222 McDowell Ave NE', ),
 9 => array ( 'lead_id' => '2', 'field_number' => '8.3', 'value' => 'Roanoke', ),
 10 => array ( 'lead_id' => '2', 'field_number' => '8.4', 'value' => 'Virginia', ),
 11 => array ( 'lead_id' => '2', 'field_number' => '8.5', 'value' => '24012', ),
 12 => array ( 'lead_id' => '2', 'field_number' => '8.6', 'value' => 'United States', ),
 13 => array ( 'lead_id' => '2', 'field_number' => '9', 'value' => '(540)265-3216', ),
 14 => array ( 'lead_id' => '2', 'field_number' => '10', 'value' => 'marty@microscope.com', ),
 15 => array ( 'lead_id' => '2', 'field_number' => '11', 'value' => 'John Doe', ),
 16 => array ( 'lead_id' => '2', 'field_number' => '16', 'value' => '987654321', ),
 17 => array ( 'lead_id' => '2', 'field_number' => '14.1', 'value' => 'Authorized', ),
 18 => array ( 'lead_id' => '3', 'field_number' => '1', 'value' => '27', ),
 19 => array ( 'lead_id' => '3', 'field_number' => '2', 'value' => '987654321', ),
 20 => array ( 'lead_id' => '3', 'field_number' => '3', 'value' => '1050000123456', ),
 21 => array ( 'lead_id' => '3', 'field_number' => '4', 'value' => '1050000123456', ),
 22 => array ( 'lead_id' => '3', 'field_number' => '15', 'value' => '350.00', ),
 23 => array ( 'lead_id' => '3', 'field_number' => '6.3', 'value' => 'Jane', ),
 24 => array ( 'lead_id' => '3', 'field_number' => '6.6', 'value' => 'Doe', ),
 25 => array ( 'lead_id' => '3', 'field_number' => '7', 'value' => 'Acme Inc', ),
 26 => array ( 'lead_id' => '3', 'field_number' => '8.1', 'value' => '6724 Laban Rd', ),
 27 => array ( 'lead_id' => '3', 'field_number' => '8.3', 'value' => 'Roanoke', ),
 28 => array ( 'lead_id' => '3', 'field_number' => '8.4', 'value' => 'Virginia', ),
 29 => array ( 'lead_id' => '3', 'field_number' => '8.5', 'value' => '24019', ),
 30 => array ( 'lead_id' => '3', 'field_number' => '8.6', 'value' => 'United States', ),
 31 => array ( 'lead_id' => '3', 'field_number' => '9', 'value' => '(540)362-2773', ),
 32 => array ( 'lead_id' => '3', 'field_number' => '10', 'value' => 'm@seoserpent.com', ),
 33 => array ( 'lead_id' => '3', 'field_number' => '11', 'value' => 'Acme Inc.', ),
 34 => array ( 'lead_id' => '3', 'field_number' => '16', 'value' => '987654321', ),
 35 => array ( 'lead_id' => '3', 'field_number' => '14.1', 'value' => 'Authorized', ),
 );

此外,您可能会发现foreachwhile循环比array_reduce更具可读性:

$ret = array();
foreach ($xs as $x) {
    $ret[$x['lead_id']][$x['field_number']] = $x['value'];
}