Recursive array search - keep proper order and iterate down array - php

When accessing the script I am writing, you pass the category path to it when accessing the page. The script then compares the data to an array of actual categories, or a branch, that should be associated with that category.
I am setting the parents and all of its children into a tree and then going down the branch and comparing the data to ensure the customer is using a correct url. Here's a quick example of how the code works:
// Customer is accessing from site.com/store/some-cat/some-othercat
// We pass those variables with the htaccess to script.php?var=$1,$2
// We then explode that to make an array on $var[0] and $var[1]
$categoryMap = explode(",", $_GET['var']);
$categoryID = array();
$categoryInfoMap = array();
foreach ($categoryMap as $a) {
$categoryIDs[] = trim($a);
}
$getCategoryInfo = $db->fn->query("SELECT * FROM store_category");
....
// Inside while loop...
$categoryInfoMap[] = $db->result[]; // stored whole results as array
// End of the while loop
$masterKey = $mainClass->findKeyInDbArray($categoryInfoMap, 'c.path', $categoryMap[0]);
if ((isset($masterKey) && $masterKey === "0") || !empty($masterKey)) {
$thisId = $categoryInfoMap[$masterKey]['c.id'];
$thisPath = $categoryInfoMap[$masterKey]['c.path'];
$thisName = $categoryInfoMap[$masterKey]['c.name'];
$tree = $mainClass->buildTree($categoryInfoMap);
$children = $tree['children'][$thisId];
$childrenItems = "";
foreach ($categoryIDs as $cid) {
// One of the categories entered doesnt exist at all so we redirect,
// else we will go through them and make sure theyre apart of the branch
if (!$mainClass->recursive_array_search($cid, $tree)) {
... redirect them somewhere and die()
} else {
if (!$mainClass->recursive_array_search($cid, $children)) {
... redirect them somewhere and die()
} else {
!!!!!!!!!!!!============!!!!!!!!!!!!!!
THIS IS THE IMPORTANT PART HERE
!!!!!!!!!!!!============!!!!!!!!!!!!!!
}
}
}
}
... Rest of the script which works for now
Here is the functions used in the code above
public function findKeyInDbArray($products, $field, $value) {
foreach($products as $key => $product) {
if ($product[$field] === $value) {
return "$key";
}
}
return NULL;
}
public function buildTree($arr) {
$tree = array(
'children' => array()
);
$index = array(0=>&$tree);
foreach ($arr as $key => $val) {
$parent = &$index[$val['c.parentcatid']];
$node = $val;
$parent['children'][$val['c.id']] = $node;
$index[$val['c.id']] = &$parent['children'][$val['c.id']];
}
return $tree;
}
public function recursive_array_search($needle,$haystack) {
foreach($haystack as $key=>$value) {
$current_key=$key;
if($needle===$value OR (is_array($value) && $this->recursive_array_search($needle,$value) !== false)) {
return $current_key;
}
}
return false;
}
And here's an example of the tree array, from the parent node down. Shorted for visibility reasons
Array(
[c.id] => 1
[c.name] => Radios
[c.path] => radios
[c.parentcatid] => 0
[children] => (
[2] => (
[0] => 2
....
[children] => (
[3] => (
[c.id] => 3
....
[c.parentcatid] => 2
),
[4] => (
[c.id] => 4
....
[c.parentcatid] => 2
)
)
)
......
[10] => (
[0] => 10
....
[c.parentcatid] => 1
)
)
SO onto the good bits
Right now the code is working to prove that the branches have matching variables from their tree. If the item path, which is the variable we are using to compare to the url $var matches, then it will continue and work. so if in the branch the following values exist:
array(c.path => 'foo'),
array(c.path => 'bar')
And I visit the script as site.com/store/foo/bar then everything works great. If i visit the site as site.com/store/foo/notBar then it will fail, as the notBar variable is not a member of this branch. That's perfect right? Everything should work! Except it doesn't and for a good reason.
The issue here
If the item matches in the branch then it has passed the check and it's the end of the check. Not if the item is passed in the wrong order, such as site.com/store/bar/foo, then it still technically has good variables in it, but it should NOT pass since the structure is not in the order its coming down the parent array. Likewise, if another branch farther up the tree, lets say barwithNoChildren exists, i can swap foo or bar out with it and still pass, even though nothing should be there.
Hopefully you guy understand what I am asking, and can help suggest ways around this. I've been wracking my brain on this system for the last couple of days and since they want fancy urls for seo and other reasons, it's been a lot more difficult than I planned. Thanks for any suggestions!

A tree structure is not really helpful for this purpose. You should be thinking about how to create a data structure that makes it easy for you to match the input. Since your category input describes a branch of the tree, the best thing to do is build an array that you can use to match those branch descriptions to your categories efficiently.
Let's build an array where the keys are the paths for each category as described by their slugs, and the values are the category IDs. We can then immediately identify the matching category, or fail if the path is not in the array.
This breadcrumb-like structure is another pattern that is commonly used with categories. Along with the tree and flat id map, you can do pretty much anything you need. The key takeaway is to think about creating different structures with your data to accomplish different tasks. It's usually more efficient and less error prone to create a new structure that's easy to work with than it is to create complex logic to try and work with an existing structure that doesn't lend itself to the task at hand.
<?php
//Mock category records, would come from the DB in the real world
$categoryRecords = [
['id' => 1, 'title' => 'Radios', 'slug' => 'radios', 'parent_id' => 0],
['id' => 2, 'title' => 'Accessories', 'slug' => 'misc', 'parent_id' => 1],
['id' => 3, 'title' => 'Motorola', 'slug' => 'motorola', 'parent_id' => 1],
['id' => 4, 'title' => 'Handheld', 'slug' => 'handheld', 'parent_id' => 3],
['id' => 5, 'title' => 'Mobile', 'slug' => 'mobile', 'parent_id' => 3]
];
//Create an array that maps parent IDs to primary keys
$idMap = [];
foreach ($categoryRecords as $currRecord)
{
$idMap[$currRecord['id']] = $currRecord;
}
//Traverse the flat array and build the path lines
$paths = [];
$categoryIds = array_keys($idMap);
foreach ($categoryIds as $currLeafId)
{
$currCategoryId = $currLeafId;
$currLine = [];
do
{
$currLine[] = $idMap[$currCategoryId]['slug'];
$currCategoryId = $idMap[$currCategoryId]['parent_id'];
} while ($currCategoryId != 0);
$currLine = array_reverse($currLine);
$currPath = implode('/', $currLine);
$paths[$currPath] = $currLeafId;
}
//Join your input - $_GET['var'] in your example
$inputPath = implode('/', ['radios', 'motorola', 'handheld']);
//Now you can see if the incoming path matched a category
if(array_key_exists($inputPath, $paths))
{
$category = $categoryRecords[$paths[$inputPath]];
echo 'Matched category: '.$category['title'].PHP_EOL;
}
else
{
echo 'Invalid category path';
}

Related

Remove deeply nested element from multi-dimensional array?

I need to remove an element form a deeply nested array of unknown structure (i.e. I do not know what the key sequence would be to address the element in order to unset it). The element I am removing however does have a consistent structure (stdObject), so I can search the entire multidimensional array to find it, but then it must be removed. Thoughts on how to accomplish this?
EDIT: This is the function I have right now trying to achieve this.
function _subqueue_filter_reference(&$where)
{
foreach ($where as $key => $value) {
if (is_array($value))
{
foreach ($value as $filter_key => $filter)
{
if (isset($filter['field']) && is_string($filter['field']) && $filter['field'] == 'nodequeue_nodes_node__nodequeue_subqueue.reference')
{
unset($value[$filter_key]);
return TRUE;
}
}
return _subqueue_filter_reference($value);
}
}
return FALSE;
}
EDIT #2: Snipped of array structure from var_dump.
array (size=1)
1 =>
array (size=3)
'conditions' =>
array (size=5)
0 =>
array (size=3)
...
1 =>
array (size=3)
...
2 =>
array (size=3)
...
3 =>
array (size=3)
...
4 =>
array (size=3)
...
'args' =>
array (size=0)
empty
'type' => string 'AND' (length=3)
...so assuming that this entire structure is assigned to $array, the element I need to remove is $array[1]['conditions'][4] where that target is an array with three fields:
field
value
operator
...all of which are string values.
This is just a cursor problem.
function recursive_unset(&$array)
{
foreach ($array as $key => &$value) # See the added & here.
{
if(is_array($value))
{
if(isset($value['field']) && $value['field'] == 'nodequeue_nodes_node__nodequeue_subqueue.reference')
{
unset($array[$key]);
}
recursive_unset($value);
}
}
}
Notes : you don't need to use is_string here, you can just make the comparison as you're comparing to a string and the value exists.
Don't use return unless you're sure there is only one occurrence of your value.
Edit :
Here is a complete example with an array similar to what you showed :
$test = array (
1 => array (
'conditions' =>
array (
0 => array ('field' => 'dont_care1', 'value' => 'test', 'operator' => 'whatever'),
1 => array ('field' => 'dont_care2', 'value' => 'test', 'operator' => 'whatever'),
2 => array ('field' => 'nodequeue_nodes_node__nodequeue_subqueue.reference', 'value' => 'test', 'operator' => 'whatever'),
3 => array ('field' => 'dont_care3', 'value' => 'test', 'operator' => 'whatever')
),
'args' => array (),
'type' => 'AND'
));
var_dump($test);
function recursive_unset(&$array)
{
foreach ($array as $key => &$value)
{
if(is_array($value))
{
if(isset($value['field']) && $value['field'] == 'nodequeue_nodes_node__nodequeue_subqueue.reference')
{
unset($array[$key]);
}
recursive_unset($value);
}
}
}
recursive_unset($test);
var_dump($test);
One way to solve this was to extend your recursive function with a second parameter:
function _subqueue_filter_reference(&$where, $keyPath = array())
You'd still do the initial call the same way, but the internal call to itself would be this:
return _subqueue_filter_reference($value, array_merge($keyPath, array($key)));
This would provide you with the full path of keys to reach the current part of the array in the $keyPath variable. You can then use this in your unset. If you're feeling really dirty, you might even use eval for this as a valid shortcut, since the source of the input you'd give it would be fully within your control.
Edit: On another note, it may not be a good idea to delete items from the array while you're looping over it. I'm not sure how a foreach compiles but if you get weird errors you may want to separate your finding logic from the deleting logic.
I have arrived at a solution that is a spin-off of the function found at http://www.php.net/manual/en/function.array-search.php#79535 (array_search documentation).
Code:
function _subqueue_filter_reference($haystack,&$tree=array(),$index="")
{
// dpm($haystack);
if (is_array($haystack))
{
$result = array();
if (count($tree)==0)
{
$tree = array() + $haystack;
}
foreach($haystack as $k=>$current)
{
if (is_array($current))
{
if (isset($current['field']) && is_string($current['field']) && $current['field'] == 'nodequeue_nodes_node__nodequeue_subqueue.reference')
{
eval("unset(\$tree{$index}[{$k}]);"); // unset all elements = empty array
}
_subqueue_filter_reference($current,$tree,$index."[$k]");
}
}
}
return $tree;
}
I hate having to use eval as it SCREAMS of a giant, gaping security hole, but it's pretty secure and the values being called in eval are generated explicitly by Drupal core and Views. I'm okay with using it for now.
Anyway, when I return the tree I simply replace the old array with the newly returned tree array. Works like a charm.

PHP Array needs fixed (consolidated/merged)

I have this multidimensional array which I'll name "original":
$original=
array
0 =>
array
'animal' => 'cats'
'quantity' => 1
1 =>
array
'animal' => 'dogs'
'quantity' => '1'
2 =>
array
'animal' => 'cats'
'quantity' => '3'
However, I want to merge internal arrays with the same animal to produce this new array (with quantities combined):
$new=
array
0 =>
array
'animal' => 'cats'
'quantity' => 4
1 =>
array
'animal' => 'dogs'
'quantity' => '1'
I understand that there are similar questions on stackoverflow, but not similar enough for me to be able to figure out how to use the feedback those questions have gotted to apply to this specific example. Yes, I know I probably look stupid to a lot of you, but please remember that there was a time when you too didn't know crap about working with arrays :)
I've tried the following code, but get Fatal error: Unsupported operand types (Referring to line 11). And if I got that error to go away, I'm not sure if this code would even produce what I'm trying to achieve.
$new = array();
foreach($original as $entity){
if(!isset($new[$entity["animal"]])){
$new[$entity["animal"]] = array(
"animal" => $entity["animal"],
"quantity" => 0,
);
}
$new[$entity["animal"]] += $entity["quantity"];
}
So, I don't know what I'm doing and I could really use some help from the experts.
To try to give a super clear question, here goes... What changes do I need to make to the code so that it will take $original and turn it into $new? If the code I provided is totally wrong, could you provide an alternative example that would do the trick? Also, the only language I am familiar with is PHP, so please provide an example using only PHP.
Thank you
You're very close.
$new[$entity["animal"]] += $entity["quantity"];
needs to be
$new[$entity["animal"]]['quantity'] += $entity["quantity"];
In your if ( !isset [...] ) line, you're setting $new[$entity['animal']] to an array, so you need to access the 'quantity' field of that array before trying to add the new quantity value to it.
One of the reasons why your code is not working is that you're using the animal name as the array index, not the integer index which is used in your desired output.
Try this:
$new = array(); // Desired output
$map = array(); // Map animal names to index in $new
$idx = 0; // What is the next index we can use
foreach ($original as $entity) {
$animal = $entity['animal'];
// If we haven't saved the animal yet, put it in the $map and $new array
if(!isset($map[$animal])) {
$map[$animal] = $idx++;
$new[$map[$animal]] = $entity;
}
else {
$new[$map[$animal]]['quantity'] += $entity['quantity'];
}
}
This works:
$new = array();
$seen = array();
foreach($original as $entity) {
// If this is the first time we're encountering the animal
if (!in_array($entity['animal'], $seen)) {
$new[] = $entity;
$seen[] = $entity['animal'];
// Otherwise, if this animal is already in the new array...
} else {
// Find the index of the animal in the new array...
foreach($new as $index => $new_entity) {
if ($new_entity['animal'] == $entity['animal']) {
// Add to the quantity
$new[$index]['quantity'] += $entity['quantity'];
}
}
}
}
Your example was using the animal name as the index, yet the actual index is just an integer.
However, I think the resulting array would be easier to use and easier to read if it was formatting like this instead:
array('cats' => 4, 'dogs' => 1)
That would require different but simpler code than above... but, it wouldn't be a direct response to your question.

Incorrect PHP sizeof or count [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
The following is a print_r of an array converted from an XML document:
Array
(
[layer1] => Array
(
[item] => Array
(
[item-id] => 1886731
[item-name] => Bad Dog
[category] => pets
[link] = http://www.baddog.com/
)
[total-matched] => 1
)
)
For the above array, sizeof($myarray[layer1][item]) should return 1, but it returns 4. I get the correct number of "item" items if there are more than one of them. The same error happens regardless of whether I use "sizeof" or "count". How do I get PHP to return "1" when there is only one item?
Consequently, if there is one item, I can't access item-name using array[layer1][item][0][item-name], I have to use array[layer1][item][item-name].
There is a world of difference between:
$array1 = array(
'layer1' => array(
'item' => array(
'0' => array(
'item-id' => 123123,
'item-name' => 'Bad Dog',
'category' => 'pets',
'link' => 'http://www.baddog.com/',
),
)
)
);
and:
$array2 = array(
'layer1' => array(
'item' => array(
'item-id' => 123123,
'item-name' => 'Bad Dog',
'category' => 'pets',
'link' => 'http://www.baddog.com/',
)
)
);
Basically they are different structures, and PHP is correct to return the following:
count($array1['layer1']['item']);
/// = 1 (count of items in the array at that point)
count($array2['layer1']['item']);
/// = 4 (count of items in the array at that point)
If you wish to get a count for ['layer1']['item'] that makes sense to your app, you will always need ['layer1']['item'] to be an array containing multiple array structures... i.e. like $array1 above. This is the reason why I ask what is generating your array structure because - whatever it is - it is basically intelligently stacking your array data depending on the number of items, which is something you don't want.
Code that can cause an array to stack in this manner normally looks similar to the following:
/// $array = array(); /// this is implied (should be set elsewhere)
$key = 'test';
$newval = 'value';
/// if we are an array, add a new item to the array
if ( is_array($array[$key]) ) {
$array[$key][] = $newval;
}
/// if we have a previous non-array value, convert to an array
/// containing the previous and new value
else if ( isset($array[$key]) ) {
$array[$key] = array($array[$key],$newval);
}
/// if nothing is set, set the $newval
else {
$array[$key] = $newval;
}
Basically if you keep calling the above code, and tracing after each run, you will see the following structure build up:
$array == 'value';
then
$array == array(0 => 'value', 1 => 'value');
then
$array == array(0 => 'value', 1 => 'value', 2 => 'value');
It's the first step in this process that is causing the problem, the $array = 'value'; bit. If you modify the code slightly you can get rid of this:
/// $array = array(); /// this is implied (should be set elsewhere)
$key = 'test';
$newval = 'value';
/// if we are an array, add a new item to the array
if ( is_array($array[$key]) ) {
$array[$key][] = $newval;
}
/// if nothing is set, set the $newval as part of an subarray
else {
$array[$key] = array($newval);
}
As you can see all I've done is delete the itermediate if statement, and made sure when we discover no initial value is set, that we always create an array. The above will create a structure you can always count and know the number of items you pushed on to the array.
$array == array(0 => 'value');
then
$array == array(0 => 'value', 1 => 'value');
then
$array == array(0 => 'value', 1 => 'value', 2 => 'value');
update
Ah, I thought so. So the array is generated from XML. In this case I gather you are using a predefined library to do this so modifying the code is out of the question. So as others have already stated, your best bet is to use one of the many XML parsing libraries available to PHP:
http://www.uk.php.net/simplexml
http://www.uk.php.net/dom
When using these systems you retain more of an object structure which should be easier to count. Both the above also support xpath notation which can allow you to count items without even having to grab hold of any of the data.
update 2
Out of the function you've given, this is the part that is causing your arrays to stack in the manner that is causing the problem:
$children = array();
$first = true;
foreach($xml->children() as $elementName => $child){
$value = simpleXMLToArray($child,$attributesKey, $childrenKey,$valueKey);
if(isset($children[$elementName])){
if(is_array($children[$elementName])){
if($first){
$temp = $children[$elementName];
unset($children[$elementName]);
$children[$elementName][] = $temp;
$first=false;
}
$children[$elementName][] = $value;
}else{
$children[$elementName] = array($children[$elementName],$value);
}
}
else{
$children[$elementName] = $value;
}
}
The modification would be:
$children = array();
foreach($xml->children() as $elementName => $child){
$value = simpleXMLToArray($child,$attributesKey, $childrenKey,$valueKey);
if(isset($children[$elementName])){
if(is_array($children[$elementName])){
$children[$elementName][] = $value;
}
}
else{
$children[$elementName] = array($value);
}
}
That should stop your arrays from stacking... however if you have any other part of your code that was relying on the previous structure, this change may break that code.

PHP Nested Navigation

I am building out a database-driven navigation, and I need some help in a method to build my data structure. I'm not very experienced with recursion, but that is most likely the path this will take. The database table has an id column, a parent_id column, and a label column. The result of calling the method provides me with the data structure. The way my data structure should result in the following:
Records with a parent_id of 0 are assumed to be root elements.
Each root element contains an array of children if a child exists which holds an array of elements containing the parent_id equal to the root element id.
Children may contain a children array containing parent_ids equal to the immediate child (this would be the recursive point)
When a record exists that contains a parent_id which isn't 0, it gets added to the array of the children elements.
Here is how the data structure should look:
$data = array(
'home' => array(
'id' => 1,
'parent_id' => 0,
'label' => 'Test',
'children' => array(
'immediatechild' => array(
'id' => 2,
'parent_id' => 1,
'label' => 'Test1',
'children' => array(
'grandchild' => array(
'id' => 3,
'parent_id' => 2,
'label' => 'Test12',
))
))
)
);
Here's something I came up with in a few moments. Its not correct, but its what I want to use and Id like some help fixing it.
<?php
// should i pass records and parent_id? anything else?
function buildNav($data,$parent_id=0)
{
$finalData = array();
// if is array than loop
if(is_array($data)){
foreach($data as $record){
// not sure how/what to check here
if(isset($record['parent_id']) && ($record['parent_id'] !== $parent_id){
// what should i pass into the recursive call?
$finalData['children'][$record['label'][] = buildNav($record,$record['parent_id']);
}
}
} else {
$finalData[] = array(
'id' => $data['id'],
'parent_id' => $parent_id,
'label' => $data['label'],
)
}
return $finalData
}
Thanks for the help!
Simplest solution (assuming you've got the data stored in relational represenation using the parent id as a FK to indicate the hierarchy) is to just brute force it:
$start=array(
array('parent_id'=>0, 'title'=>'Some root level node', 'id'=>100),
array('parent_id'=>0, 'title'=>'Other root level node', 'id'=>193),
array('parent_id'=>100, 'title'=>'a child node', 'id'=>83),
....
);
// NB this method will work better if you sort the list by parent id
$tree=get_children($start, 0);
function get_children(&$arr, $parent)
{
static $out_index;
$i=0;
$out=array();
foreach($arr as $k=>$node) {
if ($node['parent_id']==$parent) {
++$i;
$out[$out_index+$i]=$node;
if (count($arr)>1) {
$out[$out_index+$i]['children']=get_children($arr, $node['id']);
}
unset($arr[$k]);
}
$out_index+=$i;
if ($i) {
return $out;
} else {
return false;
}
}
But a better solution is to use an adjacency list model for the data in the database. As an interim solution you might want to serialize the tree array and cache it in a file rather than parse it every time.

Key Manipulation In PHP Array

So I have a function that merges two arrays replacing "variables" that are in the $template array with values from the $marketArray. It's nice tight bit of code from some kind contributor's here. Now due to a new requirement I need to switch things a bit.
First I need to make it an array of arrays that essentially groups the stuff by market instead of one giant list in a single large array. Secondly I need the keys in the new arrays to be of the format market-counter
e.g. gb/en-1, gb/en-2 etc etc (this is so a JQuery gets an id it can use to determine where the results go later.
So I have entered a couple of new entries (marked //NEW) that would get the value for the market and started a counter. It's twisting my brain around the next step that hurts!
$marketArray is a multidimensional associative array of the markets like this (but a lot more markets!)
$markets = array(
array(market => 'se/sv', storeid => 'storeId=66', langid => 'langId=-14', storenumber => '109', prodid => '741586', artid => '22112334'),
array(market => 'at/de', storeid => 'storeId=82', langid => 'langId=-11', storenumber => '234', prodid => '374637', artid => '45678214')
);
$template is a bunch of url templates that need to be manipulated on a market by market basis (again shortened)
$template = array (
'/$market',
'/$market/catalog/',
'/$marketproducts/$artid',
'StockSearchForm?&productId=$prodid'
);
Here is the function
function urlBuilder($marketArray,$template) {
$urlstohit=array();
foreach ($marketArray as $m) {
$market = $m['market']; //NEW
$counter = 1; //NEW
foreach ($template as $t) {
$tt=$t;
foreach ($m as $k=>$v)
$tt=str_replace('$'.$k, $v, $tt);
$urlstohit[]=$tt;
}
}
return ($urlstohit);
}
so what I am trying to achieve is instead of one giant array like
$urlstohit (
[0] => '/se/sv/catalog/categories/',
[1] => '/se/sv/catalog/news/',
[2] => '/se/sv/catalog/categories/departments/',
[3] => '/se/sv/search/?query=giant'
[4] => '/at/de/catalog/categories/',
[5] => '/at/de/catalog/news/',
[6] => '/at/de/catalog/categories/departments/',
[7] => '/at/de/search/?query=giant'
)
a md-array grouped by market with the market-counter as keys
$urlstohit (
['se/sv'] => array(
['se/sv-1'] => '/se/sv/catalog/categories/',
['se/sv-2'] => '/se/sv/catalog/news/',
['se/sv-3'] => '/se/sv/catalog/categories/departments/',
['se/sv-4'] => '/se/sv/search/?query=giant'
),
['at/de'] => array(
['at/de-1'] => '/at/de/catalog/categories/',
['at/de-2'] => '/at/de/catalog/news/',
['at/de-3'] => '/at/de/catalog/categories/departments/',
['at/de-4'] => '/at/de/search/?query=giant'
)
)
Try this
function urlBuilder($marketArray,$template) {
$urlstohit=array();
foreach ($marketArray as $m) {
$market = $m['market'];
$counter = 1;
$urlstohit[$market] = array(); / ADDED
foreach ($template as $t) {
$tt=$t;
foreach ($m as $k=>$v)
$tt=str_replace('$'.$k, $v, $tt);
$urlstohit[$market][$market.'-'.$counter]=$tt; // EDITED
$counter++; // ADDED
}
}
} // ADDED
return ($urlstohit);
}
I've marked the lines I've added and edited (I think you were also missing a curly brace).

Categories