i have this situation"
foreach($front->getRequest()->getParams() as $key => $value){
if ($value == '1'){
$$key = $value;
}
}
echo $test1; // test1 = 1
echo $test2; // test2 = 1
....
this will give me back one or more $test = 1 where the $$key = $test and $value = 1
i want to see how many actually come back. and i was thinking to do something like: print_r(count($key)) or print_r(count($value)) but it doesn't tell me how many results are there
any ideas?
thanks
Why not just keep a counter?
$count = 0;
foreach($front->getRequest()->getParams() as $key => $value){
if ($value == '1'){
$$key = $value;
$count++;
}
}
echo $count;
Related
I want to get final value of $score in the code below
foreach($request->jawaban as $key => $value){
$soal = Soal::find($key);
$kunci = $soal->kunci;
$score = 0;
if($value === $kunci){
$score+=1;
echo $score;
}
}
but its produce value
123456789101112131415161718192021
how do I get 21 value only?
Based on what others have said: But just to show you the full code
$score = 0; //define this before the loop
foreach($request->jawaban as $key => $value){
$soal = Soal::find($key);
$kunci = $soal->kunci;
if($value === $kunci) $score+=1; //simplify this, as I am lazy coder.
}
echo $score; //echo the final value, after the loop finishes.
Good Luck.
You need to echo after loop. As below:
$score = 0; //define this variable before loop
foreach($request->jawaban as $key => $value){
$soal = Soal::find($key);
$kunci = $soal->kunci;
if($value === $kunci){
$score+=1;
}
}
echo $score; // echo after loop end
Hope it helps you.
Initialize $score before the loop and also print the output after loop.
$score = 0;
foreach($request->jawaban as $key => $value){
$soal = Soal::find($key);
$kunci = $soal->kunci;
if($value === $kunci){
$score+=1;
}
}
echo $score;
I have the following array:
$array = [
['2017-02-26', '2017-02-27'],
['2017-03-01'],
['2017-01-01', '2017-01-02', '2017-01-03', '2017-01-04'],
['2017-01-05', '2017-01-06', '2017-01-07']
];
I'm looking to loop into this array to have something like this:
// When several dates
From 2017-02-26 to 2017-02-27.
// When only one date
On the 2017-03-01.
What I tried:
foreach ($array as $key => $value) {
$count = count($array[$key]);
if($count==1) {
echo "On the $key[$value]";
}
else {
$first = reset($array);
$last = end($array);
echo "From ".$first." to ".$last.;
}
}
But it doesn't work when there is only one date in the row.
You are looping by foreach() so it will display last echo string .Store result to one variable Eg($display) will be more easy to display that
$display = "";
foreach ($array as $key => $value) {
$count = count($array[$key]);
if($count==1) {
$display .= "On the $value[0] <br>";
}
else {
$first = $value[0];
$last = $value[$count-1];
$display .= "From ".$first." to ".$last."<br>";
}
}
echo $display;
Try this:-
foreach ($array as $key => $value) {
$count = count($value);
if($count==1) {
echo "On the ".$value[0];
}
else {
$first = reset($value);
$last = end($value);
echo "From ".$first." to ".$last;
}
}
Or just copy paste this code, it will work. Your main inside array to play with is $value.
I have been going around in circles with a multidimensional array replace..
I need to replace numbers stored in a DB that relate to a status type.. If I did this in the view it would work but it wont seem to replace in the model?
function fetch_shipments($orgID){
$this->db->select('shipID, shipRef, shipCarrier, shipOrigin, shipDestination, shipQty, shipStatus');
$this->db->where('orgID', $orgID);
$query = $this->db->get('shipments');
$result = $query->result_array();
foreach($result as $row) {
foreach ($row as $key => $val) {
$key == 'shipStatus' && $val == 0 ? $val = 'Current' : $val;
$key == 'shipStatus' && $val == 1 ? $val = 'Pending' : $val;
$key == 'shipStatus' && $val == 2 ? $val = 'Complete' : $val;
}
}
return $result;
}
Has really left me scratching my head, I know this kind of foreach works as I use it all the time... I feel I am missing something (perhaps obvious) but I just cant put my finger on it. I even tried doing the replace at the object level before outputting an array but couldn't get that to work either.
you should save it on your $result variable. not in $val
foreach($result as $k => $row) {
foreach ($row as $key => $val) {
$key == 'shipStatus' && $val == 0 ? $result[$k][$key] = 'Current' : $val;
$key == 'shipStatus' && $val == 1 ? $result[$k][$key] = 'Pending' : $val;
$key == 'shipStatus' && $val == 2 ? $result[$k][$key] = 'Complete' : $val;
}
}
return $result;
Or you could remove the inner loop
foreach($result as $k => $row) {
if($row['shipStatus']==0){
$result[$k]['shipStatus'] = 'Current';
}elseif($row['shipStatus']==1){
$result[$k]['shipStatus'] = 'Pending';
}else{
$result[$k]['shipStatus'] = 'Complete';
}
}
return $result;
I believe placing a & before your loop variable should solve your problem, as then the reference would get updated.
So your loop would start something like this
foreach($result as &$row) {
foreach ($row as $key => &$val) {
This is really grinding my gears:
if(isset($_POST['does_what'])){
$strings = array();
foreach($_POST['does_what'] as $key => $value){
if($value[$key] == 0){
$strings[0] = "This is $value";
}
$strings[] = $value;
}
}
And this gives me error: PHP Notice: Uninitialized string offset: 0
Im trying to insert "some extra" text on first key of an array. And the other keys should your be inserted.
Use array_unshift
if(!empty($_POST['does_what']) && is_array($_POST['does_what'])){
$strings = array();
foreach($_POST['does_what'] as $key => $value){
if($value[$key] == 0){
$text = "This is ".$value
$value = array_unshift($strings[$key], $text );
}
else{
echo "Value is not a Zero";
}
$strings[] = $value;
}
}
else{
echo "Post is empty Or its not an array";
}
array_unshift Example
I think you want something more like this:
foreach($_POST as $key => $value){
if (count( $strings) == 0)
$strings[] = "This is $value";
else
$strings[] = $value;
I have several $_POST variables, they are
$_POST['item_number1']
$_POST['item_number2']
and so on
I need to write a loop tha displays the values of all the variables (I don't know how many there are). What would be a simplest way to go about it? Also what would be the simplest way if I do know how many variables I have?
This will echo all POST parameters whose names start with item_number:
foreach($_POST as $k => $v) {
if(strpos($k, 'item_number') === 0) {
echo "$k = $v";
}
}
PHP Manual: foreach(), strpos()
If you know how many do you have:
for ($i=0; $i < $num_of_vars; $i++)
echo $_POST['item_number'.$i]."<br />";
UPDATE:
If not:
foreach($_POST as $k => $v) {
$pos = strpos($k, "item_number");
if($pos === 0)
echo $v."<br />";
}
Gets all POST variables that are like "item_number"
UPD 2: Changed "==" to "===" because of piotrekkr's comment. Thanks
try:
foreach($_POST as $k => $v)
{
if(strpos($k, 'item_number') === 0)
{
echo "$k = $v";
}
}
In the above example, $k will be the array key and $v would be the value.
if you know the number of variables:
<?php
$n = 25; // the max number of variables
$name = 'item_number'; // the name of variables
for ($i = 1; $i <= $n; $i++) {
if (isset($_POST[$name . $i])) {
echo $_POST[$name . $i];
}
}
if you don't know the number:
<?php
$name = 'item_number';
foreach ($_POST as $key) {
if (strpos($key, $name) > 0) {
echo $_POST[$key];
}
}
If you must stick with those variable names like item_numberX
foreach (array_intersect_key($_POST, preg_grep('#^item_number\d+$#D', array_keys($_POST))) as $k => $v) {
echo "$k $v \n";
}
or
foreach (new RegexIterator(new ArrayIterator($_POST), '#^a\d+$#D', null, RegexIterator::USE_KEY) as $k => $v) {
echo "$k $v \n";
}
Better to use php's input variable array feature, if you can control the input names.
<input name="item_number[]">
<input name="item_number[]">
<input name="item_number[]">
then php processes it into an array for you.
print_r($_POST['item_number']);
foreach($_POST as $k => $v) {
if(preg_match("#item_number([0-9]+)#si", $k, $keyMatch)) {
$number = $keyMatch[1];
// ...
}
}
try:
while (list($key,$value) = each($_POST))
${$key} = trim($value);