Jump to content

collecting information from an array


jay3ld

Recommended Posts

I have this file I can't parse because there would be lots of errors and undefined errors.

So I am using regex to pull out the array information

 

I have a basic array like this lets say:

$my_array = array(
    'key' => "value",
    'another_key' => 'another_value'
);

 

So far I have accomplished finding the first part of the array with:

preg_match_all("~\\\$([a-zA-Z0-9]+) = array\(([\s]+)('([a-zA-Z0-9]+)' => \"(.+?)\"(,))+[\s]+~", $contents, $matches);

 

The problem is I can't figure out how to get the second item from the array, or a possible third, or possible fourth, etc.

 

I thought adding () around it and giving it a + would do this. But it doesn't seem to do this.

Anyone know what I did wrong here?

Link to comment
https://forums.phpfreaks.com/topic/129910-collecting-information-from-an-array/
Share on other sites

How about something like this?

 

<pre>
<?php
$data = <<<DATA
\$my_array = array(
    'key' => "value",
    'another_key' => 'another_value'
);
DATA;

preg_match_all('/^\s*(\$\S+\s*=\s*array\(.*?\);)/ms', $data, $matches);
array_shift($matches);
print_r($matches);
foreach ($matches[0] as &$match) {
	preg_match('/^\s*\$(\S+)/', $match, $name);
	eval('$array = ' . $match);
	$result[$name[1]] = $array;
}

print_r($result);
?>
</pre>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.