Foreach from explode [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 8 years ago.
Improve this question
I found problem with this:
$explode = explode($start, $data);
$abc = explode($end, $explode[1]);
$found = $abc[0] . '<br/>';
$found .= $abc[1] . '<br/>';
$found .= $abc[2] . '<br/>';
return $found;
The abc[0], abc[1] and more is randomly based by exploded results. How to define if $found is array or something I can loop it in foreach?
That's. Thank You for answer.

explode($start, $data) will return an array unless $start is an empty string "" in which case it will return false. You can confirm whether $found is an array by using is_array($found) which will return true if $found is an array and false otherwise.

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 explode row in table? [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 these row in table
K04, K84, K53, K331, L985
how i can do with array explode to get result like :
['K04','K84','K53','K331','L985']
thanks for any help...
i tried to using explode,
$ex = explode (' ,' $myRowFromTable)
$co = count ($ex);
for ($i = 0; $i < $co; $i++)
{
echo $ex[$i];
}
result only show K04
Seems like you want to convert String to JavaScript Array. Take a look for Example;
$myRowFromTable = 'K04, K84, K53, K331, L985';
// Has Array Data
$ex = explode (', ', $myRowFromTable);
// print_r($ex);
// According to User Expected Result
$newFormat = "['" . implode("', '", $ex) . "']";
echo $newFormat;
DEMO
You should try using explode as below assuming you are getting the values as a string in $myRowFromTable:
$ex = explode (',', $myRowFromTable); //no space before comma

Categories