如何从While循环PHP

时间:2016-06-26 18:13:16

标签: php arrays variables while-loop

我正在从While循环填充数组,但无法生成正确的数组格式。我已经找到了许多不同的变化,但不是我正在寻找的,包括在下面。目的是针对第三方SMTP,如变量$to

中所示

所需数组:

array(3) { 
    ["email1@email1.com"]=> array(1) { ["userid"]=> string(1) "6" } 
    ["email2@email2.com"]=> array(1) { ["userid"]=> string(2) "64" } 
    ["email3@email3.com"]=> array(1) { ["userid"]=> string(3) "503" } 
}

尝试1:

$str = array();
while($userEmails = $query->fetch(PDO::FETCH_ASSOC)){

    $str[] = '"'.$userEmails['user_email'].'"  => array( 
       "userid" => "'.$userEmails['user_id'].'" 
    )';
}
$to = $str;

尝试1个不正确的数组:

array(3) {
     [0]=> string(54) ""email1@email1.com" => array( "userid" => "6" )"       
     [1]=> string(54) ""email2@email2.com" => array( "userid" => "64" )" 
     [2]=> string(54) ""email3@email3.com" => array( "userid" => "503" )" 
}

尝试2:

$str = "";
while($userEmails = $query->fetch(PDO::FETCH_ASSOC)){

    $str .= '"'.$userEmails['user_email'].'"  => array( "userid" => "'.$userEmails['user_id'].'" )';
}

$emails = rtrim($str, ", ");

$to = array($emails);

尝试2个不正确的数组:

array(1) { 
    [0]=> string(162) ""email1@email1.com" => array( "userid" => "6" )"email2@email2.com" => array( "userid" => "64" )"email3@email3.com" => array( "userid" => "503" )" }

3 个答案:

答案 0 :(得分:1)

你想要这个,我想:

$str = array();
while($userEmails = $query->fetch(PDO::FETCH_ASSOC)){
    $str[$userEmails['user_email']] = array(
        'user_id' => $userEmails['user_id']
    );
}

答案 1 :(得分:1)

我个人认为使用while fetch强迫您接受丑陋的代码。我通常有一些用于编写更多声明性代码的实用函数

function query_reduce(callable $f, $initialValue, $query) {
  $result = $initialValue;
  while($x = $query->fetch(PDO::FETCH_ASSOC))
    $z = call_user_func($f, $x);
  return $result;
}

现在你可以写这个

$result = query_reduce(function ($acc, $x) {
  $acc[$x['user_email']] = [['user_id'] => $x['user_id']];
  return $acc;
}, [], $query);

var_dump($result);

query_reduce只是我使用的助手之一。这是一个像foreach

一样的作品
// use like foreach
function query_foreach(callable $f, $query) {
  query_reduce(function($i, $x) {
    call_user_func($f, $i++);
  }, 0, $query);
}

// $query returns [{id:'a'}, {id:'b'}, {id:'c'}]
query_foreach(function ($x, $i) {
  echo "{$x['id']} is in position {$i}";
});

// output
// a is in position 0
// b is in position 1
// c is in position 2

或者用于映射查询的

// use like array_map
function query_map(callable $f, $query) {
  $i = 0;
  return query_reduce(function ($acc, $x) use ($i) {
    return array_merge($acc, [call_user_func($f, $x, $i++)]);
  }, [], $query);
}

// $query returns [{id:'a'}, {id:'b'}, {id:'c'}]
$ids = query_foreach(function ($x, $i) {
  return ['id'=> $x['id'], 'position'=> $i];
});

print_r($ids);

// output...
// Array
// (
//     Array
//     (
//         [id] => a
//         [position] => 0
//     )
//     Array
//     (
//         [id] => b
//         [position] => 1
//     )
//     Array
//     (
//         [id] => c
//         [position] => 2
//     )
// )

答案 2 :(得分:0)

尝试这种方式:

$str = array();
while($userEmails = $query->fetch(PDO::FETCH_ASSOC)){
    $str[$userEmails['user_email']] = array('userid' => $userEmails['user_id']);
}

$to = $str;