12

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?

2
  • 5
    Why are you running the same SQL query twice? Commented Feb 4, 2013 at 18:17
  • What do you expect array(substr(trim($mon), 0, -1)) to do? All this does is convert the string to an array, which gives you an array with one element. array('1, 2, 3') doesn't make an array of 3 elements, it makes an array on one element, the string '1, 2, 3'. To turn a string into an array, use explode. $array = explode(', ', '1, 2, 3);. Commented Feb 4, 2013 at 18:29

3 Answers 3

36

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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

To show the association, instead of using a loop, you could do echo "<pre>"; print_r($associativeArray);
@Strixy: True, I was just copying the code from the question.
7

Why don't you just do:

$associativeArray = array();
while($row = mysql_fetch_array($sql)){
    $associativeArray[$row['month']] = $row['salary'];
}

3 Comments

What is the difference between this answer and the one provided by Rocket Hazmat?
None, we just posted at the same time. He/she beat me to it.
@DiscoInfiltrator: That's because I'm a ninja ^^
0

Following worked for me for creating associative array. array_push don't work on associative array but using unary operator does work:

$associativeArray += [$key => $value];

Comments

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.