Assign value to double array - php

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;

Related

How to search if the key value in an array exists in another array, using PHP?

I need a help. I have two arrays. I need to check if the values in first array are present in second or not. The arrays are as:
$maindata=array(array('id'=>3),array('id'=>7),array('id'=>9));
$childata=array(array('id'=>7),array('id'=>11),array('id'=>3),array('id'=>123));
Here, I need to check if each value from first array is present inside second array or not. If yes, then should return true else false at each time.
Here you go, you can use the in_array() for PHP.
$maindata=array( array('id'=>3),array('id'=>7),array('id'=>9) );
$childata=array( array('id'=>7),array('id'=>11),array('id'=>3),array('id'=>123) );
foreach( $maindata as $key => $value )
{
if( in_array( $value, $childata ) )
{
echo true;
}
else
{
echo false;
}
}
You could also remove the whole if else and replace with a single line.
echo ( in_array( $value, $childata ) ? true : false );
Reference -
http://php.net/manual/en/function.in-array.php
https://code.tutsplus.com/tutorials/the-ternary-operator-in-php--cms-24010
To check if an array contains a value:
if (in_array($value, $array)) {
// ... logic here
}
To check if an array contains a certain key:
if (array_key_exists($key, $array)) {
// ... logic here
}
Resources
in_array - PHP Manual
array_key_exists() - PHP Manual
Following code will return true only if all elements of main array exists in second array, false otherwise:
$maindata=array(array('id'=>3),array('id'=>7),array('id'=>9));
$childata=array(array('id'=>3),array('id'=>7),array('id'=>11),array('id'=>123));
$match = 0;
foreach( $maindata as $key => $value ) {
if( in_array( $value, $childata ) ) {
$match++;
}
}
if($match == count($maindata)){
// return true;
} else {
// return false;
}
Use array_intersect
if(!empty(array_intersect($childata, $maindata)))
{
//do something
}
or
$result = count(array_intersect($childata, $maindata)) == count($childata);
Use array_column and array_intersect.
$first = array_column($maindata, 'id');
$second = array_column($childata, 'id');
//If intersect done, means column are common
if (count(array_intersect($first, $second)) > 0) {
echo "Value present from maindata in childata array.";
}
else {
echo "No values are common.";
}

Recursively loop through a PHP array to manipulate data

I'm trying to loop through a PHP array but I only ever get back my original data. I think it has something to do with when I break the loop
$newData = $this->seperateKeyValuePairs($data);
private function seperateKeyValuePairs($array)
{
foreach($array as $key => $item)
{
if( is_array($item) ) $this->seperateKeyValuePairs($item);
if( is_string($key) && $this->stringStartsWith($key, 'is_') ) {
$item = $this->makeBoolean($item);
}
}
return $array;
}
I think the problem is on this line:
$item = $this->makeBoolean($item);
You change the value of item. Item is not a pointer to the value in the array, but a copy of it, so the value in the array remains unchanged. What you want to do instead is this:
$array[$key] = $this->makeBoolean($item);
In the same spirit, you have to change
if( is_array($item) ) $this->seperateKeyValuePairs($item);
to
if( is_array($item) ) $array[$key] = $this->seperateKeyValuePairs($item);

PHP, add temporary only value into array

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))

detect duplicate value in for each loop php

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
}
}

how to find which if statement is met

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.

Categories