Reputation: 765
Okay im totally stuck on this, it should be pretty simple.
I have two arrays:
array(2) {
[0]=>
array(7) {
["id"]=>
string(1) "4"
["name"]=>
string(9) "Page name"
["content"]=>
string(133) "<div class="content col-lg-12">[Jumbo]</div><div class="content col-lg-12">[Jumbo]</div><div class="content col-lg-12">[Footer]</div>"
["container"]=>
string(1) "0"
["background"]=>
string(7) "#ff7676"
["homepage"]=>
string(1) "1"
["active"]=>
string(1) "1"
}
[1]=>
array(7) {
["id"]=>
string(1) "7"
["name"]=>
string(8) "Layout 1"
["content"]=>
string(119) "<div class="content col-lg-12">[Header]</div><div class="content col-lg-12"></div><div class="content col-lg-12"></div>"
["container"]=>
string(1) "1"
["background"]=>
string(7) "#ff7676"
["homepage"]=>
string(1) "1"
["active"]=>
string(1) "1"
}
}
and:
array(2) {
[0]=>
array(9) {
["id"]=>
string(1) "8"
["name"]=>
string(5) "Jumbo"
["headCSS"]=>
string(0) ""
["classes"]=>
string(39) "jumbotron module col-lg-6 round-corners"
["inlineCSS"]=>
string(144) "background: none repeat scroll 0% 0% rgb(229, 255, 135); border-width: 3px; border-style: solid; border-color: rgb(45, 152, 255); padding: 10px;"
["element"]=>
string(247) " <div style="background: none repeat scroll 0% 0% rgb(229, 255, 135); border-width: 3px; border-style: solid; border-color: rgb(45, 152, 255); padding: 10px;" class="jumbotron module col-lg-6 round-corners">Example</div> "
["type"]=>
string(6) "header"
["size"]=>
string(1) "6"
["content"]=>
string(7) "Example"
}
[1]=>
array(9) {
["id"]=>
string(2) "12"
["name"]=>
string(6) "Footer"
["headCSS"]=>
string(0) ""
["classes"]=>
string(30) "module round-corners col-lg-10"
["inlineCSS"]=>
string(64) "background: none repeat scroll 0% 0% transparent; padding: 10px;"
["element"]=>
string(143) "<footer style="background: none repeat scroll 0% 0% transparent; padding: 10px;" class="module round-corners col-lg-10"><p>Raaaa!</p>
</footer>"
["type"]=>
string(6) "footer"
["size"]=>
string(2) "10"
["content"]=>
string(14) "<p>Raaaa!</p>
"
}
}
Basically I want to replace the occurances of [xxx] in the first array[content] with the second array[element] that matches the array[name]. Hope that makes sense (?!)
So far I have tried this:
foreach($array1 as $layout) {
foreach($array2 as $module) {
$needle = "[" . trim($module['name']) . "]";
$pages[$layout['name']] = str_replace($needle, $module['element'], $layout['content']);
};
};
However that only seems to replace the content matched in the first array2 element and not all of them.
Any idea whats wrong?
As a side note, iv done some reading and think this might be easier with strtr() but im not sure if thats possible with my data. any advise on this would be great!
Upvotes: 0
Views: 66
Reputation: 781088
the problem is that you're always using $layout['content']
as the subject in str_replace
, rather than the updated $pages[$layout['name']]
. So you're not merging all the replacements, you're overwriting the old replacement with a new replacement.
You can solve this by doing all the replacements at once, by giving arrays of search and replacement strings to str_replace()
.
$searches = array();
$replacements = array();
foreach ($array2 as $module) {
$searches[] = "[" . trim($module['name']) . "]";
$replacements[] = $module['element'];
}
foreach ($array1 as $layout) {
$pages[$layout['name']] = str_replace($searches, $replacements, $layout['content']);
}
Upvotes: 2