这是我的数组
$array = array( 0 => 10, 1 => 9, 2 => 8, 3 => 6, 4=> 4 );
我想获得数组值6.因为在此值/系列中断之前缺少7。
请帮助我如何轻松完成?快速的方法。
答案 0 :(得分:0)
我不确定我理解你想要达到的目标,但让我们在这里开始吧。
更新:
for($i = 0; $i < count($array); ++$i) {
if($i > 0) {
if($array[$i] != ($array[$i-1]-1)) {
echo($array[$i]);
break;
}
}
}
输出:
6
答案 1 :(得分:0)
循环播放,保存以前的值并与当前值进行比较:
<?php
function findMissing($array) {
$missing = [];
foreach($array as $key => $val) {
if(isset($previousValue) && $previousValue-1!=$val) {
echo "not in series: ".($previousValue-1) .", returning ".$val."<br>\n";
$missing[] = $val;
}
$previousValue=$val;
}
return $missing;
}
// USAGE:
$array = array( 0 => 10, 1 => 9, 2 => 8, 3 => 6, 4=> 4 );
findMissing($array);
// not in series: 7, returning 6
// not in series: 5, returning 4
$array2 = array( 10, 9, 8, 6, 5 );
$missingValues = findMissing($array2);
// not in series: 7, returning 6
var_dump($missingValues);
// array(1) { [0]=> int(6) }
答案 2 :(得分:0)
<?php
// Set the name for your New Sub Domain
$subDomainname = $_POST['userInput'];
// cPanel Username
$cPanelUserName = "XXXXXXXX";
// cPanel Password
$cPanelPassName = "XXXXXXXXXX";
// Main Domain Name
$rootDomainName = "XXXXXXXXXXX";
// Function to create subdomain
function create_subdomain($subDomain, $cPanelUser, $cPanelPass, $rootDomain)
{
// Generate URL for access the subdomain creation in cPanel through PHP
$buildRequest = "/frontend/x3/subdomain/doadddomain.html?rootdomain=" . $rootDomain . "&domain=" . $subDomain . "&dir=public_html/subdomains/" . $subDomain;
// Open the socket
$openSocket = fsockopen('localhost', 2082);
if (!$openSocket)
{
// SHow error
return "Socket error";
exit();
}
// Login Details
$authString = $cPanelUser . ":" . $cPanelPass;
// Encrypt the Login Details
$authPass = base64_encode($authString);
// Request to Server using GET method
$buildHeaders = "GET " . $buildRequest . "\r\n";
// HTTP
$buildHeaders.= "HTTP/1.0\r\n";
// Define Host
$buildHeaders.= "Host:localhost\r\n";
// Request Authorization
$buildHeaders.= "Authorization: Basic " . $authPass . "\r\n";
$buildHeaders.= "\r\n";
// fputs
fputs($openSocket, $buildHeaders);
while (!feof($openSocket))
{
fgets($openSocket, 128);
}
fclose($openSocket);
// Return the New SUbdomain with full URL
$newDomain = "http://" . $subDomain . "." . $rootDomain . "/";
// return with Message
}
// Call the subdomain creator function
echo create_subdomain($subDomainname, $cPanelUserName, $cPanelPassName, $rootDomainName);
?>
输出:
<?php
$sequence =
[
0 => 10,
1 => 9,
3 => 8,
4 => 6,
5 => 4
];
$last = null;
foreach($sequence as $k => $v)
{
if(!is_null($last) && $last - $v > 1)
break;
$last = $v;
}
var_dump($k, $v);
答案 3 :(得分:0)
可以通过从第二个值中减去第一个值来计算序列。然后,您可以使用for
循环来遍历数组的值,并通过检查是否存在索引+ 1来检查是否还有下一个值。
然后,如果是这种情况,您可以从下一个值中减去循环中的当前值,并检查该结果是否等于步长。
如果不是这样,那么迭代的下一个值就是破坏序列的值,你可以突破循环。
$array = [10,9,8,6,4];
if (count($array) > 2) {
$step = $array[0] - $array[1];
for ($i = 0; $i < count($array); $i++) {
if (isset($array[$i + 1]) && $array[$i] - $array[$i + 1] !== $step) {
$wrongValue = $array[$i + 1];
echo sprintf(" The step count is %d, but after %d comes %d which breaks the sequence.",
$step, $array[$i], $wrongValue
);
break;
}
}
}