Add 2 values to 1 key in a PHP array

后端 未结 7 1039
面向向阳花
面向向阳花 2021-02-08 03:35

I have a result set of data that I want to write to an array in php. Here is my sample data:

**Name** **Abbrev**
Mike     M
Tom      T
Jim      J
7条回答
  •  渐次进展
    2021-02-08 04:07

    PHP's not my top language, but try these:

    array_push($values, array("Mike", "M"))
    array_push($values, array("Tom", "T"))
    array_push($values, array("Jim", "J"))
    
    $name1 = $values[1][0]
    $abbrev1 = $values[1][1]
    

    or:

    array_push($values, array("name" => "Mike", "abbrev" => "M"))
    array_push($values, array("name" => "Tom", "abbrev" => "T"))
    array_push($values, array("name" => "Jim", "abbrev" => "J"))
    
    $name1 = $values[1]["name"]
    $abbrev1 = $values[1]["abbrev"]
    

    The trick is to use a nested array to pair the names and abbreviations in each entry.

提交回复
热议问题