将 2 个值添加到 PHP 数组中的 1 个键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3065939/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:36:01  来源:igfitidea点击:

Add 2 values to 1 key in a PHP array

phparrays

提问by Mike Munroe

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

我有一个数据结果集,我想写入 php 中的数组。这是我的示例数据:

**Name** **Abbrev**
Mike     M
Tom      T
Jim      J

Using that data, I want to create an array in php that is of the following:

使用该数据,我想在 php 中创建一个数组,如下所示:

1|Mike|M
2|Tom|T
3|Jim|j

I tried array_push($values, 'name', 'abbreviation') [pseudo code], which gave me the following:

我尝试了 array_push($values, 'name', 'abbreviation') [伪代码],它给了我以下内容:

1|Mike
2|M
3|Tom
4|T
5|Jim
6|J

I need to do a look up against this array to get the same key value, if I look up "Mike" or "M".

如果我查找“Mike”或“M”,我需要对这个数组进行查找以获得相同的键值。

What is the best way to write my result set into an array as set above where name and abbreviation share the same key?

将我的结果集写入上面设置的数组的最佳方法是什么,其中名称和缩写共享相同的键?

回答by Walter Mundt

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

PHP 不是我的首选语言,但试试这些:

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.

诀窍是使用嵌套数组来配对每个条目中的名称和缩写。

回答by ceejayoz

$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).

也就是说,我不确定您为什么要单独存储数据。可以通过 直接从名称中获取首字母substr($name, 0, 1)

回答by Phil

You will need to create a two dimensional array to store more than one value.

您将需要创建一个二维数组来存储多个值。

Each row in your result set is already an array, so it will need to be added to your variable as an array.

结果集中的每一行都已经是一个数组,因此需要将其作为数组添加到变量中。

array_push($values, array('name', 'abbreviation'));

回答by Andreas Linden

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

回答by Tom

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.

所以你使用两个数组,一个用于第二列和第三列,使用第一列中的值作为两个数组的键。

回答by Konel Sum

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:

php 数组的工作方式类似于哈希查找表,因此为了获得所需的结果,您可以初始化 2 个键,一个使用实际值,另一个使用指向第一个的引用。例如你可以这样做:

$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
)

回答by Jake Kalstad

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

必须为键为 Mike 和 M 设置相同的值。