Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7552135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:41:00+00:00 2026-05-30T10:41:00+00:00

So I have a function that merges two arrays replacing variables that are in

  • 0

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'
     )
)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T10:41:03+00:00Added an answer on May 30, 2026 at 10:41 am

    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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have merge two array with php function merge_recursive (both came from sql query)
I have written a function that merges two linked list. (Note that the function
I have a project that merges in one form field two variables. Number of
1. My array I have functon that returns an array of arrays: function show_array()
In my Java code I have function that gets file from the client in
Suppose that I have two numpy arrays of the form x = [[1,2] [2,4]
I have tried the merge function to merge two csv files that I imported.
I have a function which takes two arrays containing the tokens/words of two texts
I have two functions that pulling some of content from html and returning it
I'm trying to merge two arrays that have some overlapping results and some that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.