how do I group this in array [closed] - php

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 1 year ago.
Improve this question
$db_string = "|1||3||1||5||1||3||8||1||10||2|2||13||2||15|";
$array = array_filter(explode("|", $db_string));
if(in_array("1", $array)){
echo"<li>
Honeymoon Packages
</li>";
}
RESULT is Coming
* Honeymoon Packages
* Honeymoon Packages
* Honeymoon Packages
* Honeymoon Packages
because in array 1 is there 4 times, how do I group this in array and print result one time without duplicate?

I do prefer splitting with a regex, so no empty values will ever occur.
$db_string = "|1||3||1||5||1||3||8||1||10||2|2||13||2||15|";
$array = preg_split('/\|/', $db_string, 0, PREG_SPLIT_NO_EMPTY);
Now the array has an element for each number in the listed order. With the help of array map we use the empty $data array and build a key for unique element. So we know how many entries are there per item.
$data = [];
array_map(function($e) use (&$data) { isset($data[$e]) ? $data[$e]++ : $data[$e] = 1; }, $array);
Array
(
[1] => 4
[3] => 2
[5] => 1
[8] => 1
[10] => 1
[2] => 3
[13] => 1
[15] => 1
)
You can use the keys to have each item uniquely.
print_r(array_unique(array_keys($data)));
Array
(
[0] => 1
[1] => 3
[2] => 5
[3] => 8
[4] => 10
[5] => 2
[6] => 13
[7] => 15
)

Related

How to filter out the array with lowest date value using SQL query [closed]

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
Ive run a query which results in an array of objects, i tried to filter out the objects in the array based on the lowest date value but i'm not able to get the required output. I tried to use min(tb2.date as Date) in the query to filter out based on lowest date with respects of each 'title', but it returns only one object with lowest date value.
The query I've used is :
SELECT
tb2.date as Date,
tb1.title as title,
tb1.descp as descp
FROM
table1 tb1
LEFT JOIN table2 tb2 ON ( tb2.id = tb1.id )
WHERE
tb1.id = x
ORDER BY
tb1.date,
tb1.status ASC
The output i got is :
Array(
[0] => Array
(
[Date] => 2020-05-05
[title] => new test
[descp] => req changes
)
[1] => Array
(
[Date] => 2020-05-06
[title] => new test
[descp] => req changes
)
[2] => Array
(
[Date] => 2020-05-08
[title] => Project
[descp] => asdasdasda
)
[3] => Array
(
[Date] => 2020-05-18
[title] => Project
[descp] => asdasdasda
)
)
The output I required is :
[0] => Array
(
[Date] => 2020-05-05
[title] => new test
[descp] => req changes
)
[1] => Array
(
[Date] => 2020-05-08
[title] => Project
[descp] => asdasdasda
)
Test
SELECT MIN(tb2.date) AS `date`,
tb1.title,
tb1.`desc`
FROM table1 tb1
LEFT JOIN table2 tb2 USING ( id )
WHERE tb1.id = x -- ??? transferred from the question text as-is
GROUP BY tb1.title, tb1.`desc`
ORDER BY `date`

How can i remove a session in an array? [closed]

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 4 years ago.
Improve this question
I need to write a product basket for my website, but I can't remove an index from array. I wrote an array index below:
Array
(
[0] => Array
(
[urun_id] => 174
[urun_adi] => ANT-5527N - Dual Polarity Parabolic Dish Anten 4.8-6.1Ghz 27Dbi
[urun_fiyati] => 672
[urun_fiyati_kur] => TL
[urun_adeti] => 1
)
[1] => Array
(
[urun_id] => 149
[urun_adi] => Cloud Router Switch 16x SFP+(10G ) 1Gbit Eth
[urun_fiyati] => 1683
[urun_fiyati_kur] => TL
[urun_adeti] => 1
)
[2] => Array
(
[urun_id] => 175
[urun_adi] => ANT-5531N - Dual Polarity Parabolic Dish Anten 4.8-6.1Ghz 31Dbi
[urun_fiyati] => 694
[urun_fiyati_kur] => TL
[urun_adeti] => 1
)
)
You can use unset for this:
unset($array[2]); removes the element with index 2

php string replace array and create new nested array [closed]

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 an array, I can print it with
print_r($show_arr);
it gives me this output(html source)
Array
(
[0] => Marvel's Daredevil.S01E01 - Into the Ring.mp4
[1] => Marvel's Daredevil.S01E02 - Cut Man.mp4
[2] => Marvel's Daredevil.S02E05 - Kinbaku.mp4
[3] => Marvel's Daredevil.S02E06 - Regrets Only.mp4
)
how would I go about getting the array to look like this?
Array
(
Season[1] => Array
(
Array(
episode => "01 - Into the Ring",
file => "Marvel's Daredevil.S01E01 - Into the Ring.mp4",
)
Array(
episode => "02 - Cut Man",
file => "Marvel's Daredevil.S01E02 - Cut Man.mp4",
)
)
Season[2] => Array
(
Array(
episode => "05 - Kinbaku",
file => "Marvel's Daredevil.S02E05 - Kinbaku.mp4",
)
Array(
episode => "06 - Regrets Only",
file => "Marvel's Daredevil.S02E06 - Regrets Only.mp4",
)
)
I was bored. Just loop your array and use preg_match() to build the array using the matched groups:
foreach($show_arr as $val) {
preg_match('/[^.]+\.S([\d]+)E([0-9]+[^.]+).*/', $val, $m);
$result['Season'][(int)$m[1]][(int)$m[2]] = array('episode' => $m[2],
'file' => $m[0]);
}
[^.]+ is 1 or more NOT dot . characters
\.S([\d]+) is a dot . then S followed by 1 or more digits (capture as group 1)
E([0-9]+[^.]+) is E followed by 1 or more digits followed by 1 or more NOT dot . characters (capture as group 2)
Additionally, this indexes the subarray by the episode. If you don't want that, remove the [(int)$m[2]] and just use [].

PHP array to stars [closed]

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 5 years ago.
Improve this question
I have array with values:
7.09, 5.63 , 5.35 , ...
I'm rounding them to 0.5 with:
round(x * 2) / 2;
Now I have 7, 6, 5.5
How can I make stars by these? Max stars 5. So I need to make 5 stars = highest array value, what's then? Sorry for my bad english. I don't know what should I do.
7 - 5 stars
6 - 4.5 stars
5.5 - 4 stars
$mArray = array(8,2,5,1);
$biggestNumber = max($mArray);
foreach($mArray as $index => $star){
$percent = $star / $biggestNumber * 100;
$mFinalArray[$index]['number'] = $star;
$mFinalArray[$index]['stars'] = $percent / 100 * 5;
}
print_r($mFinalArray);
Output :
Array
(
[0] => Array
(
[number] => 8
[stars] => 5
)
[1] => Array
(
[number] => 2
[stars] => 1.25
)
[2] => Array
(
[number] => 5
[stars] => 3.125
)
[3] => Array
(
[number] => 1
[stars] => 0.625
)
)
Also if you want round numbers, you could change the last line from
$mFinalArray[$index]['stars'] = $percent / 100 * 5;
to
$mFinalArray[$index]['stars'] = round($percent / 100 * 5);
Example output using round :
Array
(
[0] => Array
(
[number] => 8
[stars] => 5
)
[1] => Array
(
[number] => 2
[stars] => 1
)
[2] => Array
(
[number] => 5
[stars] => 3
)
[3] => Array
(
[number] => 1
[stars] => 1
)
)
If you just want to get an average of your first integers, out of 5, just do a conversion (use the max to get max value = 5 stars) :
max = 7.09
a = 7.09
b = 5.63
starsA = (a/max)* 5
starsB = (b/max)* 5
and then round these values to get your stars on that 10-point scale.

Perform arithmetic operation on specifc values of Array - PHP [closed]

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 6 years ago.
Improve this question
I have an array
Array
(
[0] => 34
[1] => 04:32 PM
[2] => 05:32 PM
[3] => MNL | ITLY
[4] => 2h 10m
[5] => PHP 9,222
[6] => 33
[7] => 04:32 PM
[8] => 04:32 PM
[9] => ITLY | MNL
[10] => 2h 10m
[11] => PHP 7,227
)
how can i perform arithmetic operation on value of index 5 and 11.
By using preg_replace you can strip the 'PHP' part and the comma from value.
echo preg_replace('| PHP (\d+),(\d+)|', '$1$2', $array[5]) + preg_replace('| PHP (\d+),(\d+)|', '$1$2', $array[11]);
or if the number of spaces is random '\s' is any whitespace char
echo preg_replace('|\s+PHP\s+(\d+),(\d+)|', '$1$2', $array[5]) + preg_replace('|\s+PHP\s+(\d+),(\d+)|', '$1$2', $array[11]);
or str_replace()

Categories