Add 2 values to 1 key in a PHP array

后端 未结 7 1028
面向向阳花
面向向阳花 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.

    0 讨论(0)
  • 2021-02-08 04:09

    maybe you create a simple class for that as the abbreviation is redundant information in your case

    class Person
    {
        public $name;
    
        pulbic function __construct($name)
        {
            $this->name = (string)$name;
        }
    
        public function getAbbrev()
        {
            return substr($this->name, 0, 1);
        }
    
        public function __get($prop)
        {
            if ($prop == 'abbrev') {
    
                return $this->getAbbrev();
            }
        }
    }
    
    
    $persons = array(
        new Person('Mike'),
        new Person('Tom'),
        new Person('Jim')
    );
    
    foreach ($persons as $person) {
    
       echo "$person->name ($person->abbrev.)<br/>";
    }
    
    0 讨论(0)
  • 2021-02-08 04:09
    $person = array('name' => 'Mike', 'initial' => 'M');
    array_push($people, $person);
    

    That said, I'm not sure why you're storing the data separately. The initial can be fetched directly from the name via substr($name, 0, 1).

    0 讨论(0)
  • 2021-02-08 04:10

    have to set the same value to both Mike and M for keys.

    0 讨论(0)
  • 2021-02-08 04:12

    php arrays work like hash lookup tables, so in order to achieve the desired result, you can initialize 2 keys, one with the actual value and the other one with a reference pointing to the first. For instance you could do:

    $a = array('m' => 'value');
    $a['mike'] = &$a['m']; //notice the end to pass by reference
    

    if you try:

    $a = array('m' => 'value');
    $a['mike'] = &$a['m'];
    
    print_r($a);
    
    $a['m'] = 'new_value';
    print_r($a);
    
    $a['mike'] = 'new_value_2';
    print_r($a);
    

    the output will be:

    Array
    (
        [m] => value
        [mike] => value
    )
    Array
    (
        [m] => new_value
        [mike] => new_value
    )
    Array
    (
        [m] => new_value_2
        [mike] => new_value_2
    )
    
    0 讨论(0)
  • 2021-02-08 04:14

    You could use two separate arrays, maybe like:

    $values_names = array();
    $values_initials = array();
    array_push($values_names, 'Mike');
    array_push($values_initials, 'M');
    array_push($values_names, 'Tom');
    array_push($values_initials, 'T');
    array_push($values_names, 'Jim');
    array_push($values_initials, 'J');
    

    So you use two arrays, one for each of the second and third columns using the values in the first one as keys for both arrays.

    0 讨论(0)
提交回复
热议问题