gpt4 book ai didi

php - 如何使用php获取唯一的单词对(字符串)并插入到mysql表中

转载 作者:行者123 更新时间:2023-11-29 07:38:36 24 4
gpt4 key购买 nike

代码:

$a=array("apple","bird","cat","dog");
$b=array("bird","cat","dog","flog");

$res = array();
foreach($a as $v1) {
foreach($b as $v2) {
$t = array($v1, $v2);
asort($t);
$val = implode('', $t);
if(!in_array($val, $res))
print $val;
print "/";
}
}

本文代码由@pala_重写 How to using php count the word pair (string) array insert into MySQL

这是输出

applebird/applecat/appledog/appleflog/birdbird/birdcat/birddog/birdflog/birdcat/catcat/catdog/catflog/birddog/catdog/dogdog/dogflog/

好的,我的问题是我是否想将此组合字符串拆分为两个字符串

例如:applebird ----> applebird 并插入 MySQL 表

喜欢:

来源 |目标

苹果|鸟

苹果|猫

苹果|狗

......类似的东西,有什么想法吗?谢谢。

最佳答案

删除内爆,将它们存储为多维数组。:

$a=array("apple","bird","cat","dog");
$b=array("bird","cat","dog","flog");

$res = array();
foreach($a as $v1) {
foreach($b as $v2) {
$t = array($v1, $v2);
asort($t);
if(!in_array($t, $res)) {
print $res[] = $t;
// alternatively, instead of building them into an array for further processing
// you could immediately execute your sql query here, using $t[0], $t[1] as
// the values (thanks to comments). If you are using prepared statements
// (which you should be), if you prepare the statement outside of the loops
// you can simply execute it here, passing $t[0] and $t[1] as the bound parameters
}
}
}

foreach($res as $pair) {
...
// build your query using whatever mysql interface you are using
// $pair[0] will be one word, $pair[1] will be the other
}

关于php - 如何使用php获取唯一的单词对(字符串)并插入到mysql表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29840384/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com