1

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?

1
  • and your question is...? Commented Jul 20, 2014 at 19:41

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

3 Comments

Dont use this. Use once below.
Please can you explain why. I added the third desired output.
To get the third desired output format simply call array _ keys on the original array. I will amend my answer with a code snippet when I get to work.
2

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

3 Comments

This is actually the onlu rigt answer. The others need to fill an array first fro mysql and then call additional methods on thta array to get the desited result. This gives the deisted result directly.
Per the initial question, the author displays his method for filling the data array from mysql. He is/was not asking for a method to marshal his data from the database - only for a method to get it into the desired form. Please be sure to carefully read the question before weighting in.
@Michal-sk You are right, but I am asking for the method first, and then came to understand my additional needs. You guys are great. I have updated my question with the third desired output and tried to apply the methods and queries here but was at loss. Please can you update the answers. Would be great help as I actualy used this in my project.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.