将2个值添加到PHP数组的1个键中 [英] Add 2 values to 1 key in a PHP array

查看:133
本文介绍了将2个值添加到PHP数组的1个键中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

使用该数据,我想在php中创建以下数组:

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

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

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

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

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

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

I need to do a look up against this array to get the same key value, if I look up "Mike" or "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?

推荐答案

PHP不是我的主要语言,但是请尝试以下方法:

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]

或:

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.

这篇关于将2个值添加到PHP数组的1个键中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆