What is the best way for combining data from multiple arrays enclosing semicolons?

萝らか妹 提交于 2020-01-17 15:15:26

问题


Let me explain bellow practically what i have and have i need

I'll be using this data in PHP, MySQL and WordPress Project, Currently I have these data in JSON file.

array_texts:

Link Text 1; Link Text 2; Link Text 3

array_links

https://url1.com; https://url2.com; https://url3.com

this is not limited to 3 i have more & less.

I need the best solution to use huge data from JSON to PHP/Wordpress with MySQL (Which ever works faster)

Expected result for each Link Text

<a href="https://url.com">Link Text</a>

and the whole combination as array or something like:

Link Text 1; Link Text 2; Link Text 3

<a href="https://url1.com">Link Text 1</a>; <a href="https://url2.com">Link Text 2</a>; <a href="https://url3.com">Link Text 3</a>

回答1:


How about use explode and implode to break the string, combine them with array_map (manual - notice the use of null in the function) and foreach as:

$array_texts = explode("; ", "Link Text 1; Link Text 2; Link Text 3");
$array_links = explode("; ", "https://url1.com; https://url2.com; https://url3.com");

$arr = array_map(null, $array_texts, $array_links);
foreach($arr as $aa) {
    $az[] = '<a href="' . $aa[1] . '">' . $aa[0] . '</a>';
}
echo implode("; ", $az);

This will give you the desire output

Live example 3v4l



来源:https://stackoverflow.com/questions/55602078/what-is-the-best-way-for-combining-data-from-multiple-arrays-enclosing-semicolon

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!