split string by order like 3x2x1 [closed] - php

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
(PHP) how can I split a string in multiple strings separated by comma,
like is I have a string "Hello my neighbor",
then after the split process, it should be
("Hello my neighbor","Hello my","Hello neighbor","my neighbor","Hello","my","neighbor")

try this :
<?php
$string = "Hello my neighbor";
$str = explode(" ",$string);
$newArray = [];
$newArray[] = $string;
for($i = 0; $i<count($str); $i++) {
if($i <= 1) {
$newArray[] = $str[$i]." ".$str[$i+1];
if($i != 0) {
$newArray[] = $str[$i-1]." ".$str[$i+1];
}
}
$newArray[] = $str[$i];
}
echo "<pre>";
print_r($newArray);
echo "</pre>";

Related

how to explode a string using a foreach [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 last year.
Improve this question
I got this string
$string = '1:23:health,2:24:mana';
$v = explode(",", $string);
foreach($v as $data)
{
echo $data[0][1];
}
What im looking is to making a "multidimensional string" by exploding ','
Wanna do this:
data[0][0] = 1;
data[0][1] = 23;
data[0][2] = "health";
How can i do this in PHP?
$string = '1:23:health,2:24:mana';
$result = [];
foreach (explode(',', $string) as $step) {
$result[] = explode(':', $step);
}
print_r($result);
An alternative is:
$string = '1:23:health,2:24:mana';
$result = array_chunk(preg_split('/[:,]/', $string), 3);
print_r($result);

Duplicate words in PHP [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 4 years ago.
Improve this question
I have probably information about whether it is available in PHP to detect a duplicate and a book about not removing it and adding it to -1, -2, -3
Example:
$string = 'H4,H4,H3,WY21W,W5W,W5W,WY21W,W21/5W,W21/5W,W21W,W16W,W5W';
Result:
$output = 'H4,H4-1,H3,WY21W,W5W,W5W-1,WY21W-1,W21/5W,W21/5W-1,W21W,W16W,W5W-2'
$string = 'H4,H4,H3,WY21W,W5W,W5W,WY21W,W21/5W,W21/5W,W21W,W16W,W5W';
$parts = explode(',', $string); // split by comma
$used = []; // array to count the number of occurrences
$result = []; // array to take the "new" elements
foreach($parts as $part) {
if(isset($used[$part])) { // if there is an entry in the counter array already,
// increment it by one,
$used[$part]++;
}
else { // else initialize with 0
$used[$part] = 0;
}
// put part into new array, append -(counter) if counter > 0
$result[] = $part . ($used[$part] ? '-'.$used[$part] : '');
}
echo implode(',', $result); // join together with comma
Pretty much the same as misorude's answer.
<?php
$string = 'H4,H4,H3,WY21W,W5W,W5W,WY21W,W21/5W,W21/5W,W21W,W16W,W5W';
$values = explode(',', $string);
foreach($values as $k => $value)
if($counts[$value] = !isset($counts[$value]) ? 0 : $counts[$value]-1)
$values[$k] = $value . $counts[$value];
print implode(',', $values);
Output:
H4,H4-1,H3,WY21W,W5W,W5W-1,WY21W-1,W21/5W,W21/5W-1,W21W,W16W,W5W-2

How can I get result as array (one, two, three, one, two, three, one, two, three, one) [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 7 years ago.
Improve this question
I have two arrays.
$a = array("one","two","three");
$b = array ( 0,1,2,3,4,5,6,7,8,9);
Look at the user contributed example in http://www.php.net/manual/en/class.infiniteiterator.php
$obj = new stdClass();
$obj->Mon = "Monday";
$obj->Tue = "Tuesday";
$obj->Wed = "Wednesday";
$obj->Thu = "Thursday";
$obj->Fri = "Friday";
$obj->Sat = "Saturday";
$obj->Sun = "Sunday";
$infinate = new InfiniteIterator(new ArrayIterator($obj));
foreach (new LimitIterator($infinate, 0, 14) as $value ) {
print($value . PHP_EOL);
}
You could create a mapping function and use array_map as follows:
function numToName($num) {
return array("zero","one","two","three","four","five",
"six","seven","eight","nine")[$num];
}
$b = array (0,8,2,3,7,5,6,0,4,1);
$result = array_map("numToName", $b);
print_r($result);

foreach - return the highest value [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 8 years ago.
Improve this question
I have a foreach loop in php.
Loop will return 4 diffirent values and i want to display the highest from it.
Specifically, my loop will return array with date and temperature for that day.
The example code for indication:
foreach ($variable as $key => $value) {
$temperature = temprature();
$date = date();
$teploty[$date] = $teplota;
if(!isset($teploty[$date]) > -50) {
$teploty[$date] = $teplota;
}
}
Your code is confusing. This is how you find the highest value in an array of numbers:
$highest = null;
foreach ($numbers as $num) {
if (is_null($highest) || $num > $highest) {
$highest = $num;
}
}
You should be able to adapt this pattern to your code and data.

How to Adding values of two dimnetional array in 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 8 years ago.
Improve this question
I have the below given array
Array
(
[0] => 2,3
[1] => 2,3
)
I want to sum up and get result in a variable. E.g. variable a = 4 (2+2=4) and variable b = 6 (3+3=6). I am coding in php.
Use EXPLODE inside foreach
<?php
$yourArr = array('2,3','2,3');
$a = 0;
$b =0;
foreach ($yourArr as $temp)
{
$tempnew = explode(",",$temp);
$a += $tempnew[0];
$b += $tempnew[1];
}
echo "a = ".$a."<br>";
echo "b = ".$b;
?>
Try this
$a = 0;
$b =0;
foreach ($yourArr as $temp)
{
$a += $temp[0];
$b += $temp[1];
}
echo "a = ".$a."<br>";
echo "b = ".$b;
Output
a = 4
b = 6

Categories