Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9236325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:15:54+00:00 2026-06-18T07:15:54+00:00

I want to create an associative array in php with dynamic key and also

  • 0

I want to create an associative array in php with dynamic key and also a dynamic value from a particular mysql table.

The table name is monthly_salary with a two column named month and salary respectively.

I get the data inside it:

$sql = mysql_query('SELECT * FROM monthly_salary');
$sql2 = mysql_query('SELECT * FROM monthly_salary');

Then assigned and concatenated the collected data to $mon and $sal:

$mon = "";
$sal = "";
while($row = mysql_fetch_array($sql)){
    $mon .= $row['month'].", ";
}
while($row = mysql_fetch_array($sql2)){
    $sal .= $row['salary'].", ";
}

After that I’ve converted it to array and concatenate it until it became and associative array:

$monArray = array(substr(trim($mon), 0, -1));
$salArray = array(substr(trim($sal), 0, -1));
$key = "";
$keyWithVal = "";
foreach($monArray  as $k){
    $key .= $k." => ";
}
foreach($salArray  as $k){
    $keyWithVal .= $key.$k.",";
}

$associativeArray = array(substr(trim($keyWithVal), 0, -1));

My Problem is that when I’ve echo it the result is always like this
3500=>Jan=>3500:

foreach($associativeArray  as $k => $id){
    echo $k."=>".$id;
}

So how can I fix it and with the correct output Jan=>3500?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T07:15:55+00:00Added an answer on June 18, 2026 at 7:15 am

    You are way over-complicating this problem. This can be done simply, with fewer loops.

    First, you only need to run the SQL once. Second, build the array in the 1st loop.

    $sql = mysql_query('SELECT * FROM monthly_salary');
    
    $associativeArray = array();
    while($row = mysql_fetch_array($sql)){
       // Put the values into the array, no other variables needed
       $associativeArray[$row['month']] = $row['salary'];
    }
    
    foreach($associativeArray as $k => $id){
        echo $k."=>".$id;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Example is in PHP but i want to create same associative 2d array in
I have many inputs on a page. I want to create an associative array
In my PHP-script I have a multidimensional and associative array I want to transform
I'm trying to create associative array of objects from row result set with member
I'm trying to create an associative array with dynamic data, and having some trouble.
I want to append the key from $teasers to the key of the array
I want to create an associative array: var aa = {} // Equivalent to
I want to create an array of associative arrays in a while loop. In
I have a string string = masterkey[key1][key2]; I want to create an associative array
i want create multiple search where statement $where_search is a multiple condition from post

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.