What is the best way for combining data from multi

2019-12-16 19:48发布

问题:

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