Jump to content

Longest string in arrays


Nodral

Recommended Posts

Hi

 

I've written a function which should reference a couple of arrays, and return an array of the length of the longest value in the arrays.  Confused, let me explain some more.

 

<?php
function getLongest($records,$headers){
foreach ($headers as $fieldid){
	foreach($records as $recordid){
		$length[]=strlen(getContent($fieldid,$recordid));
	}
	$result[$fieldid]=max($length);
}


return $result;
}

?>

 

I pass in 2 arrays, the first ($records) being references to the record id's from my DB, the second ($headers) is the column from my DB table where I pull the record info from. 

 

I have another function getContent which cross references these 2 values and gets the actual data (this works fine)

 

I am trying to create an array of every content entry for each heading, then iterate through it and find the longest value.  Then return an array of heading reference against longest content length.

 

My content varies between 10 characters to a couple of hundred characters long, dependant on the column.  I have approx 3000 entries in the DB.

 

The results I'm getting are just not what I'd expect, i.e, I run the function against columns showing longtitude and latitude of locations, (expect the result to be approx 12 for the longest) but I get 38?!?!  I've even tried using trim() with it incase there's any leading / trailing whitespace.

 

Any thoughts?

Link to comment
Share on other sites

Aside from the fact that it would probably be more efficient to do this inside a query (so you don't have to keep re-querying the database!):

 

Why don't you try printing out the result of getContent() each time around the inner loop. This should help you find out why you're getting bogus results.

Link to comment
Share on other sites

Without all the relevant code needed to reproduce the problem (your code that displays the result or the getContent() code could in fact be where the problem lies) and showing at least one piece of wrong data that is being returned that should not be and stating how you know it should not be returned in comparison to a correct piece of data, it's not really possible to directly help you.

 

You have basically asked someone who is not standing right next to you to guess what your relevant code and data is, guess what result you got that is wrong and why it is wrong, and guess what result you expect.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.