Jump to content

arrays.. please help:)


phorcon3

Recommended Posts

<?php

/*
1: name
2: type
3: city
4: state
5: country
*/

$Arrays=array(
1=>array('Name1','1','Minneapolis','MN','US'),
2=>array('Name2','1','Minneapolis','MN','US'),
3=>array('Name3','1','Chicago','IL','US'),
4=>array('Name4','1','Minneapolis','MN','US'),
5=>array('Name5','1','Milwaukee','WI','US'),
6=>array('Name6','1','Chicago','IL','US'),
7=>array('Name7','1','Berlin','','DE'),
8=>array('Name8','1','Vienna','','AT'),
9=>array('Name9','1','London','','UK'),
10=>array('Name10','1','St. Paul','MN','US'),
11=>array('Name11','1','St. Cloud','MN','US'),
12=>array('Name12','1','Delano','MN','US'),
13=>array('Name13','1','Minnetonka','MN','US'),
);

?>

 

okay ..so what i wanna do is create a list with 3 columns

 

criteria:

1. must be in the US

2. must be in the US-State Minnesota (MN)

3. type must be 1

 

so.. since minneapolis is listed more than just once, i gotta make sure i group it and have it displayed only once...

 

critera cont'd:

4. group existing once

5. sort them by City in ascending order

6. make 3 columns

 

does anyone know how to do this?

 

ie:

 

<?php

$list='';
foreach($Arrays as $id=>$val)
{
$i='';
if($val[4]=='US' && $val[3]=='MN' && $val[1]==1)
{
//group existing once?

$i++;

//sort asc -> sort function?

//make three columns

$city_total=5;//<-gotta find the total..

$break=ceil($city_total/3);

$nc='';
if($i==$break||$i==($break*2))
{
$nc='</td><td>';
}

$list.=$val[0].$nc;
}

echo '<table><tr><td>'.$list.'</td></tr></table>';

?>

 

and it should display:

 


Delano                   Minnetonka                  St. Paul
Minneapolis              St. Cloud

 

question: or do u think it would be better to put that all into a MySQL table? but the problem is that it could contain up to 40,000+ such entries...

Link to comment
https://forums.phpfreaks.com/topic/105714-arrays-please-help/
Share on other sites

I wrote this for you, it should suite your needs, i have only done some tests,

 

 

 

<?php

/*
1: name
2: type
3: city
4: state
5: country
*/

$Arrays=array(
1=>array('Name1','1','Minneapolis','MN','US'),
2=>array('Name2','1','Minneapolis','MN','US'),
3=>array('Name3','1','Chicago','IL','US'),
4=>array('Name4','1','Minneapolis','MN','US'),
5=>array('Name5','1','Milwaukee','WI','US'),
6=>array('Name6','1','Chicago','IL','US'),
7=>array('Name7','1','Berlin','','DE'),
8=>array('Name8','1','Vienna','','AT'),
9=>array('Name9','1','London','','UK'),
10=>array('Name10','1','St. Paul','MN','US'),
11=>array('Name11','1','St. Cloud','MN','US'),
12=>array('Name12','1','Delano','MN','US'),
13=>array('Name13','1','Minnetonka','MN','US'),
);

//Edit these to suit
$Search = array(null,'1',null,'MN','US');
$PrimeIndex = 2;
$SortIndex = 3;

$newArray = SortArray($Arrays, $Search, $PrimeIndex, $SortIndex);

//output
echo "<pre>";
print_r($newArray);

//you can build the table

function SortArray($Arrays, $Search, $PrimeIndex, $SortIndex)
{
$list = array();

foreach($Arrays as $A)
{
	foreach($A as $K => $item)
	{
		$found = true;
		if( !is_null($Search[$K]) && $item != $Search[$K])
		{
			$found = false;
		}
	}
	if($found) $list[$A[$PrimeIndex]] = $A;
}
uasort($list,"SortIndex");
return $list;
}


function SortIndex($a,$b)
{
global $SortIndex;
$sortable = array(strtolower($a[$SortIndex]),strtolower($b[$SortIndex]));
$sorted = $sortable;
natsort($sorted);   
return ($sorted[0] == $sortable[0]) ? -1 : 1;
}
?>

Link to comment
https://forums.phpfreaks.com/topic/105714-arrays-please-help/#findComment-541652
Share on other sites

id just have one more thing to add ...i could be wrong tho:p

 

shouldnt:

 

<?php

if($found){$list[$A[$PrimeIndex]]=$A;}

?>

 

be:

 

<?php

if($found){if($A[3]==$Search[3]){$list[$A[$PrimeIndex]]=$A;}}

?>

 

or else it would list all cities in the US ...and not just the ones in MN..? :-X

Link to comment
https://forums.phpfreaks.com/topic/105714-arrays-please-help/#findComment-541692
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.