2017-06-18 77 views
1

我正在使用此代码从数组中输出多个特定值并将这些值发送到电子邮件。发送特定数组值到电子邮件php

如果我这样写:

print_r($products[1]['Notes'], true) 

然后它会显示1个值off当然,我已经把 “[1]” 只针对1行。

,如果我这样写:

print_r($products, true) 

然后,它输出的所有值和所有行。

是否有我可以输出多个值的“注释”?

回答

1

如果你的PHP 5.5,更 - 使用array_column功能:

print_r(array_column($products, 'Notes'), true); 

否则,您需要选择与所需的列foreach and print'em:

$columns = []; 
foreach ($products as $prod) { 
    $columns[] = $prod['Notes']; 
} 
print_r($columns, true); 
+0

它显示的值但具有这样的: “阵列([0] =>产物1 [1] =>产物2)”。有没有什么方法可以显示“产品1,产品2”? – orbnexus

+0

我通过内爆实现了这一目标 – orbnexus

1

有一个array_column()函数可以帮助你在这里。

<?php 
$data = [ 
    ['Notes' => 'test1'], 
    ['Notes' => 'test2'], 
    ['Notes' => 'test3'], 
    ['Notes' => 'test4'], 
    ['Notes' => 'test5'], 
    ]; 

$notes = array_column($data, 'Notes'); 
print_r($notes); 

输出:

Array 
(
    [0] => test1 
    [1] => test2 
    [2] => test3 
    [3] => test4 
    [4] => test5 
) 

https://3v4l.org/Mp24S

1
$notes = array(); 
$i = 0; 
while($i<count($products)){ 
    $notes[] = $products[$i]['Notes']; 
    $i++; 
} 
print_r($notes);