我从一个文本文件中创建了这个数组,每行都用";"进行了扩展。
如何将'='符号之前的值设置为键,将'=''符号之后的另一个值设置为值?
Array
(
[0] => Array
(
[0] => title=PHILIPS MIXER H31453
[1] => gramaj=buc
[2] => greutate=1
[3] => prettotal=116.07
[4] => pretredus=0
[5] => poza=110.jpg
[6] => pretunitar=116.07
[7] => categorie=2210
[8] => brand=PHILIPS
[9] => descriere=
[10] => vizibil=0
[11] => cod=110
[12] => ordine=1
[13] => stoc=0
[14] => nou=0
[15] => cant_variabila=0
[16] => congelat=0
[17] => tva=24
[18] => alias=philips-mixer-h31453
)
[0] => Array
(
[0] => titlu=PHILIPS MIXER H31453
[1] => gramaj=buc
[2] => greutate=1
[3] => prettotal=116.07
[4] => pretredus=0
[5] => poza=110.jpg
[6] => pretunitar=116.07
[7] => categorie=2210
[8] => brand=PHILIPS
[9] => descriere=
[10] => vizibil=0
[11] => cod=110
[12] => ordine=1
[13] => stoc=0
[14] => nou=0
[15] => cant_variabila=0
[16] => congelat=0
[17] => tva=24
[18] => alias=philips-mixer-h31453
)
)
结果应该是:
[titlu] => PHILIPS MIXER H31453
[gramaj] => buc
[greutate] => 1
[prettotal] => 116.07
等等…
这就是我需要的地方。
function runSql(){
session_start();
header('Content-type: text/html;charset=utf8');
global $outcount;
$db = $this->pdoconnect();
$db->setErrorLog(true);
$files = $_SESSION['sql'];
$to_process = array_shift($files);
$get_sql = @file_get_contents($to_process);
@unlink($to_process);
$split = explode(";", $get_sql);
unset($split[count($split) - 1]); // Clear the last empty element
$final_array = array();
foreach ($split as $row) {
$final_array[] = explode(',', $row); // Explode each row by , to each row of final_array
}
//$stmt = $db->insertBatch('produse', $final_array, true)->getAllLastInsertId();
echo 1;
}
只需做同样的事情,分解每个项目,并使用第一部分作为关键字,第二部分作为值
$split = explode(";", $get_sql);
$final_array = array();
foreach ($split as $row) {
$arr = explode(',', $row);
$res = array();
foreach($arr as $item) {
$parts = explode("=", $item);
$res[$parts[0]] = $parts[1];
}
$final_array[] = $res;
}