4

So i am having trouble getting an array from one PHP file to another.

In my first file (file1.php) i got following code:

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.

What i tried so far is including file1.php to file2.php and then echo the array... But the result is empty.

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

echo "<div id='test'>" . print_r($order->delivery, true) . "</div>";

And then getting the inner HTMl of the div with DOM

    $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?

Marcel Wasilewski
  • 2,519
  • 1
  • 17
  • 34
  • Are you sure the file actually gets included? Do you have displaying errors enabled? Try using `require` instead of include just to make sure the file is included (or get an error if it's not). Check out your log files as well. – mrun Nov 24 '15 at 15:41
  • I also used require_once because i got an error when using include, i am sure it gets included, no errors. Will update my post. – Marcel Wasilewski Nov 24 '15 at 15:43
  • You're not assigning anything in `file1.php`. A `print_r` doesn't change any variables (it just has the side-effect of printing). You need to include wherever the variable `$order` is actually defined. – Jon Surrell Nov 24 '15 at 15:49
  • So can you paste a little bit more of `file1.php` or may be the whole if it's not too long? Specifically the code surrounding the initialization of `$order` – mrun Nov 24 '15 at 15:52

4 Answers4

1

You have to set a variable or something, not echoing it.

file1.php:

$delivery = $order->delivery;

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

file2.php

include('path/file1.php');
echo "<div id='test'>" . (isset($order->delivery['firstname']) ? $order->delivery['firstname'] : '') . "</div>";
KiwiJuicer
  • 1,952
  • 14
  • 28
  • Well, sadly this is not working. Still i get an empty result. It's like that: file1.php and file2.php get also included into another page which puts everything out. Is it possible that this will not work when file.2php gets included earlier than file1.php? – Marcel Wasilewski Nov 24 '15 at 15:45
  • file2.php needs to be second. – KiwiJuicer Nov 24 '15 at 15:46
  • The important part of this answer is that `file1.php` is different. It is now an assignment: `$delivery = $order->delivery;`. Now after it is `include`/`require`'d from another file, `$delivery` is defined. – Jon Surrell Nov 24 '15 at 15:51
1

You can do this by using $_SESSION or $_COOKIE, See here for more detail; PHP Pass variable to next page

Community
  • 1
  • 1
Brad Fletcher
  • 3,547
  • 3
  • 27
  • 42
1

Be careful at the variable scope. See this link: 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.

Ciprian Stoica
  • 2,309
  • 5
  • 22
  • 36
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'));
Marcel Burkhard
  • 3,453
  • 1
  • 29
  • 35