简体   繁体   中英

Assigning Index to PHP Array

I dynamically pull the following array from mysql table and need to assign index to each of the element.

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?

Use array_values . "Array_values() returns all the values from the array and indexes the array numerically."

Also,

$myarray = array();
while($row = mysql_fetch_array($sql)){
     $myarray[] = $row['contribution'];
}

This will assign a numeric index starting from 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 .

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    
        (
            [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. 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'];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM