I've got code:
foreach( $postData as $d => $a ) {
if( in_array( $d, $existingDomains ) ) {
foreach( $a as $p => $v ) {
echo "\t\t$d : $p = $v<br />\n";
}
}
}
and I want to add to the $existingDomains array, one value only for the purpose of this one loop. Of course I can array_push() and later unset(), but my question is if I can make it something like this:
if( in_array( $d, $existingDomains + [ 'some_value' ] ) ) { }
Instead of temporarily adding an element to your array and then running in_array. How about using an or statement instead?
if($d == 'some_value' || in_array($d,$existingDomains))
Related
How to get only 1 parameter without repeating from array?
$a = array("5WG", "5WG", "6WG", "6WG", "3WG", "2WG");
I want to return a result:
5WG
6WG
3WG
2WG
(if there is a recurring one, it will only show 1 at a time)
https://www.php.net/manual/en/function.array-unique.php
<?php
$a = array("5WG", "5WG", "6WG", "6WG", "3WG", "2WG");
$a = array_unique($a);
print_r($a);
You should php buitin funciton to remove duplicate values.
$unique_value_array = array_unique($your_array);
read more : https://www.php.net/manual/en/function.array-unique.php
also u can do with this code
$new_arr = array();
$a = array("5WG", "5WG", "6WG", "6WG", "3WG", "2WG");
foreach( $a as $k => $v ) {
if( ! in_array( $v, $new_arr ) ) {
$new_arr[] = $v;
}
}
var_dump( $new_arr );
foreach( array_combine( $ContentArray, $fileDateArray ) as $Content_Individual_Filename => $File_Individual_Date )
{
}
I want to detect duplicate date in $File_Individual_Date. if duplicate date/value then run if condition otherwise run else. for example
foreach( array_combine( $ContentArray, $fileDateArray ) as $Content_Individual_Filename => $File_Individual_Date )
{
if( $File_Individual_Date === duplicateValue )
{
}
else
{
}
}
Just use array_count_values() to create a lookup table before you run through the loop.
$a = array_combine($ContentArray, $fileDateArray);
$counts = array_count_values($a);
foreach($a as $Content_Individual_Filename => $File_Individual_Date) {
if($counts[$File_Individual_Date] > 1) { // has duplicate
} else { // unique value
}
}
I have the following if statement
if ( $missCh[0]['type']==4 OR $missCh[1]['type'] == 4 OR $missCh[2]['type'] == 4 ) {
echo 'go ahead';
}
I would like to find out if $missCh[0] or [1] or [2] is meeting the statement so that I can then ask if($missCh[X]['value]==3) but I don't know which part of the array holds true.
$types = array( $missCh[0]['type'], $missCh[1]['type'], $missCh[2]['type'] );
foreach ( $types as $key => $val ) {
if ( $val == 4 ) $fours[] = $key;
}
foreach ( $types as $key => $val ) {
if ( in_array( $key, $fours ) ) continue;
if ( $val == 3 ) $threes[] = $key;
}
print_r( $fours );
print_r( $threes );
I would like to find out if $missCh[0] or [1] or [2] is meeting the statement
AS you currently have it, you're not going to be able to manage this within the script as you currently allow the echo to be actioned if ANY of the criteria return TRUE.
If you want to action something based on what a particular array value is individually, you'll need to check each individually and action as appropriate:
if($missCh[0]['type']==4)
{
//Do something
}
elseif ($missCh[1]['type']==4)
{
//Do something
}
elseif($missCh[2]['type']==4)
{
//Do something
}
If appropriate, your final elseif could be just else, which would then be a catch all if none of the previous checks returned TRUE.
I've isolated the responsibility to check the if in a function. That function echoes "go ahead" and return the index $miss[$i]['type'] which is equals to 4.
<?php
$miss[0]['type'] = 2;
$miss[1]['type'] = 4;
$miss[2]['type'] = 5;
function goAhead($miss) {
for($i=0;$i<=count($miss);$i++) {
if($miss[$i]['type']==4) {
echo 'go ahead';
return $i;
}
}
}
$i = goAhead($miss);
echo $i;
This solution will work with one or infinite indexes or $miss array. This means that you will never need to refactor this code if $miss array will growth.
I have an issue assigning values to an array of arrays to add duplicates into the same values. Code is as following:
if( isset($rowsarray) && 0 < count($rowsarray) ) {
foreach ( $rowsarray as $rowt ) {
if( $version == $rowt[0] ) {
$rowt[1] += $success;
$rowt[2] += $failure;
$noDuplicate = false;
break;
}
}
}
if( true == $noDuplicate) {
$rowsarray[] = array($version, $success, $failure);
}
So if it's the first occurence of a version I just add it, otherwise I just increas the success and failure rate in the array. I have tried foreach ( $rowsarray as $key => $rowt ) without success as the "internal" array doesnt seem to be affected here. I am total new to php so I guess it's a rather simple solution but for now it takes only the first version access and all the other is inreased inside the loop but not outside it.
You're trying to modify a temporary array created by foreach(), which means $rowt is trashed/replaced on every iteration. You can work around it with a reference or a modified foreach:
foreach ( $rowsarray as &$rowt ) {
^----
$rowt[1] += $success;
or, less troublesome:
foreach($rowsarray as $key => $rowt) {
$rowsarray[$key][1] += $success;
You can simplify this check with:
if( isset($rowsarray) && 0 < count($rowsarray) ) {
with
if( ! empty( $rowsarray ) ){
And then,
foreach ( $rowsarray as $key => $rowt ) {
if( $version == $rowt[0] ) {
$rowsarray[$key][1] += $success;
$rowsarray[$key][2] += $failure;
I am trying to remove keys from array.
Here is what i get from printing print_r($cats);
Array
(
[0] => All > Computer errors > HTTP status codes > Internet terminology
[1] =>
Main
)
I am trying to use this to remove "Main" category from the array
function array_cleanup( $array, $todelete )
{
foreach( $array as $key )
{
if ( in_array( $key[ 'Main' ], $todelete ) )
unset( $array[ $key ] );
}
return $array;
}
$newarray = array_cleanup( $cats, array('Main') );
Just FYI I am new to PHP... obviously i see that i made mistakes, but i already tried out many things and they just don't seem to work
Main is not the element of array, its a part of a element of array
function array_cleanup( $array, $todelete )
{
$cloneArray = $array;
foreach( $cloneArray as $key => $value )
{
if (strpos($value, $todelete ) !== false)
unset( $array[ $key ] ); //$array[$key] = str_replace($toDelete, $replaceWith, $value) ; // add one more argument $replaceWith to function
}
return $array;
}
Edit:
with array
function array_cleanup( $array, $todelete )
{
foreach($todelete as $del){
$cloneArray = $array;
foreach( $cloneArray as $key => $value )
{
if (strpos($value, $del ) !== false)
unset( $array[ $key ] ); //$array[$key] = str_replace($toDelete, $replaceWith, $value) ; // add one more argument $replaceWith to function
}
}
return $array;
}
$newarray = array_cleanup( $cats, array('Category:Main') );
Wanted to note that while the accepted answer works there's a built in PHP function that handles this called array_filter. For this particular example it'd be something like:
public function arrayRemoveMain($var){
if ( strpos( $var, "Category:Main" ) !== false ) { return false; }
return true;
}
$newarray = array_filter( $cats, "arrayRemoveMain" );
Obviously there are many ways to do this, and the accepted answer may very well be the most ideal situation especially if there are a large number of $todelete options. With the built in array_filter I'm not aware of a way to pass additional parameters like $todelete so a new function would have to be created for each option.
It's easy.
$times = ['08:00' => '08:00','09:00' => '09:00','10:00' => '10:00'];
$timesReserved = ['08:00'];
$times = (function() use ($times, $timesReserved) {
foreach($timesReserved as $time){
unset($times[$time]);
}
return $times;
})();
The question is "how do I remove specific keys".. So here's an answer with array filter if you're looking to eliminate keys that contain a specific string, in our example if we want to eliminate anything that contains "alt_"
$arr = array(
"alt_1"=>"val",
"alt_2" => "val",
"good key" => "val"
);
function remove_alt($k) {
if(strpos($k, "alt_") !== false) {
# return false if there's an "alt_" (won't pass array_filter)
return false;
} else {
# all good, return true (let it pass array_filter)
return true;
}
}
$new = array_filter($arr,"remove_alt",ARRAY_FILTER_USE_KEY);
# ^ this tells the script to enable the usage of array keys!
This will return
array(""good key" => "val");