如何存储print_r数据

时间:2017-03-27 13:26:00

标签: php arrays

我想重用print_r结果,所以我尝试再次存储结果。这可能吗?

  

数组列表

<?php

          $books =  array(

              "phil" => array("my girl" => 2.5, "the god delusion" => 3.5,
                              "tweak" => 3, "the shack" => 4,
                              "the birds in my life" => 2.5,
                              "new moon" => 3.5),

              "sameer" => array("the last lecture" => 2.5, "the god delusion" => 3.5,
                                "the noble wilds" => 3, "the shack" => 3.5,
                                "the birds in my life" => 2.5, "new moon" => 1),



          ?>
  

推荐功能

  

显示结果        

require_once("recommend.php");
require_once("sample_list.php");


$re = new Recommend();
$hi = print_r ($re->getRecommendations($books, "tom"));

$out = array($hi);
print_r ($out);

?>

2 个答案:

答案 0 :(得分:2)

只需使用true传递第二个参数,它会将结果存储为字符串(您可以使用echo进行打印)并且不会打印。

$re = new Recommend();
$hi = print_r ($re->getRecommendations($books, "tom"), true);

示例:

$arr = array('a', 'b', 'c', 'd');
$result = print_r($arr, true);

$result输出为:

Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
)

其他可能性是使用var_export()输出它有点不同

$arr = array('a', 'b', 'c', 'd');
$result = var_export($arr, true);

输出:

array (
  0 => 'a',
  1 => 'b',
  2 => 'c',
  3 => 'd',
)

答案 1 :(得分:0)

就这样做吧

$hi = $re->getRecommendations($books, "tom");

print_r ($hi);