Skip to content
Advertisement

How do I get every array/variable from another php file without knowing variables/arrays names

I have a file named “example.php”:

<?php

$var_0803202173634 = [[592,1004],[592,1004],[586,1179],[584,1288],[591,1374],[592,1429],[591,1452],[591,1461],[591,1362],[591,1263],[592,1163],[592,962],[598,962],[604,961],[611,960],[627,958],[632,957],[643,953],[646,951],[649,947],[653,939],[663,924],[669,909],[670,895],[668,886],[665,879],[659,867],[654,862],[647,855],[639,850],[632,848],[622,845],[607,844],[580,846],[560,849],[542,853],[527,857],[513,861],[489,870],[471,879],[447,892],[425,905],[406,918],[388,931],[373,944],[354,963],[344,975],[335,988],[330,1001],[325,1018],[325,1034],[328,1049],[333,1062],[342,1080],[353,1092],[366,1105],[387,1118],[415,1131],[442,1139],[505,1147],[547,1150],[589,1152],[622,1151],[660,1147],[725,1138],[746,1132],[801,1108],[832,1093]];

$var_0803202173637 = [[863,1076],[893,1057],[913,1041],[930,1026],[942,1011],[956,990],[960,979],[963,969],[965,957],[965,949],[963,942],[958,933],[954,925],[949,922],[940,917],[927,910],[915,906],[904,902],[892,898],[874,893],[860,889],[846,886],[831,885],[814,883],[798,881],[781,880],[757,880],[720,881],[697,883],[677,886],[658,887],[639,889],[620,893],[591,897],[582,898],[562,904],[551,908],[540,912],[529,915],[503,921],[485,926],[469,931],[465,934],[466,934],[469,933],[474,930],[479,927],[481,926],[485,925],[489,924],[496,923],[501,924],[506,924],[518,928],[528,931],[537,933],[543,936],[552,940],[567,946],[578,949],[584,953],[586,954],[585,955],[576,960],[553,966],[505,975],[411,985],[279,995],[22,1008],[177,895],[240,891],[347,881],[444,871],[528,862],[595,856],[667,842],[720,831],[752,823],[779,816],[793,812],[808,809],[821,808],[832,808],[843,808],[851,808],[853,808],[858,808],[862,810],[871,813],[883,818],[902,725],[907,728]];

$var_0803202173640 = [[911,730],[913,730],[913,631],[914,632],[914,633],[916,634],[917,636],[919,538],[919,540],[920,541],[920,542],[920,544],[919,546],[919,549],[918,553],[917,556],[916,558],[915,560],[915,562],[915,563],[914,564],[914,567],[912,570],[912,571],[909,575],[906,578],[903,581],[900,484],[898,487],[896,488],[894,491],[885,497],[880,500],[876,503],[871,506],[863,509],[846,516],[834,521],[822,526],[810,531],[793,537],[776,543],[748,555],[737,560],[704,576],[683,589],[663,602],[646,615],[624,635],[618,642],[606,662],[604,668],[602,684],[604,693],[607,698],[612,701],[618,703],[620,703],[623,704],[625,704],[626,704],[628,804],[628,803],[629,803],[630,802],[632,900],[634,897],[635,894],[638,889],[641,882],[644,874],[647,966],[649,953],[652,947],[654,941],[657,937],[660,1033],[661,1033],[662,1031],[665,1030],[668,1029],[669,1129],[670,1129],[670,1130],[670,1131],[670,1132],[670,1133],[672,1236],[676,1242],[681,1248],[685,1252],[688,1256],[694,1260],[704,1266],[723,1273],[742,1277],[767,1281],[792,1286],[816,1290],[843,1291],[865,1292],[879,1293],[918,1299],[936,1300],[952,1300],[968,1300],[985,1300],[1000,1300],[1012,1202],[1023,1202],[1037,1202],[1045,1202],[1051,1201],[1053,1201],[1057,1101],[1058,1101],[1060,1099],[1061,999],[1062,999],[1063,999],[1064,699],[1065,599],[1066,599],[1066,598],[1066,596],[1066,595],[1065,594],[1062,592],[1059,590],[1057,587],[1053,584],[1050,581],[1047,578],[1043,575],[1041,572],[1036,570],[1034,568],[1031,567],[1028,566],[1025,563],[1023,561],[1020,560],[1010,657],[1000,653],[988,649],[976,645],[962,641]];

I want to get every variable/array from “main.php” without knowing every variable/array name. Is it possible?

get_defined_vars gives me every variable from the whole system so it’s not what I need.

Advertisement

Answer

Despite the comments correctly suggesting to think about whether this is a preferrable approach at all (see XY problem), I still think this might be a niche use-case that warrants a solution (after all, we simply don’t know all the legitimate use-cases that may exist).

So here’s what you can do: Use get_defined_vars() after and before requiring the file. Then just include all the variables that didn’t exist before the require (array_diff will not work in this case, as it will throw an “array to string conversion” error).

<?php
$vars_before = get_defined_vars();

require 'example.php';

$vars_after = array_filter( get_defined_vars(), function( $key ) use ( $vars_before ) {
    return ! array_key_exists( $key, $vars_before ) && $key !== 'vars_before';
}, ARRAY_FILTER_USE_KEY );

var_dump( $vars_after );
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement