Variable name of array element

It is for static page, dynamic page, website, SEO, performance and other web development discussion
Posts: 435
Joined: Sat Aug 23, 2014 5:34 pm

Variable name of array element

Postby ramverma » Sat Sep 12, 2015 10:46 am

Hello All,

Just started to learn and work with PHP, got stuck and couldn't figure out with documentation.

$nodes = array('server01', 'server02' , 'server03' , 'server04');
$comps = array('CPU', 'Memory', 'Uptime', 'Disk', 'IO-s', 'pgflts');
$file = 'test_file.txt';
foreach ($nodes as $node_each){
foreach ($comps as $comp_each){
$searchfor = $node_each .'|'. $comp_each;
$contents = file_get_contents($file);
$pattern = preg_quote($searchfor, '/');
$pattern = "/^.*$pattern.*\$/m";
if(preg_match_all($pattern, $contents, $matches)){
$line = reset($matches[0]);
$line_arr = (explode('|', $line));
${$nodes.$comps} = $line_arr[2];
echo ${$nodes.$comps};
echo "<br />";
echo "No matches found";
// I would like to call all the variables here outside of foreach loop scope as below..
// echo $server01.cpu;
// echo $server02.mem;
please hint or guide me here? If my approach itself is not correct here, please point the same. Thanks!

Return to “PHP, HTML, MYSQL and Web Hosting”

Who is online

Users browsing this forum: No registered users and 1 guest