[英]Assigning Index to PHP Array
I dynamically pull the following array from mysql table and need to assign index to each of the element. 我从mysql表中动态拉出以下数组,并且需要为每个元素分配索引。
Array
(
[yma] => 65.0000
[mhip] => 65.0000
[tzp] => 40.0000
[mzp] => 45.0000
[su] => 40.0000
[tkp] => 74.0000
)
here is my expected output. 这是我的预期输出。
Array
(
[0][yma] => 65.0000
[1][mhip] => 65.0000
[2][tzp] => 40.0000
[3][mzp] => 45.0000
[4][su] => 40.0000
[5][tkp] => 74.0000
)
Or 要么
Array
(
[0] => 65.0000
[1] => 65.0000
[2] => 40.0000
[3] => 45.0000
[4] => 40.0000
[5] => 74.0000
)
There can be issue with the answer below if my desired output is like below: 如果我想要的输出如下所示,则答案可能有问题:
Array
(
[0] => 'yma'
[1] => 'mhip'
[2] => 'tzp'
[3] => 'mzp'
[4] => 'su'
[5] => 'tkp'
)
I am using this code for pulling the data: 我正在使用以下代码提取数据:
$myarray = array();
while($row = mysql_fetch_array($sql)){
$myarray[$row['pawl']] = $row['contribution'];
}
How can I take the array I am fetching/building from MySQL and generate the desired output? 如何获取要从MySQL提取/构建的数组并生成所需的输出?
Use array_values
. 使用
array_values
。 "Array_values() returns all the values from the array and indexes the array numerically." “ Array_values()返回数组中的所有值并以数字方式对数组进行索引。”
Also, 也,
$myarray = array();
while($row = mysql_fetch_array($sql)){
$myarray[] = $row['contribution'];
}
This will assign a numeric index starting from 0. 这将分配一个从0开始的数字索引。
Now, if you need a specific numeric index to match each pawl code, 现在,如果您需要一个特定的数字索引来匹配每个棘爪代码,
Then build a map: 然后建立地图:
$map = array
(
['yma'] => 1,
['mhip'] => 2,
['tzp'] => 3,
['mzp'] => 4,
['su'] => 5,
['tkp'] => 6,
);
And Remap the pawl codes. 并重新映射 棘爪代码。
$remappedArray = array();
foreach($myarray as $pawlCode => $value){
$newIndex = $map[$pawlCode];
$remappedArray[$newIndex] = $value;
}
How you want to handle missing/new codes is up to you. 您要如何处理丢失的/新的代码由您决定。
In order to create the array you want (I will focus on example two because it is more reasonable) you just need one function - array_values . 为了创建您想要的数组(我将重点放在示例二,因为它更合理),您只需要一个函数-array_values 。
Given an input array of 给定一个输入数组
Array
(
[yma] => 65.0000
[mhip] => 65.0000
[tzp] => 40.0000
[mzp] => 45.0000
[su] => 40.0000
[tkp] => 74.0000
)
It will return a numerically indexed array of just the values, which will look like this: 它将返回仅包含值的数字索引数组,如下所示:
Array
(
[0] => 65.0000
[1] => 65.0000
[2] => 40.0000
[3] => 45.0000
[4] => 40.0000
[5] => 74.0000
)
If you need the keys, rather than the values you can simply use the array_keys function on the same starting array (see input array above) - and you will get the following output: 如果需要键而不是值,则可以在同一起始数组上使用array_keys函数(请参见上面的输入数组)-您将获得以下输出:
Array
(
[0] => 'yma'
[1] => 'mhip'
[2] => 'tzp'
[3] => 'mzp'
[4] => 'su'
[5] => 'tkp'
)
PHP has a large suite of array functions and in general you can most likely find a function to help you get/do whatever you need with an array. PHP具有大量的数组函数,通常,您很可能会找到一个函数来帮助您获取/执行数组所需的一切。 I encourage you to take some time and scan through these at some point.
我鼓励您花一些时间并仔细阅读这些内容 。 It pays to know what is available to you.
知道您有什么可用是值得的。
Use this for your first expected array: 将此用于您的第一个预期数组:
$myarray = array();
while($row = mysql_fetch_array($sql)){
$myarray[] = array($row['pawl'] => $row['contribution']);
}
And this for your second expected array: 这是您的第二个预期数组:
$myarray = array();
while($row = mysql_fetch_array($sql)){
$myarray[] = $row['contribution'];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.