Jump to content

PHP looping through array


Go to solution Solved by requinix,

Recommended Posts

Hi All,  I am using the below with great success.  It finds for me the MIn and max values allong with the company names. The problem that has just presented itself is some of the data returned in the SLevel is text.  I need to ignore text and only include numeric values.  Is this possible.  I have tried to work this out but have hit a brick wall.

 

Many thanks
 

Jon

 

 

 

$SLevels = array();

foreach ($xml->Type->Company as $item)
{
    $companyName = (string) $item->Name; // get the company name
    $tmp = (array) $item->$Level;
    if($tmp[0] != '-')
        $SLevels[] = array($tmp[0], $companyName); // add SLevel and company name to array
}

$minSLevel = min($SLevels); // get minimum value
$minSLevel[0] = str_replace(',', '', $minSLevel[0]);

$premium = $minSLevel[0];
$company = $minSLevel[1];

 

$maxSLevel = max($SLevels); // get minimum value
$maxSLevel[0] = str_replace(',', '', $maxSLevel[0]);

$maxpremium = $maxSLevel[0];
$maxcompany = $maxSLevel[1];

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/299830-php-looping-through-array/
Share on other sites

  • Solution

Seems weird that you have this mixture of numbers and strings, but whatever.

 

So you only want to add to $SLevels if the data is a number? I figure

if($tmp[0] != '-')
would be a good place to do that. To test for a number, try is_numeric.

Thanks for reply, ya Im not in control of the data.  Its coming from a live quote system and they replace what should be a numberwith a  text message for any companies that  dont return a quote.

 

I tired the below but I think the problem here is I just dont understand how it should work..  I can use is_numeric and even concidered strpos but no joy.  Im trying to only add to the array numbers.

 

 

if(is_numeric($tmp[0]))
        $maxLevels[] = $tmp[0];

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.