简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM