简体   繁体   中英

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

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)

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>

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

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