I have this foreach statement:

foreach ($googlebasemap as $key => $linkdata) {

$xmlmap['item.' . count($xmlmap)] = array('title.0'                => $linkdata['title'],
                                          'link.0'                 => $linkdata['link'] . '?source=googlebase',
                                          'description.0'          => $linkdata['desc'],
                                          'g:price.0'              => $linkdata['price'],
                                          'g:id.0'                 => $linkdata['id'],
                                          'g:model_number.0'       => $linkdata['prodnum'],
                                          'g:brand.0'              => $linkdata['brand'],
                                          'g:condition.0'          => 'new',
                                          'g:mpn.0'                => $linkdata['mpn'],
                                          'g:image_link'           => $linkdata['img'],
                                          'g:product_type.0'       => $linkdata['ptype'],
                                          'g:payment_accepted.0'   => 'AmericanExpress',
                                          'g:payment_accepted.1'   => 'Cash',
                                          'g:payment_accepted.2'   => 'Check',
                                          'g:payment_accepted.3'   => 'Discover',
                                          'g:payment_accepted.4'   => 'MasterCard',
                                          'g:payment_accepted.5'   => 'Visa',
                                          'g:payment_accepted.6'   => 'wiretransfer',
'g:payment_notes.0' => 'All card payments through PayPal or Nochex.  Alternatively through PayOffline using the Post Office or any branch of the Alliance & Leicester building society with a variety of payment methods, including cash and cards. Wire transfer payments only in the form of BACs or IBT payments.');

}

...and $linkdata needs to be in the following format (which could vary considerably in length depending on how deep the ptype is within the category tree):

Assorted Gadgets > Bells & Whistles > Various Widgets

This is not a problem because, for the application I am trying to modify, I would make the ptype value Various Widgets, and would then append the rest of the category tree entries to the output value in order to simplify things. The additional category tree values would also all be known values, so the best way to handle this would definitely seem to be having all the category tree values in an array with some kind of merging with the $linkdata value.

The problem is in appending Assorted Gadgets > Bells & Whistles > to the Various Widgets value when output by $linkdata ...and that I what I need some advice with, - what method would work best and how to implement it, etc.?

- I have experimented using an array with str_replace using $before = array() and $after = array() values, but I was unable to get that setup to work.

Just to try and clarify things further, this is one solution that I have tried, which outputs the value of $after, ie: Hardware > Locks & Locksmithing > (not the desired result, which would be Hardware > Locks & Locksmithing > Keeps & Strikeplates in this particular example):

$before = array('Keeps & Strikeplates');
$after  = array('Hardware > Locks & Locksmithing > ');

$output = $after += $before;

foreach ($googlebasemap as $key => $linkdata) {

$xmlmap['item.' . count($xmlmap)] = array('title.0'                => $linkdata['title'],
                                          'link.0'                 => $linkdata['link'] . '?source=googlebase',
                                          'description.0'          => $linkdata['desc'],
                                          'g:price.0'              => $linkdata['price'],
                                          'g:id.0'                 => $linkdata['id'],
                                          'g:model_number.0'       => $linkdata['prodnum'],
                                          'g:brand.0'              => $linkdata['brand'],
                                          'g:condition.0'          => 'new',
                                          'g:mpn.0'                => $linkdata['mpn'],
                                          'g:image_link'           => $linkdata['img'],
                                          'g:product_type.0'       => str_replace($before, $output, $linkdata['ptype']),
                                          'g:payment_accepted.0'   => 'AmericanExpress',
                                          'g:payment_accepted.1'   => 'Cash',
                                          'g:payment_accepted.2'   => 'Check',
                                          'g:payment_accepted.3'   => 'Discover',
                                          'g:payment_accepted.4'   => 'MasterCard',
                                          'g:payment_accepted.5'   => 'Visa',
                                          'g:payment_accepted.6'   => 'wiretransfer',
'g:payment_notes.0' => 'All card payments through PayPal or Nochex.  Alternatively through PayOffline using the Post Office or any branch of the Alliance & Leicester building society with a variety of payment methods, including cash and cards. Wire transfer payments only in the form of BACs or IBT payments.');

}

The reason why I cannot just add everything to the ptype field in the database is twofold: length (which could be several hundred characters) and special characters (which I try to avoid adding to the database even when in &, >, etc. format). Also, I would like to avoid having to add the same category tree values to different ptypes over and over again, when something more like the method I am trying to use would be far more efficient.

At the moment I am using:

$before =	array('1' => "Keeps & Strikeplates");
		array('2' => "Keeps & Strikeplates");

$after   = array('1' => "Hardware > Locks & Locksmithing > ");
'g:product_type.0' => str_replace($before['1'], ($after['1'] . $linkdata['ptype']), $linkdata['ptype']),

This probably is not the best, or most efficient, way of producing the desired output but it works.

One important detail, and one that the above will hopefully work for, is that $linkdata must be accurately paired with the correct category tree values.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.