1

I'm sorry if the title might be a bit confusing. But here is my problem. I have the following array called classes:

Array
(
    [0] => Array
        (
            [Name] => BE1A
            [Department] => Emmen
        )
     etc.

And I have an array called Departments:

Array
(
    [0] => Array
        (
            [Name] => 3 Receptie Emmen
            [code] => Emmen
        )
    etc.

These arrays will be imported into my database. But first i need replace value department in array classes to the index of the row where classes.department==departments.code. This way I can link classes with departments by using a foreign key. So array classes should be:

 Array
    (
        [0] => Array
            (
                [Name] => BE1A
                [Department] => 0
            )
         etc.

Any ideas?

2
  • did you try using foreach on your classes array and on every element using array_search for code? or you can create an array from classes with key as index of classes array and value as department and then search via array_search on that array Commented Jun 28, 2013 at 13:53
  • I thought about it, but since i have thousands of rows that need to be processed daily, this doesn't seem to be a very efficient way(i think). Commented Jun 28, 2013 at 13:57

3 Answers 3

1

This may not be the most efficient code, but it does what you want. In case multiple entries in the $departments match, it'll take the index of the first one. If no matches found, it will assign value NULL.

$classes = array(
    array('Name' => 'BE1A', 'Department' => 'Emmen'),
    array('Name' => 'Something', 'Department' => 'Another'),
    array('Name' => 'Yet more', 'Department' => 'More'),
    array('Name' => 'Yet more again', 'Department' => 'Nothing')
);

$departments = array(
    array('Name' => '3 Receptie Emmen', 'code' => 'Emmen'),
    array('Name' => 'Something else', 'code' => 'Another'),
    array('Name' => 'More stuff', 'code' => 'More')
);


for($i=0; $i<count($classes); $i++) {
    $arr = array_filter($departments, function($dpt) use($classes, $i) {
        return ($dpt['code'] == $classes[$i]['Department']);
    });
    if(count($arr) > 0) {
        reset($arr);
        $classes[$i]['Department'] = key($arr);
    }
    else {
        $classes[$i]['Department'] = NULL;
    }
}

print_r($classes);

This will output:

Array
(
    [0] => Array
        (
            [Name] => BE1A
            [Department] => 0
        )

    [1] => Array
        (
            [Name] => Something
            [Department] => 1
        )

    [2] => Array
        (
            [Name] => Yet more
            [Department] => 2
        )

    [3] => Array
        (
            [Name] => Yet more again
            [Department] => -1
        )

)

Note that it uses closures for array_filter, therefore you need to have at least php 5.3 for this to work. If you don't, then you can remove the use($classes, $i) and add global $classes, $i; inside the comparison function.

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

3 Comments

@JasperJ I expanded my answer slightly to cater for cases where departments array doesn't have a match for your code.
I am a bit scared about the code efficiency, but I'll use this at first. Thanks.
@JasperJ You need to benchmark it to be sure, however there's not really any other option except to loop over one array and find matches from the other.
1

Late to this but you can use an array iterator:

$classes = array(
  array('name' => 'Class1', 'department' => 'xyz'),
  array('name' => 'Class2', 'department' => 'foo'),
  array('name' => 'Class3', 'department' => 'bar'),
);

$departments = array(
  array('name' => 'Department1 name', 'code' => 'xyz'),
  array('name' => 'Department2 name', 'code' => 'foo'),
  array('name' => 'Department3 name', 'code' => 'bar'),  
);


$test = new ArrayIterator($classes);
while ($test->valid()) {
  $class = $test->current();
  foreach($departments as $department) {
    if ($class['department'] == $department['code']) {
      $key = $test->key();
      $classes[$key]['department'] = $key;
    }
  }
  $test->next();
}

print_r($classes);

2 Comments

Thanks, this solution is way faster then the array_filter method. +1
@JasperJ - No problem, you can change your accepted answer if you think mine works better
0

array are accessed trhough index value and you need to iterate through one of array and then uyou can use it's index to get value from other array

classes[$key]['department']==departments[$key]['code']

1 Comment

Then how would i retrieve the index of departments while iterating through classes?

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.