jandante@telenet.be Posted February 25, 2009 Share Posted February 25, 2009 Hi, I have written a function that returns an array. The functions runs several time so I then have 3-dimensional array (several array's from the function in layers). Now I want to "concatenate" these array's, so I can work with it. (I want only unique values from the arrays: if a value is in the first and the third array i only need it once). I think the solution is: - concatenate array's to 1 array (but how do I do this?) - loop through it looking for doubles (i have no idea how i will pull this one off but that's for later) Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/146874-concatenate-arrays/ Share on other sites More sharing options...
blintas Posted February 25, 2009 Share Posted February 25, 2009 Please post your code Quote Link to comment https://forums.phpfreaks.com/topic/146874-concatenate-arrays/#findComment-771097 Share on other sites More sharing options...
JonnoTheDev Posted February 25, 2009 Share Posted February 25, 2009 to merge arrays use array_merge() and to remove duplicates use array_unique() Quote Link to comment https://forums.phpfreaks.com/topic/146874-concatenate-arrays/#findComment-771120 Share on other sites More sharing options...
jandante@telenet.be Posted February 25, 2009 Author Share Posted February 25, 2009 <?php /* this class will search the database for relevant results. The results have different weights. 1. The full string given by the user A. in the name = 100 (peace of it 95) B. in the address = 90 (peace of it 85) C. in the VAT = 90 (peace of it 85) D. in the categorie = 90 (peace of it 85) E. in the description = 80 (peace of it 75) F. in the tags = 70 (peace of it 65) 2. Parts of the string given by the user A. in the name = 60 (peace of it 55) B. in the address = 50 (peace of it 45) C. in the category = 50 (peace of it 45) D. in the VAT = 50 (peace of it 45) E. in the description = 40 (peace of it 35) F. in the tags = 30 (peace of it 25) */ include "config.php"; include "opendb.php"; /* Class to clean up the searchstring */ class Cleaner { var $stopwords = array(" vind ", " over "); var $symbols = array('/','\\','\'','"',',','.','<','>','?',';',':','[',']','{','}','|','=','+','-','_',')','(','*','&','^','%','$','#','@','!','~','`'); function parseString($string) { $string = ' '.$string.' '; $string = $this->removeStopwords($string); $string = $this->removeSymbols($string); return $string; } function removeStopwords($string) { for ($i = 0; $i < sizeof($this->stopwords); $i++) { $string = str_replace($this->stopwords[$i],' ',$string); } //$string = str_replace(' ',' ',$string); return trim($string); } function removeSymbols($string) { for ($i = 0; $i < sizeof($this->symbols); $i++) { $string = str_replace($this->symbols[$i],' ',$string); } return trim($string); } } // check if the city entered was correct $city_English = $_POST['where']; $query_getCityID ="SELECT ID FROM loc_cities WHERE CITY_ENGLISH = '".$city_English."'"; $result_getCityID= mysql_query($query_getCityID) or die(mysql_error()); $num_result_getCityID = mysql_num_rows($result_getCityID); //check if city exists if($num_result_getCityID = 0) { echo "<br /><h2 align=\"center\">Sorry maar de gemeente '".$city_English."' is ons onbekend.</h2>"; //geef alternatieven indien er zijn, dit moet zeer geavanceerd zoals google //geef resultaten voor geolocatie include "closedb.php"; } elseif($num_result_getCityID = 1) { $city = mysql_result($result_getCityID, 0, 'ID'); //do the search and the output /* Before we search the database the string is cleaned up, we don't allow signs and some words */ $clean_string = new Cleaner(); $what = $clean_string->parseString($_POST['what']); /* now we have our cleaned string */ $l = 0; $completeArray = search($what, $city, false); $spaces = " "; if(strpos($what, $spaces)){ $words = explode(" ", $what); while($l < count($words)) { echo $words[$l]; $accountsSplitString[$l] = search($words[$l], $city, true); $l++; } } //$completeArray = array_merge($accountsFullString, $accountsSplitString[1]); $cnt = count($completeArray); echo $cnt; for($i=0; $i<$cnt; $i++) { echo $completeArray[$i][0]." met een gewicht van ".$completeArray[$i][1]."<br />"; } // arrays nog koppelen en dan kuisen en weergeven include "closedb.php"; } else { //er zijn meerdere gemeente's gevonden echo "<br /><h2 align=\"center\">Sorry maar de gemeente '".$city_English."' is ons onbekend.</h2>"; } function search($what, $where, $split) { /* we set up a dataset for our results (we don't keep track of the openingshours for the moment */ $datasetIndex = 0; $accounts; /* 1. we search for our full string in te areas name/address/VAT/description */ $fields[0][0] = "name"; $fields[1][0] = "address"; $fields[2][0] = "VAT"; $fields[3][0] = "description"; $fields[0][1] = 100; $fields[1][1] = 90; $fields[2][1] = 90; $fields[3][1] = 80; for($i=0; $i < 4 ; $i++) { $queryNameFS = "SELECT id FROM accounts WHERE ".$fields[$i][0]." LIKE '".$what."' AND POSTALCODE = ".$where; $resultNameFS = mysql_query($queryNameFS) or die (mysql_error()."mutn"); while($row = mysql_fetch_array($resultNameFS, MYSQL_ASSOC)) { $accounts[$datasetIndex][0] = $row['id']; $accounts[$datasetIndex][1] = $fields[$i][1]; $datasetIndex ++; } } /* 2. we search for our full string with jokers (%) in te areas name/address/VAT/description */ $fields[0][1] = 95; $fields[1][1] = 85; $fields[2][1] = 85; $fields[3][1] = 75; for($i=0; $i < 4 ; $i++) { $queryNameFS = "SELECT id FROM accounts WHERE ".$fields[$i][0]." LIKE '%".$what."%' AND POSTALCODE = ".$where; $resultNameFS = mysql_query($queryNameFS) or die (mysql_error()."mutn"); while($row = mysql_fetch_array($resultNameFS, MYSQL_ASSOC)) { $accounts[$datasetIndex][0] = $row['id']; $accounts[$datasetIndex][1] = $fields[$i][1]; $datasetIndex ++; } } // 3 now we search for the category for full string and full string with jokers // is there a category with the full string in? $queryCategoryFS = "SELECT auto_id FROM accounts_category WHERE name LIKE '".$what."'"; $resultCategoryFS = mysql_query($queryCategoryFS) or die (mysql_error()); while($row = mysql_fetch_array($resultCategoryFS, MYSQL_ASSOC)) { //get accounts that have this category $query = "SELECT id FROM accounts WHERE category_id = ".$row['auto_id']." AND POSTALCODE = ".$where; $result = mysql_query($query) or die (mysql_error()); while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $accounts[$datasetIndex][0] = $row['id']; $accounts[$datasetIndex][1] = 90; $datasetIndex ++; } } // is there a category with the full string in if we search with %? $queryCategoryFS = "SELECT auto_id FROM accounts_category WHERE name LIKE '%".$what."%'"; $resultCategoryFS = mysql_query($queryCategoryFS) or die (mysql_error()); while($row = mysql_fetch_array($resultCategoryFS, MYSQL_ASSOC)) { //get accounts that have this category $query = "SELECT id FROM accounts WHERE category_id = ".$row['auto_id']." AND POSTALCODE = ".$where; $result = mysql_query($query) or die (mysql_error()); while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $accounts[$datasetIndex][0] = $row['id']; $accounts[$datasetIndex][1] = 85; $datasetIndex ++; } } // 4. search for the tags database $queryGetTags = "SELECT auto_id FROM tags WHERE name LIKE '".$what."'"; $resultGetTags = mysql_query($queryGetTags) or die (mysql_error()); while($row = mysql_fetch_array($resultGetTags, MYSQL_ASSOC)) { //get accounts that have this tag $query = "SELECT account_id FROM tags_accounts WHERE tag_id = ".$row['auto_id']; $result = mysql_query($query) or die (mysql_error()); while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $query = "SELECT id FROM accounts WHERE postalcode = ".$where; $result = mysql_query($query) or die (mysql_error()); $numrows = mysql_numrows($result); $k = 0; while($k < $numrows) { $array[$k] = mysql_result($result, $k, 'id'); $k++; } if(is_array($array)) { $arrayUnique = array_unique($array); $k = 0; while(count($arrayUnique) > $k) { $accounts[$datasetIndex][0] = $arrayUnique[$k]; $accounts[$datasetIndex][1] = 70; $datasetIndex ++; $k++; } } } } // 4. search for the tags with % database $queryGetTags = "SELECT auto_id FROM tags WHERE name LIKE '%".$what."%'"; $resultGetTags = mysql_query($queryGetTags) or die (mysql_error()); while($row = mysql_fetch_array($resultGetTags, MYSQL_ASSOC)) { //get accounts that have this tag $query = "SELECT account_id FROM tags_accounts WHERE tag_id = ".$row['auto_id']; $result = mysql_query($query) or die (mysql_error()); while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $query = "SELECT id FROM accounts WHERE postalcode = ".$where; $result = mysql_query($query) or die (mysql_error()); $numrows = mysql_numrows($result); $k = 0; while($k < $numrows) { $array[$k] = mysql_result($result, $k, 'id'); $k++; } if(is_array($array)) { $arrayUnique = array_unique($array); $k = 0; while(count($arrayUnique) > $k) { $accounts[$datasetIndex][0] = $arrayUnique[$k]; $accounts[$datasetIndex][1] = 65; $datasetIndex ++; $k++; } } } } if($split == true) { $i = 0; while($datasetIndex > $i) { $accounts[$i][1] -= 40; $i++; } } return $accounts; } Could be freaky code . so i call the function search several times and get several array's back. For starters does this look any good to you? Quote Link to comment https://forums.phpfreaks.com/topic/146874-concatenate-arrays/#findComment-771131 Share on other sites More sharing options...
JonnoTheDev Posted February 25, 2009 Share Posted February 25, 2009 Its hard to say if it looks anything with just a huge lump of script posted. What matters is if it generates the results you require. You would be better posting a sample of the arrays returned by your functions and then post what you would like the final result to look like. Quote Link to comment https://forums.phpfreaks.com/topic/146874-concatenate-arrays/#findComment-771136 Share on other sites More sharing options...
jandante@telenet.be Posted February 25, 2009 Author Share Posted February 25, 2009 Maybe not a bad Idea: the array's that I'm getting back from the function: array1( 1; 100 5; 95 7; 90 ) array2( 6; 80 7; 35 2; 55 ) Depending on the input I get 1 to 5 array's back like the above. I want them to become one array like this (doubles are gone and from the second column the highest values is kept for doubles): completeArray( 1; 100 5; 95 7; 90 6; 80 2; 55 ) Am I clear on this? Quote Link to comment https://forums.phpfreaks.com/topic/146874-concatenate-arrays/#findComment-771158 Share on other sites More sharing options...
blintas Posted February 25, 2009 Share Posted February 25, 2009 $complete_array = array_merge($array1,$array2); Quote Link to comment https://forums.phpfreaks.com/topic/146874-concatenate-arrays/#findComment-771175 Share on other sites More sharing options...
jandante@telenet.be Posted February 25, 2009 Author Share Posted February 25, 2009 I'm not sure this will work because when I test with two array's I get strange results. But another problem is that I don't know how many array's there are gonna be. One time it can be 1 array other times it might be 5 array's... how can i put a loop in that? Quote Link to comment https://forums.phpfreaks.com/topic/146874-concatenate-arrays/#findComment-771403 Share on other sites More sharing options...
jandante@telenet.be Posted February 25, 2009 Author Share Posted February 25, 2009 it worked. not so easy but the function provide was the one i needed. then i sorted my multidimensional array through: function compare($x, $y) { if($x[1] == $y[1]) { return 0; } else if ($x[1] < $y[1]) { return 1; } else { return -1; } } $cleanAccounts = array_unique($accounts); But now I want to extract the doubles from the array based on the first column and the row with the highest value in the second column: array 1( 1, 100 1, 95 2, 95 1, 80 ) and it should become: arrayNew ( 1, 100 2, 95 ) Could this be achieved like the sorting function? Quote Link to comment https://forums.phpfreaks.com/topic/146874-concatenate-arrays/#findComment-771464 Share on other sites More sharing options...
JonnoTheDev Posted February 26, 2009 Share Posted February 26, 2009 Probably need to build a new array from looping over the first array Quote Link to comment https://forums.phpfreaks.com/topic/146874-concatenate-arrays/#findComment-771698 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.