Jump to content

[SOLVED] "1.2.3.4.5" minus "2.4.5"


Evan69

Recommended Posts

Hi, im working on a script where i have the user ids stored in a field separated by periods. I am trying to create a function that can subtract two sets of users so it would basically complete the equation:

 

1.2.3.4 - 2.4 = 1.3

 

So far i have managed to get it so you can subract one number from an array, but i am stuck as to how i would subract a set of numbers?

Heres the code i have, theres so other stuff in there that isnt so important, its the second half of the function that matters:

	function get_users($minus)
{
	$this->query = mysql_query("SELECT * FROM cal_users");
	$num = mysql_num_rows($this->query);

	$users = array();	
	$out = array();

	if($num > 0)
	{
		while($res = mysql_fetch_assoc($this->query))
		{
			array_push($users, $res);
		}
	}else{
		$users = '';
	}

	//This is whats important
	if($minus > 0)
	{

		$del = explode(".", $minus);

		foreach($users as $user)
		{
			if($user['id'] == $minus)
			{
				continue;
			}else{
				array_push($out, $user);
			}
		}
	}else{
		$out = $users;
	}

	return $out;
}

Any help would be much appreciated!

Link to comment
https://forums.phpfreaks.com/topic/83238-solved-12345-minus-245/
Share on other sites

try this

<?php

$strData = "1.2.3.4";
$strMinus = "2.4";

$arrData = explode(".",$strData);
$arrMinus = explode(".",$strMinus);

$arrResult = array_diff($arrData,$arrMinus);

print_r($arrResult);

?>

 

you can incorperate the same in your code..

 

hope its helpful

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.