[英]get csv values from two coloumns mysql csv using php
參考圖片
我在mysql中有這樣的值。
我需要基於Wholesaletlcontact的Wholesaletl名稱。
示例:當我具有值ajin3時,我需要值1234587452
從db獲取字段值。 將它們分解為陣列。
$myString = "ajin1,ajin3,ajin2";
$myArray = explode(',', $myString);
print_r($myArray);
對其他字段值執行相同的操作。
輸出將是:數組([0] => ajin1 [1] => ajin3 [2] => ajin2)
然后獲取特定值的ID:
$key = array_search('ajin3', $myArray);
並且該ID將對應於另一個數組中的所需值。
這可能是您的解決方案
$wholesaletl=explode(",",$record->wholesaletl); $wholesaletlcontact=explode(",",$record->wholesaletlcontact); $wholesaletlcontact_arr=array(); if(count($wholesaletl) > 0){ foreach($wholesaletl as $key => $value){ $wholesaletlcontact_arr[$value]=$wholesaletlcontact[$key]; } echo $wholesaletlcontact_arr["ajin3"];
如果您對使用csv文件沒有選擇,或者您認為缺點並不重要,那么以下代碼應該可以幫助您訪問信息:
function get_contact_from_name($name, &$name_array, &$contact_array) {
$name_index = array_search($name, $name_array);
// Name is not in csv list
if ($name_index === false) {
echo "Name does not exist: $name<br />";
return null;
}
// There is no contact in the corresponding postion
if (!isset($contact_array[$name_index])) {
echo "Contact does not exist at position: $name_index<br />";
return null;
}
return $contact_array[$name_index];
}
$names = 'bill,george,sophia,marge';
$wholesaleltnames = str_getcsv("bill,george,sophia", ",");
$wholesaleltcontacts = str_getcsv("123,456", ",");
foreach (explode(',', $names) as $name) {
$c = get_contact_from_name($name, $wholesaleltnames, $wholesaleltcontacts);
if ($c) {
echo "Contact: $name, $c<br />";
}
}
您將需要對其進行修改以適合您的需求。 例如,將需要進行更新以使用mysql返回的行並正確處理錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.