php: how to change string data become numeric data

六月ゝ 毕业季﹏ 提交于 2019-12-02 13:34:54
SELECT `Model`,
IF(`Class`='S', 1, 0) AS `S`,
IF(`Class`='A', 1, 0) AS `A`,
IF(`Class`='B', 1, 0) AS `B`,
IF(`Class`='C', 1, 0) AS `C`
FROM `inspection_report`

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

Very crude example, you'd probably go with HTML tables in reality.

<?php // $rows = array(array('name' => 'blue', 'class_defect' => 'S'), ...); ?>

<pre>
name      s  a  b  c
<?php
foreach ($rows as $row) {
    printf('%-10s', $row['name']);  // padding with spaces
    foreach (array('s', 'a', 'b', 'c') as $col) {
        echo (strtolower($row['class_defect']) == $col) ? 1 : 0;
        echo '  ';  // just padding again
    }
}
?>
</pre>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!