当 PHP 数组有共同前缀时,我想将它们分开。
$data = ['status.1', 'status.2', 'status.3',
'country.244', 'country.24', 'country.845',
'pm.4', 'pm.9', 'pm.6'];
Run Code Online (Sandbox Code Playgroud)
我希望它们每个都在单独的变量中,例如$status, $countries, $pms将包含:
$status = [1,2,3];
$country = [244, 24, 845]
$pms = [4,9,6]
Run Code Online (Sandbox Code Playgroud)
我当前的代码需要 1.5 秒才能对它们进行分组:
$statuses = [];
$countries = [];
$pms = [];
$start = microtime(true);
foreach($data as $item){
if(strpos($item, 'status.') !== false){
$statuses[]= substr($item,7);
}
if(strpos($item, 'country.') !== false){
$countries[]= substr($item,8);
}
if(strpos($item, 'pm.') !== false){
$pms[]= substr($item,3);
}
}
$time_elapsed_secs = microtime(true) - $start;
print_r($time_elapsed_secs);
Run Code Online (Sandbox Code Playgroud)
我想知道是否有更快的方法来做到这一点
我在 C++ 代码中看到以下内容:
vector<vector<string>> arr(n);
Run Code Online (Sandbox Code Playgroud)
我无法理解如何使用它......
谁能解释它是什么以及如何使用 var arr?
这是我用于从数据库中删除行的代码:
<?php
$eid = $_GET['eid'];
$con = mysqli_connect("localhost", "root", "","project") or die("Connection failed");
echo "connection is done";
$query = "delete from exam where eid='$eid'";
if ($con->query($query)==TRUE)
{
echo " record deleted";
}
else
{
echo "Error: " . $query . "<br>" . $con->error;
}
$con->close();
?>
Run Code Online (Sandbox Code Playgroud)
else 语句没有被执行。即使在数据库中找不到该值,它也会为每个值显示“记录已删除”。
为什么会这样?如何验证我的记录已从我的数据库中删除?
我有2个这样的数组:
数组一:
[
[
"id" => 1234
"name" => "John"
],
[
"id" => 1235
"name" => "doe"
]
]
Run Code Online (Sandbox Code Playgroud)
数组二:
[
[
"age" => 12
],
[
"age" => 13
]
]
Run Code Online (Sandbox Code Playgroud)
我已经尝试使用它,并结合array_reduce,array_map或者array_merge_recursive,但它仍然没有结果相符。
我如何使两个数组如下所示?
[
[
"id" => 1234
"name" => "John"
"age" => 12
],
[
"id" => 1235
"name" => "doe"
"age" => 13
]
]
Run Code Online (Sandbox Code Playgroud)
请帮我。谢谢
我想计算所有可能的子集形式数组的总和。
$array= Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 6 ); //changing
function powerSet($array) {
// add the empty set
$results = array(array());
foreach ($array as $element) {
foreach ($results as $combination) {
$results[] = array_merge(array($element), $combination);
$total= array_sum($results); // I try this
}
echo $total; // I try this
}
return $results;
}
Run Code Online (Sandbox Code Playgroud)
上面的代码用于查找子集。我从这里找到了这段代码。我只添加array_sum但显示0的方式如何找到每个子集的总和?有什么办法吗?
我有两个数组:
$array1 = array("red", "blue", "green", "yellow");
$array2 = array("one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten");
Run Code Online (Sandbox Code Playgroud)
我想随机回显一种颜色的10倍。
我尝试使用foreachloop和进行操作shuffle,但尝试此操作时出现错误:
数组到字符串的转换.....
这是我的代码:
shuffle($array1);
foreach($array2 as $array2) {
echo $array1;
}
Run Code Online (Sandbox Code Playgroud)
请有人能帮我解决这个问题吗?