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 3 years ago.
Improve this question
I have a question about the differents between the normal array code and the shortcode.
The output from the shortcode version has all the results.
But the normal code has only the last record of the data from the database.
Please is there somebody who can explain this?
Below the shortcode
<?php
while ($data = mysqli_fetch_assoc($result)) {
$output[$data['category']][] = $data['type'];
}
?>
Below the normal code
<?php
while ($data = mysqli_fetch_assoc($result)) {
//alleen laatste database result
$output = array($data['category'] => array($data['type']));
}
?>
When you assign
$output = <anything>;
you discard all the previous contents of $output, and replace it with the value of <anything>. So each time through the loop in the second version, you replace the variable with the data from the current row. When the loop is done, it just has the data from the last row.
You don't want to replace all of $output. It should be a multi-dimensional array, where the first dimension is associative with categories as keys, and the second dimension is an array of all type values in that category. Assigning to
$output[$data['category']][]
does two things:
$output[$data['category']] creates the category key in the first dimension if it doesn't already exist.
Assigning to [] pushes a new element onto the array in the second dimension.
It's equivalent to the following long code
while ($data = mysqli_fetch_assoc($result)) {
$cat = $data['category'];
if (!isset($output[$cat])) {
$output[$cat] = array();
}
array_push($output[$cat], $data['type']);
}
Related
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 2 years ago.
Improve this question
Laravel Version:7.0
I would like to know how to create this helper.
Input is date range array. For example:
$input1 = [2020-07-19, 2020-07-25];
$input2 = [2020-07-26, 2020-08-01];
$input3 = [2020-08-01, 2020-08-07];
$input4 = [2020-10-01, 2020-10-07];
$input5 = [2020-10-19, 2020-10-25];
I would like to make one helper function.
function mergeDate($array)
{
...
}
So when I use this helper, I would like to get as following result.
$array = [$input1, $input2, $input3, $input4, $input5];
$mergedResult = mergeDate($array);
$mergedResult[0] = [2020-07-19, 2020-08-07];
$mergedResult[1] = [2020-10-01, 2020-10-07];
$mergedResult[2] = [2020-10-19, 2020-10-25];
Can anyone help me how to make mergeDate function?
Input period elements aren't overlapped.
Thank you!
This would be my first guess at how to solve it.
public function merge($array){
$results = [];
foreach ($array as $element){
if (sizeof($results) == 0){
array_push($results,$element);
}else{
$found = null;
foreach ($results as $key => $r){
if (Carbon::parse($element[0])->equalTo(Carbon::parse($r[1])))
{
$found = $key;
break;
}
}
if (!is_null($found)){
$results[$found][1] = $element[1];
}else{
array_push($results, $element);
}
}
}
return $results;
}
It is a simple take on the problem. If our resulting array is empty we add the first element otherwise we iterate over the results to find a matching pair of the elements end date and the start date of the item in the results array. If we find a matching start end pair we replace the results end value with the elements end value. Otherwise we have no overlap and we can add it as a new item to the results array.
An interesting library to use would be the Spatie/Period library.
https://github.com/spatie/period
#edit
since the array is not sorted as mentioned in a comment above, you would have to sort it prior.
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
how do i loop this kind of array?
$arr = array (
"aa"=>array("apple","orange"),
"bb"=>array("373","22"),
"cc"=>array("t0","h0"),
"dd"=>array("1","0")
);
I want to loop through the column of each item.
e.g.: i want to display ('apple','373','t0','1') at the first loop and ('orange','22','h0','0') at the last loop. thanks
We are assuming that all the arrays inside the main array are the same size in this example.
$arr = array (
"aa"=>array("apple","orange"),
"bb"=>array("373","22"),
"cc"=>array("t0","h0"),
"dd"=>array("1","0")
);
for($i = 0; $i<sizeof($arr["aa"]); $i++)
{
foreach($arr as $key=>$item)
{
echo($item[$i]);
}
echo ' - ';
}
Output: (obviously you can do any necessary formatting you need such as new lines or commas)
apple373t01 - orange22h00 -
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 3 years ago.
Improve this question
I'm using PHP and MySQL to pull some rows from a table. One column is called "item_notes" and this column has a dynamic number of comma-delimited values in it. If I printed 1 row, the column would look something like this:
item_1, new_item_1, ** note_1, ** note_2, item_2, ** note_3, old_item_1, new_item2
Is there a way I can split this into 2 arrays using PHP, where 1 array is has only values that start with a ** and the ones that don't go into the other array?
As you show no effort trying, I suppose I'll just leave it here)))
$s = 'item_1, new_item_1, ** note_1, ** note_2, item_2, ** note_3, old_item_1, new_item2';
$items = array_map('trim', explode(',', $s));
$notes = $starredNotes = [];
foreach ($items as $item) {
if (0 === strpos($item, '**')) {
$starredNotes[] = $item;
} else {
$notes[] = $item;
}
}
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 two associative arrays:
$array1=[key1=>$value1,key2=>$value2,key3=>$value3];
$array2=[key1=>$value1,key2=>$value2,key4=>$value4];
How to create the following array:
$arraytotal=[key1=>$value1,key2=>$value2,key3=>$value3,key1=>$value1,key2=>$value2,key4=>$value4];
array_merge is not an option because i have to keep the same values in the final array.
Tnx.
You can't have multiple keys set to the same value (you have key1 in each array), and as you've seen, the second key1 will override the first when you use array_merge.
Your options depend on if each original array functions as a separate group of key/value pairs, or if you want similar key/value pairs grouped together.
So, the first one is easy:
$array1=[key1=>$value1,key2=>$value2,key3=>$value3];
$array2=[key1=>$value1,key2=>$value2,key4=>$value4];
$super = [$array1, $array2];
The second needs a bit more looping:
$array1=[key1=>$value1,key2=>$value2,key3=>$value3];
$array2=[key1=>$value1,key2=>$value2,key4=>$value4];
$super = [];
foreach ($array1 as $key => $value) {
$super[$key] = [$value];
}
foreach ($array2 as $key => $value) {
if (!isset($super[$key])) {
$super[$key] = [];
}
$super[$key][] = $value;
}
If you know that you'll always have the same keys in each array, then you can skip the isset
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 7 years ago.
Improve this question
From a mySQL row I get the below from column coop as appeared (with commas)
1,2,6,27
My question is how can I have something like
for
as numbers as the column
do the loop {
{
Assuming you have the values stored in a string, let's call it $dbValue, you can split that string into an array:
$values = explode(",", $dbValue);
Then just loop over that array:
foreach ($values as $value) {
// do something with each value
}
As an aside... Storing delimited values in a single database field is very often a bad idea. Each database field should contain one discrete value. If multiple values are needed, you'd create a separate table with a many-to-one relationship.
seems foreach
$tmp = explode(',', $yourvalue) // $yourvalue = '1,2,6,27'
foreach ( $tmp as $key => $value ) {
echo $value;
}