2

I'm trying to create hash map or a 2d array using arrays in PHP. My values are hardcoded in, but this is what I currently have. I tried the code in the comments, but I realized that wasn't actually making a 2d array.

$hash_map = array();
$query = $this->students_m->getStudentClasses('2');
foreach ($query->result_array() as $key1=>$value1) {

    $query2 = $this->students_m->getStudentsInfo('2', '10', '3');

    foreach ($query2->result_array() as $key2=>$value2) {

        /*
        if (!in_array($value2['name'],$hash_map, true)) {
            array_push($hash_map, $value2['name']); 
        }
        */

        hash_map[$value1['id']][] = $value2['name'];
    } 
}

id and student_name are the names of the columns from the sql table. I ran this code and a couple variations of it:

foreach ($hash_map as $student) {
    echo $student;
    echo "<br>";
}

And I wasn't able to get the correct output. I'd like it so each value maps to an array of unique values, so an example of what I'm going for looks like:

hash_map['10'][0] = '123'

hash_map['10'][1] = '124'

hash_map['10'][2] = '125'

hash_map['3'][1] = '123'

hash_map['3'][2] = '128'

Is what I have close? Could someone help point me in the right direction?

2 Answers 2

1

When working with multi dimension arrays you generally want to check for existing keys, not existing values.

Your commented out code should be something like this

if (!array_key_exists($value1['id'],$hash_map)) {
    $hash_map[$value1['id']] = array();
}

That will setup the outer array, allowing you to add elements to it.

There are a couple of ways to prevent duplicate data being put into the second part of the array. If we assume that the name field is unique, or we just want to know the unique entries, then

if (!in_array($value2['name'],$hash_map[$value1['id']]) {
    $hash_map[$value1['id'][]=$value2['name'];
}

will work fine, however, user's names might not be unique (John Smith anyone?), and so you might want to check $value2['id'] in some manner instead.

One way to accomplish this would be to use $value2['id'] as the key for the second part of the array

$hash_map[$value1['id'][$value2['id']]=$value2['name'];

This will prevent duplicate keys from existing since they will just overwrite one another.

If your inner array MUST start at 0 and continue to X where X is the number of entries, then an "exists" array is probably best

$exists = array();
if (!in_array($value2['id'],$exists)) {
    $hash_map[$value1['id']][]=$value2['name'];
    $exists[]=$value2['id'];
}

Then, for printing you will probably want something like:

foreach ($hash_map as $student) {
    echo implode(",",$student);
    echo "<br>";
}
Sign up to request clarification or add additional context in comments.

3 Comments

I forgot to mention that there shouldn't be any duplicate keys, but I'd like to set it up so there aren't any duplicate values as well. Inside of the if loop that you have, could I just use something similar to what I have commented out above?
Both answers were helpful to getting me to the right answer, but I forgot to check for duplicate keys in addition to checking for duplicate values. So I used array_key_exists and in_array inside of that.
Edited based on your comments.
1

Use:

if (!in_array(value2['name'], $hash_map[$value1['id']])) 
{ 
  hash_map[$value1['id']][] = $value2['name'];
} 

This will avoid adding duplicate values.

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.