PHP CSV to Associative Array with Row Headings

前端 未结 3 683
我寻月下人不归
我寻月下人不归 2021-01-29 06:43

I have a CSV file with headings in both the columns and rows that i need as an associative array and the examples i have tried on SO haven\'t worked for me.

The closest

3条回答
  •  广开言路
    2021-01-29 07:08

    You need a second foreach to achieve what you want.

    Something like this:

    foreach ($all_rows as $row) {
        if($row) {
            foreach($row as $key => $val) {
                if($key == "Food" && $val == "Apples") {
                    echo "Apples: " . "
    "; echo $row["In_Stock"] . "
    "; echo $row["On_order"] . "
    "; } else if($key == "Food" && $val == "Oranges") { echo "Oranges: " . "
    "; echo $row["In_Stock"] . "
    "; echo $row["On_order"] . "
    "; } } } }

    If you just want to print the values on your page, this could do it.

    But I since you've mentioned to just call the values based on food type, you can create a function out of this.

提交回复
热议问题