如何将具有相同值的数组结果进行嫁接,我尝试使用array_unique但似乎对我不起作用
代码:
$row_content = mysql_fetch_assoc($sql);
$array = unserialize($row_content['content_id']);
foreach($array as $row) {
print_r($row);
}
这是结果
Array
(
[0] => v
[1] => 2040
)
Array
(
[0] => v
[1] => 526
)
Array
(
[0] => v
[1] => 200
)
Array
(
[0] => p
[1] => 2040
)
Array
(
[0] => p
[1] => 600
)
在此示例中,需要合并[1]或2040,也可以将值V或P合并在这里
编辑:
这就是我需要得到的结果
Array
(
[1] => 200
)
Array
(
[1] => 526
)
Array
(
[1] => 600
)
Array
(
[1] => 2040
)
答案 0 :(得分:0)
如果我没有误解您的要求,那么这就是您所需要的。
---
title: "test"
output:
flexdashboard::flex_dashboard:
theme: bootstrap
runtime: shiny
---
```{r setup, include=FALSE}
library(flexdashboard)
library(tidyverse)
```
```{r global, include=FALSE}
set.seed(1)
dat <- data.frame(age = sample(15:99, 100, replace=TRUE),
y = runif(100))
```
Sidebar {.sidebar}
=====================================
```{r}
# age
sliderInput("agerange", label = "Age",
min = 15,
max = 99,
value = c(15, 99),
step=10)
```
Page 1
=====================================
Column {.tabset}
-----------------------------------------------------------------------
### Plot
```{r}
dat2 <- reactive(
dat %>%
filter(age >= input$agerange[1] & age <= input$agerange[2]) %>%
mutate(y2 = y*2)
)
renderPlot({
dat2 <- dat2()
ggplot(dat2, aes(y2)) +
geom_histogram()
})
```
Column {.tabset}
-----------------------------------------------------------------------
### Table
```{r}
DT::renderDataTable({
dat2 <- dat2()
DT::datatable(dat2, options = list(bPaginate = FALSE))
})
```
答案 1 :(得分:0)
我不完全明白你想说的话。但是,如果要合并/添加“ p”和“ v”的所有值。您可以这样做:
$array = array(array('v',2040),array('v',526),array('v',200),array('v',2040),array('p',600));
$keeper = array();
foreach($array as $row){
if(array_key_exists($row[0],$keeper)){
$keeper[$row[0]] += $row[1];
}else{
$keeper[$row[0]] = $row[1];
}
}