当文件包含PHP和HTML代码时,如何读取php文件并搜索特定数组?

I have a file data.php, it has this code

<?php
$car[0]="BMW";$car[1]="Audi";$acr[2]="Honda";
$mob[0]="apple";$mob[1]="Nokia";$mob[2]="Sony";
?>
<html>
<body>
Some html code...
</body>
</html>

I want to create a php file that reads data.php and gives me the array named mob to use in this new file and I don't need html or other php code so include 'data.php' may not not work.

Basically, I need a php code that fetches a specific array in a different php file to reuse.

if you only want to use them in 1 file you can do:

include 'data.php'; 

at the top of the page where you gonna need it. If you need them on multiple pages you can load them into the session.

But it has to be noted that this question is asked a lot of times. so please google before you ask : https://stackoverflow.com/a/35884027/3493752

You can do it easily by including you data.php file in your other file.

 include 'data.php';
 echo $car[0];
 echo $car[1];
 echo $mob[0];
 echo $mob[1];

This way you can achive your desired output.Hope it helps.

I don't see the purpose of doing this but you can do a separate file which returns an array of items like:

<?php

return [
    'car' => [
          'BMW',
          'Audi',
          'Honda',
     ],
    'mob' => [
          'Apple',
          'Nokia',
          'Sony',
     ]
];

On top of your file, where you want to use these items you just require the items file:

<?php
    $items = (array) include '../path/to/items.php';
?>
<html>
<body>
Some html code... <?php echo $items['car'][0] ?>
</body>
</html>

Like this you can include the items wherever you want, without including the html part too..