php: how to change string data become numeric data

前端 未结 3 2008
我在风中等你
我在风中等你 2021-01-29 14:27

can you tell how to change this result in php and mysql script:

  Model                  Class
Ball                        S
Book                        A
Spoon
         


        
3条回答
  •  迷失自我
    2021-01-29 15:22

    Your question is a little unclear, but I'm assuming you have the input data in an array mapping name to defect, and you want for each row a 1 in the appropriate column and a zero everywhere else. If so, it's just this:

    $arr = array('blue' => 'S', 'red' => 'A', 'yellow' => null, 'green' => 'B', 'black' => 'C');
    
    $defects = array_filter(array_unique(array_values($arr)));
    echo "name\t";
    echo implode("\t", $defects);
    echo "\n";
    
    foreach($arr as $name => $defect) {
        echo "$name";
        foreach($defects as $test) {
            echo "\t";
            echo $test == $defect ? 1 : 0;
        }
        echo "\n";
    }
    

提交回复
热议问题