简体   繁体   中英

get csv values from two coloumns mysql csv using php

enter image description here

Refer Image

I have values like this in mysql.

I need wholesaletl name based on wholesaletlcontact.

Example: when I have value ajin3 then i need the value 1234587452

Get the field values from db. Explode them to an array.

$myString = "ajin1,ajin3,ajin2";
$myArray = explode(',', $myString);
print_r($myArray);

Do the same for the other field values.

Output will be: Array ( [0] => ajin1 [1] => ajin3 [2] => ajin2 )

Then get the id for a specific value:

$key = array_search('ajin3', $myArray);

And that Id will correspond to the required value in the other array.

这可能是您的解决方案

$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"];

If you don't have a choice about using the csv files or if you don't feel that the drawbacks matter, the following code should help you with accessing the information:

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 />";
    }
}

You will need to modify it to fit your needs. For example, it would need to be updated to use the row returned from mysql and to handle errors properly.

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