HTT
HTT

Reputation: 23

how to store print_r data

I want to reuse the print_r result so I try to store result again. it's possible ?

array list

<?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),



          ?>

recommend function


show results

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);

?>

Upvotes: 0

Views: 221

Answers (2)

rray
rray

Reputation: 2556

Just pass the second argument with true, it will store the result as string (you could use echo to print) and does not print.

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

Example:

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

$result output is:

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

Other possibility is use var_export() the output it's a little different

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

Output:

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

Upvotes: 2

Tim Hoeksema
Tim Hoeksema

Reputation: 101

Just do it this way

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

print_r ($hi);

Upvotes: 0

Related Questions