2

I have following array in one of my php file and i want to return that through another file

set-info.php

<?php
return [

    'email' => [
        '[email protected]',
        '[email protected]',
    ]

this is what i tried

public function getInfo()

{
    //read file contents from file
    $filename =$_SERVER['DOCUMENT_ROOT'].'/set-info.php';
    $filename = str_replace(" ", "", $filename);
    if (file_exists($filename)) {
        $str = file($filename);
        $str = file_get_contents($filename);
            return $str;
    }


}

im getting whole contents with php tags as output. how to get only php array ? pls advice ];

6
  • 1
    The file_get_contents probably want's to be an include or require. Commented Nov 16, 2016 at 9:31
  • return it from a function not from a file ! . function arraymaker (){$myarray=['apple','orange']; return $myarray;} Commented Nov 16, 2016 at 9:33
  • 1
    $arr = include "set-info.php"; Commented Nov 16, 2016 at 9:33
  • 1
    @Accountantم a file can actually return a value. Commented Nov 16, 2016 at 9:34
  • @FranzGleichmann wow!, I wasn't know that, thanks I'm looking after that Commented Nov 16, 2016 at 9:35

2 Answers 2

2

Try to use:

public function getInfo()

{
    //read file contents from file
    $filename =$_SERVER['DOCUMENT_ROOT'].'/set-info.php';
    $filename = str_replace(" ", "", $filename);
    if (file_exists($filename)) {
        $str = (include $filename);
        return $str;
    }


}
Sign up to request clarification or add additional context in comments.

Comments

1

You need to use include for to get that array that you can use in PHP. With file_get_contents you get the content of the file as string. Change your method like this:

public function getInfo()
{
    $info = include 'set-info.php';        
    //Now $info will be the array from set-info.php file.
}

You can check more examples here: http://php.net/manual/en/function.include.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.