So i am having trouble getting an array from one PHP file to another.
所以我在从一个PHP文件到另一个PHP文件中获取数组时遇到问题。
In my first file (file1.php) i got following code:
在我的第一个文件(file1.php)中,我得到以下代码:
print_r($order->delivery);
It will get visible when using echo of course, and it outputs the right things. It gives me an array with order information. Now i got another PHP file I need to use this information in.
当使用echo当然会显示它,并输出正确的东西。它给了我一个包含订单信息的数组。现在我得到另一个PHP文件,我需要使用这些信息。
What i tried so far is including file1.php to file2.php and then echo the array... But the result is empty.
我到目前为止尝试的是将file1.php包含到file2.php然后回显数组......但结果是空的。
require_once('path/file1.php');
echo print_r($order->delivery);
And i tried echo my array directly in file1.php adding a div like this
我尝试直接在file1.php中回显我的数组,添加这样的div
echo "<div id='test'>" . print_r($order->delivery, true) . "</div>";
And then getting the inner HTMl of the div with DOM
然后用DOM获取div的内部HTMl
$dom = new DOMDocument();
$dom->loadHTML("mypageurl");
$xpath = new DOMXPath($dom);
$divContent = $xpath->query('//div[id="test"]');
if($divContent->length > 0) {
$node = $divContent->item(0);
echo "{$node->nodeName} - {$node->nodeValue}";
}
else {
// empty result set
}
Well... none of it works. Any suggestions?
嗯......没有一个有效。有什么建议么?
4 个解决方案
#1
1
You have to set a variable or something, not echoing it.
你必须设置一个变量或其他东西,而不是回应它。
file1.php:
file1.php:
$delivery = $order->delivery;
file2.php
file2.php
include('path/file1.php');
echo "<div id='test'>" . (isset($delivery['firstname']) ? $delivery['firstname'] : '') . "</div>";
Or you use the $object directly if it is set in file1.php
或者如果在file1.php中设置了$ object,则直接使用它
file2.php
file2.php
include('path/file1.php');
echo "<div id='test'>" . (isset($order->delivery['firstname']) ? $order->delivery['firstname'] : '') . "</div>";
#2
1
You could return an array in a file and use it in another like this:
您可以在文件中返回一个数组,并在另一个文件中使用它,如下所示:
<?php
/* File1.php */
return array(
0,1,2,3
);
-
-
<?php
/* File2.php */
var_dump(require_once('File1.php'));
#3
0
Be careful at the variable scope. See this link: http://php.net/manual/en/language.variables.scope.php
在可变范围内要小心。请看这个链接:http://php.net/manual/en/language.variables.scope.php
And try this code please:
并试试这个代码:
require_once('path/file1.php');
global $order;
print_r($order->delivery);
Defining $order
as global
should fix your issue.
将$ order定义为全局应该可以解决您的问题。
#4
-1
You can do this by using $_SESSION or $_COOKIE, See here for more detail; PHP Pass variable to next page
您可以使用$ _SESSION或$ _COOKIE执行此操作,有关详细信息,请参阅此处; PHP Pass变量到下一页
#1
1
You have to set a variable or something, not echoing it.
你必须设置一个变量或其他东西,而不是回应它。
file1.php:
file1.php:
$delivery = $order->delivery;
file2.php
file2.php
include('path/file1.php');
echo "<div id='test'>" . (isset($delivery['firstname']) ? $delivery['firstname'] : '') . "</div>";
Or you use the $object directly if it is set in file1.php
或者如果在file1.php中设置了$ object,则直接使用它
file2.php
file2.php
include('path/file1.php');
echo "<div id='test'>" . (isset($order->delivery['firstname']) ? $order->delivery['firstname'] : '') . "</div>";
#2
1
You could return an array in a file and use it in another like this:
您可以在文件中返回一个数组,并在另一个文件中使用它,如下所示:
<?php
/* File1.php */
return array(
0,1,2,3
);
-
-
<?php
/* File2.php */
var_dump(require_once('File1.php'));
#3
0
Be careful at the variable scope. See this link: http://php.net/manual/en/language.variables.scope.php
在可变范围内要小心。请看这个链接:http://php.net/manual/en/language.variables.scope.php
And try this code please:
并试试这个代码:
require_once('path/file1.php');
global $order;
print_r($order->delivery);
Defining $order
as global
should fix your issue.
将$ order定义为全局应该可以解决您的问题。
#4
-1
You can do this by using $_SESSION or $_COOKIE, See here for more detail; PHP Pass variable to next page
您可以使用$ _SESSION或$ _COOKIE执行此操作,有关详细信息,请参阅此处; PHP Pass变量到下一页