Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have this array (much more longer - only part)
[366] => Array
(
[0] => 8
[1] => 7
[2] => 8
[3] => 9
[4] => 8
)
[367] => Array
(
[0] => 8
[1] => 9
[2] => 9
[3] => 10
[4] => 10
[5] => 9
[6] => 8
[7] => 9
[8] => 10
[9] => 10
[10] => 11
[11] => 10
[12] => 11
[13] => 11
[14] => 10
[15] => 9
[16] => 10
[17] => 8
[18] => 10
)
The keys must be hold (there are an id). I want to sum the values foreach (in this example the values are generations). So Generation 8 is count 3 times for 366.
Expect is for id 366: 7 - 1x, 8 - 3x, 9 - 1x or
[366] => Array
(
[7] => 1
[8] => 3
[9] => 1
)
[367] => Array
(
[8] => 3
[9] => 5
[10] => 8
[11] => 3
)
Every suggestion is welcomed and appreciated!
I´m always stopped with the numeric keys or the foreach ... one of code i´ve tried:
$outdams = array();
foreach ($dams as $key => $value) {
foreach ($value as $key2 => $value2) {
$index = $value2;
if (array_key_exists($index, $outdams)) {
$outdams[$index]++;
} else {
$outdams[$index] = 1;
}
}
}
check this code
<?php
$data = array(366=>array(8,7,8,8,9),
367=>array(8,9,13,14,17,14));
echo "<pre>"; print_r($data);
$response = array();
foreach ($data as $outerkey => $outerData){
foreach ($outerData as $innerkey => $innerData){
//echo "<pre>"; print_r($innerData);
if(array_key_exists($innerData, $response[$outerkey])){
$response[$outerkey][$innerData] ++;
}else{
$response[$outerkey][$innerData] = 1;
}
}
}
echo "<pre>"; print_r($response);
?>
Related
I'm actually running into an issue when trying to put this into practice. So, I have a list of goal ids and a current amount. I need to pass all these values which I am by doing goalidlist[] and current[]. The issue is when I'm running the foreach and doing the SQL Update per goalid. Here's my code below. Hope someone can point out what I'm doing wrong.
$goalidlist = $_POST['goalidlist'];
$currentnums = $_POST['current'];
if (isset($_POST['goalidlist'])) {
foreach($goalidlist as $key => $glist) {
// Update key goals
$updGoals = "UPDATE okr_3519031jkl.key_goals SET current=? WHERE goalid=? AND userid=?";
if ($stmt = mysqli_prepare($conn, $updGoals)) {
// Bind Params
mysqli_stmt_bind_param($stmt, "sii", $currentnums[$key], $glist, $userid);
mysqli_stmt_execute($stmt);
$stmt->close();
}
}
}
goalidlist[] = Array ( [0] => 4 [1] => 1 [2] => 2 [3] => 3 [4] => 5 [5] => 6 [6] => 9 [7] => 7 [8] => 8 [9] => 10 [10] => 11 [11] => 12 [12] => 13 )
current[] = Array ( [0] => 3 [1] => 1 [2] => 2 [3] => 6 [4] => 2 [5] => 12 [6] => 3 [7] => 1 [8] => 7 [9] => 9 [10] => 2 [11] => 6 [12] => 3 )
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have an array with many dates. These dates are passive. If the free time in between is more than 8 days, i need to get the opposite of these dates. I have to process these opposite dates as the number of day, start date and end date.
Example Array
Array
(
[0] => 17-05-2020
[1] => 18-05-2020
[2] => 19-05-2020
[3] => 20-05-2020
[4] => 28-05-2020
[5] => 29-05-2020
[6] => 30-05-2020
[7] => 02-06-2020
[8] => 03-06-2020
[9] => 10-07-2020
[10] => 10-07-2020
[11] => 11-07-2020
[12] => 12-07-2020
[13] => 13-07-2020
[14] => 20-07-2020
[15] => 21-07-2020
[16] => 25-07-2020
[17] => 26-07-2020
[18] => 27-07-2020
)
The result I want
Array
(
[0] => Array
(
[0] => 30-05-2020
[1] => 02-06-2020
[2] => 3
)
[1] => Array
(
[0] => 13-07-2020
[1] => 20-07-2020
[2] => 7
)
[2] => Array
(
[0] => 21-07-2020
[1] => 25-07-2020
[2] => 4
)
)
What i understood is you want to get difference between dates and if difference is >= 8 then you want those dates in separate array with days counts as well
Do like below:
$finalArray = [];
foreach($array as $key=>$arr){
if(isset($array[$key+1])){
$diff = round((strtotime($array[$key+1]) - strtotime($arr)) / (60 * 60 * 24));
if(($diff < 8) and ($diff > 1)){
$finalArray[] = array(
$arr,
$array[$key+1],
$diff
);
}
}
}
print_r($finalArray);
Output : https://3v4l.org/QBYYR
how to search array by specifying the index to start search from in php
for example:
Assuming
$needle_to_search = 5;
$array_to_search = [6,8,4,9,7,5,9,4,5,9,3,5,4,6,7];
$index_to_start_search_from = 5;
$key = array_search($needle_to_search, $array_to_search, $index_to_start_search_from);
is there any function that can implement above pseudo code
so that $key will return index: 8
what i mean is that i want to start the search from index 6 so that it returns index 8=> which has the value 5, since value 5 is my aim to search in the array.
i think you can do with array_slice.
$needle_to_search = 5;
$array_to_search = [6,8,4,9,7,5,9,4,5,9,3,5,4,6,7];
$index_to_start_search_from = 6;
// output
Array
(
[0] => 6
[1] => 8
[2] => 4
[3] => 9
[4] => 7
[5] => 5
[6] => 9
[7] => 4
[8] => 5
[9] => 9
[10] => 3
[11] => 5
[12] => 4
[13] => 6
[14] => 7
)
// return 5
echo array_search($needle_to_search, $array_to_search);
// return 8
echo array_search($needle_to_search, array_slice($array_to_search, $index_to_start_search_from, null, true));
This question already has answers here:
Count number of values in array with a given value
(8 answers)
Closed 5 years ago.
i am trying to count all multiple value in array and display in one array.
Array
(
[0] => unicomp6.unicomp.net
[1] =>
[2] => burger.letters.com
[3] =>
[4] => burger.letters.com
[5] =>
[6] => burger.letters.com
[7] =>
[8] =>
[9] => d104.aa.net
[10] =>
[11] => unicomp6.unicomp.net
[12] =>
[13] =>
[14] => unicomp6.unicomp.net
[15] =>
[16] => unicomp6.unicomp.net
[17] =>
[18] => d104.aa.net
[19] =>
[20] => d104.aa.net
[21] =>
)
output result would be like that.
Array
(
[unicomp6.unicomp.net ] => 4
[burger.letters.com ] => 3
[d104.aa.net] => 3
)
I have written this code but i want to know how to merge all the unique value in array ,please help me how can iphp do it:
$j=0;
$arrayName = array();
foreach ($host_name as $key => $value) {
$size= sizeof($host_name);
if($value!='')
{
$count=1;
for ($k=0; $k<$size; $k++) {
if($host_name[$j]==$host_name[$k])
{
$arrayName = array($host_name[$j]=> $count++);
}
}
}
$j++;
}
print_r($arrayName);
<?php
$data=Array
(
'unicomp6.unicomp.net','','burger.letters.com','','burger.letters.com','','burger.letters.com','','','d104.aa.net','unicomp6.unicomp.net','','unicomp6.unicomp.net','d104.aa.net','','d104.aa.net',''
);
echo"<pre>";
print_r(array_count_values(array_filter($data)));
echo"</pre>";
And the output after trimmed the empty fields :
Array
(
[unicomp6.unicomp.net] => 3
[burger.letters.com] => 3
[d104.aa.net] => 3
)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have an array represent by $details_sort.
I want to use my array in foreach loop.
like foreach (`$details_sort` as $detail)
{
}
Array
(
[0] => Array
(
[id] => 1
[score] => 233
[user_id] => 4
[date] => 2014-02-03 00:00:00
)
[1] => Array
(
[id] => 2
[score] => 1256
[user_id] => 5
[date] => 2014-02-05 00:00:00
)
[2] => Array
(
[id] => 4
[score] => 123
[user_id] => 7
[date] => 2014-03-04 00:00:00
)
[3] => Array
(
[id] => 3
[score] => 100
)
[4] => Array
(
[id] => 5
[score] => 8
[user_id] => 2
[date] => 2014-03-13 00:00:00
)
)
foreach($details_sort as $detail) {
foreach($detail as $attribute => $value) {
echo "$attribute => $value";
}
}
or
foreach($details_sort as $detail) {
echo $detail['id'];
echo $detail['score'];
echo $detail['userid'];
echo $detail['date'];
}
This really basic of php. you should have some efforts.
see php foreach
foreach ($details_sort as $detail)
{
echo $detail['id'];
echo $detail['score'];
echo $detail['user_id'];
echo $detail['date'];
}