Welcome Guest, Not a member yet? Register   Sign In
Merge Arrays
#1

[eluser]Jay Logan[/eluser]
Working with a new API that basically gives me a second database of schools around the country. I already have a decent sized DB of schools myself. What I want to do is allow the user to search for a school and if it exists in my DB, don't show the school from the second DB. I'm matching the DB's up with a column in my DB. What I need help with is the arrays that get spit out from each DB. See below.

Code:
[0] => Array
        (
            [ID] => 4167
            [Name] => Pope High School
            [Abbreviation] => Pope
            [TeamType] => H
            [City] => Marietta
            [State] => GA

        )

    [1] => Array
        (
            [ID] => 4167
            [Name] => Pope High School
            [Abbreviation] => PHS
            [TeamType] =>
            [City] => Marietta
            [State] => GA
        )

Key "1" is my item and is the only item that should show. Notice that the ID's are the same. Is there a way I can search if ID matches - and if they match, only show my item?
#2

[eluser]drewbee[/eluser]
Check out array_unique. array_unique will keep the first matched element and remove the others. So if you want your array to override the other array, during the merge you will want to add your data first, then the api database data second.

I have never used it to compare arrays before, so lets hope it works Smile
#3

[eluser]Jay Logan[/eluser]
Don't know if that will work for me. Because there could be several results at a given time. I wouldn't want them all condesed into one key. But I will mess around with it. Thanks.
#4

[eluser]jedd[/eluser]
It might be worth the pain to massage the data (both yours and theirs) up front, and get the array's key changed to what is now the ID. It would make PHP's various array merge functions a lot more useful. Well, more predictable at least.

I just used array_merge_recursive() to overlay an admin menu on top of a standard user's menu - this was after looking at the range of 'combine these two arrays' functions in PHP - none of them were *quite* what I needed, until I re-jigged my array structure.
#5

[eluser]Jay Logan[/eluser]
Well the keys from my database are aliases so that they match the API keys. I thought this might make it easier for me to work with. I don't think I can change the keys from API if that's what you mean. And really, I'm not trying to merge arrays. More like check for duplicate values of the ID key. If found, use second item or something like that. I see some array functions that use a callback. Maybe that is the path I need to head towards.
#6

[eluser]Jay Logan[/eluser]
I think I have a working solution. What I'm doing is combining the arrays then creating a new array using the ID as the key. So when a duplicate ID is found, it will overwrite the item with the second ID's info - which will be the info from my database.

Code:
$values = array('name' => $this->input->post('name'), 'state' => $this->input->post('state'));
            $get_milesplit_results = $this->milesplit->call_method('teams/search', $values);
            $milesplit_results = $get_milesplit_results['content']['list'];
            $ptentry_results = $this->teams_model->search();

            $combined_results = array_merge($milesplit_results, $ptentry_results);
            foreach ($combined_results as $result) {

                $results[$result['ID']]['Name'] = $result['Name'];
                $results[$result['ID']]['Abbreviation'] = $result['Abbreviation'];
                $results[$result['ID']]['City'] = $result['City'];
                $results[$result['ID']]['State'] = $result['State'];
                $results[$result['ID']]['TeamType'] = $result['TeamType'];
            }

            $data['teams'] = $results;
            $this->layout->view('teams/choose', $data);




Theme © iAndrew 2016 - Forum software by © MyBB