来自file_get_contents的PHP数组值

时间:2014-05-13 07:08:37

标签: php arrays

我宣布了一个像这样的变量

echo $OPTIONS="500=>250.00, 1000=>500.00,2500=>1100.00,5000=>2250.00";

我通过file_get_contents()函数获得了这些变量。

$contents = file_get_contents(SERVICE_URL."options_config.php?options=".$OPTIONS);
$package=array($contents)
foreach($package as $pack=>$price)
{
echo $pack;
}

但我得到0值。有什么问题?

print_r($package);

结果是:

Array ( [0] => 500=>250.00, 1000=>500.00,2500=>1100.00,5000=>2250.00 ) 

我想要这样的结果

500 as 250.00
1000 as 500.00

1 个答案:

答案 0 :(得分:1)

我认为您要找的是serializeunserialize

示例: test.php

<?php

// Handle Get Request
// This portion of your code can be on another file
//
if (isset($_GET['getOptions']))
{
    $myOptions = array(
        500  => 250.00,
        1000 => 500.00,
        2500 => 1100.00,
        5000 => 2250.00
    );
    exit(serialize($myOptions));
}

// Sample Usage
$options = file_get_contents('http://localhost/test.php?getOptions');
$options = unserialize($options);

// Debug
var_dump($options);

?>

输出:

enter image description here