2 PHP arrays for merge - php

How to delete element from array as it has partial in another?
Other words, I need to compare 2 array likely in_array php function, but partially.
Code:
$a = ['abc', 'cde', 'efg', 'hi'];
$b = ['bce', 'ifg', 'hig'];
// return $b without 'hig' because in $a has partially 'hi'
Thanks very much for any solution!

If you want to make any string in $b not have any string in $a as a substring. demo
<?
$a = ['abc', 'cde', 'efg', 'hi'];
$b = ['bce', 'ifg', 'hig'];
$b = array_filter($b, function($v) use($a){
foreach($a as $str){
if(strpos($v, $str) !== false)
return false;
}
return true;
});
print_r($b);

You can use array_filter() to filter the array with anonymous function. Then a combination of array_diff() and str_split() to compare each letters of each elements. This works even if the letters on the element of $a array is shuffled
$a = ['abc', 'cde', 'efg', 'hi'];
$b = ['bce', 'ifg', 'hig'];
$b = array_filter($b, function($bElem) use($a) {
foreach ($a as $aElem) {
$diff = array_diff(
str_split($aElem),
str_split($bElem)
);
if (empty($diff)) return false;
}
return true;
});
print_r($b);

As I understood, yuo want to compare start of string from $b with full string from $a
$b = array_udiff($b, $a,
function($x, $y) { return strcmp(substr($x, 0, strlen($y)), $y); });
demo

Loop through the array and look for match. If doesn't match any elements of the array, then push the value.
$a = ['abc', 'cde', 'efg', 'hi'];
$b = ['bce', 'ifg', 'hig'];
$result = $a;
foreach ($b as $bs) {
$flag = true;
foreach ($a as $as) {
if (strpos($bs, $as) !== false) {
$flag = false;
}
}
if($flag==true)
array_push($result, $bs);
}
return $result;

Related

How to check multidimensional arrays for the same key pairs ie.exactly same keys but values may differ?

I have made this solution, this is working fine for the normal arrays but failed for multidimensional arrays.
$a = ['a'=>11, 'b'=>2,'c'=>['r'=>5,'s'=>8]];
$b = ['a'=>1, 'b'=>2, 'c'=>['r'=>15,'s'=>18]];
function array_equal($a, $b) {
return (
is_array($a)
&& is_array($b)
&& count($a) == count($b)
&& array_diff_key($a, $b) === array_diff_key($b, $a)
);
}
$c = array_equal($a,$b);
echo $c;
For the Above set of arrays it is working fine.
But for the below arrays it returns 1 even if keys are different.
$a = ['a'=>11, 'b'=>2,'c'=>['r'=>5,'s'=>8]];
$b = ['a'=>1, 'b'=>2, 'c'=>['r1'=>15,'m'=>18]];
This would work if the array keys are in the same order:
https://3v4l.org/jDmON
<?php
function array_keys_recursive(array $array) : array
{
foreach ($array as $key => $value) {
if (is_array($value)) {
$index[$key] = array_keys_recursive($value);
} else {
$index []= $key;
}
}
return $index ?? [];
}
$a = ['a'=>11, 'b'=>2,'c'=>['r'=>5,'s'=>8]];
$b = ['a'=>1, 'b'=>2, 'c'=>['r'=>15,'s'=>18]];
var_dump(array_keys_recursive($a) === array_keys_recursive($b)); // true
$a = ['a'=>11, 'b'=>2,'c'=>['r'=>5,'s'=>8]];
$b = ['a'=>1, 'c'=>2, 'b'=>['r'=>15,'s'=>18]];
var_dump(array_keys_recursive($a) === array_keys_recursive($b)); // false
$a = ['a'=>11, 'b'=>2,'c'=>['r'=>5,'s'=>8]];
$b = ['a'=>1, 'b'=>2, 'c'=>['r1'=>15,'m'=>18]];
var_dump(array_keys_recursive($a) === array_keys_recursive($b)); // false
This should work -
function array_equal($a, $b) {
// count mismatch -> not equal
if (count($a) != count($b)) {
return false;
}
foreach ($a as $key => $val) {
// key not present -> not equal
if (empty($b[$key])) {
return false;
}
// check for inner arrays
if (is_array($val)) {
return array_equal($val, $b[$key]);
}
}
return true;
}
array_equal($a, $b); // true for first arrays
$a = ['a'=>11, 'b'=>2,'c'=>['r'=>5,'s'=>8]];
$b = ['a'=>1, 'b'=>2, 'c'=>['r1'=>15,'m'=>18]];
array_equal($a, $b); // false
I'm reading your question, as that you want to check for the same key structure, but don't care about the values.
I've cheated here by changing all the leaf values to null for both arrays, and then you can compare the leftovers.
<?php
$a = ['a'=>11, 'b'=>2, 'c'=> ['r'=>5, 's'=>8 ]];
$b = ['a'=>1, 'b'=>2, 'c'=> ['r'=>15,'s'=>18]];
function arrays_have_same_keys(array $a, array $b) {
array_walk_recursive($a, function(&$v) {
$v = null;
});
array_walk_recursive($b, function(&$v) {
$v = null;
});
return $a==$b;
}
var_dump(arrays_have_same_keys($a, $b));
Output:
bool(true)

PHP use result of as variable

Is it possible to use the result of an if with an OR statement as a variable for a function?
As example:
$a = true;
$b = false;
if ($a || $b) {
$this->functionCall($a)
}
Other example:
$a = false;
$b = true;
if ($a || $b) {
$this->functionCall($b)
}
Third and final exmaple:
$a = true;
$b = true;
if ($a || $b) {
$this->functionCall($a, $b)
}
So I need to detect what variable is true and pass it as a paramater. Is this even possible?
Any helps is appreciated!
Many thanks in advance
I'd do the logic bit inside a two-parameter function if I were you, as such :
function myFunc($a = false, $b = false) {
if ($a == true)
echo 'a';
if ($b == true)
echo 'b';
}
myFunc(); // echoes nothing
$a = true;
$b = false;
myFunc($a, $b); // echoes 'a'
$a = false;
$b = true;
myFunc($a, $b); // echoes 'b'
$a = true;
$b = true;
myFunc($a, $b); // echoes 'ab'
PHP 5.6+ version, filter out the falsely values (you can pass a callback to array_filter for different checks) and use those with the splat operator.
$params = array_filter([$a, $b]);
$this->callFunction(...$params);
No need for any IF checks and confusing in IF assignments.
Explore Variadic functions and Argument unpacking.

PHP How to add two list of decimal together

public function Encrypt($message)
{
$character = str_split($message);
$encrypted = '';
foreach ($character as $character)
{
$encrypted .= (ord($character). '.');
}
return $encrypted;
}
I use that code to generate ASCII numbers. Example of the result that I generated
$a = 1.2.4.3.4.3
$b = 1.4.3.2.4.3
Then I want both together (1+1,2+4,4+3,3+2,4+4,3+3) then the result is
$c = 2.6.7.5.8.6
Is it possible to do that ? Can anyone help me please.
It's definitely possible:
$a = '1.2.4.3.4.3';
$b = '1.4.3.2.4.3';
$result = join('.', array_map(
function($a, $b) { return $a + $b; },
explode('.', $a),
explode('.', $b)
));
var_dump($result);
Explanation:
split by .
summarize
join back
Ideone: http://ideone.com/uzBVed
Perhaps you could use a function like this?
function add_number_strings($a, $b) {
$a_arr = explode('.', $a);
$b_arr = explode('.', $b);
$c_arr = array();
for ($i=0; $i<count($a_arr); $i++) {
$c_arr[] = $a_arr[$i] + $b_arr[$i];
}
return implode('.', $c_arr);
}
// Testing
$a = '1.12.9.4.3.2.1';
$b = '2.3.2.4.3.2.1';
$c = add_number_strings($a, $b);
var_dump($c); // should be 3.15.11.8.6.4.2

Php, in_array with no exactly match

I want to do the following:
$a = array();
$a[] = array(1,2);
$a[] = array(2,5);
$a[] = array(3,4);
var_dump (in_array(array(2,5), $a));
this returns OK, as it expected, but if the source array is not fully matched:
$a = array();
$a[] = array(1,2, 'f' => array());
$a[] = array(2,5, 'f' => array());
$a[] = array(3,4, 'f' => array());
var_dump (in_array(array(2,5), $a));
it returns false. Is there a way to do it with the built-in way, or I have to code it?
in_array() is just not the thing that you should use for this issue. Because it will compare values with type casting, if that's needed. Instead, you may use plain loop or something like:
function in_array_array(array $what, array $where)
{
return count(array_filter($where, function($x) use ($what)
{
return $x===$what;
}))>0;
}
So then
var_dump(in_array_array(array(2, 5), $a)); //true
$needle = array(2, 5);
$found = array_reduce($a, function ($found, array $array) use ($needle) {
return $found || !array_diff($needle, $array);
});
This does an actual test of whether the needle is a subset of an array.
function subset_in_array(array $needle, array $haystack) {
return array_reduce($haystack, function ($found, array $array) use ($needle) {
return $found || !array_diff($needle, $array);
});
}
if (subset_in_array(array(2, 5), $a)) ...

PHP merging arrays uniquely

So i'm working on a small php applications that combines four arrays.
Now there is a possibility that some of the possible arrays will be null.
I tried the following solution to merge the four arrays uniquely.
<?php
$a = [1,2,3,4,5];
$b = null;
$c = [5,4,3,2,1];
$d = [1,2];
$new_array;
if(is_array($a) && is_array($b) && is_array($c) && is_array($d))
{
$new_array = array_unique(array_merge($a,$b,$c,$d));
}else if(is_array($a) && is_array($b) && is_array($c))
{
$new_array = array_unique(array_merge($a,$b,$c));
}else if(is_array($a) && is_array($b))
{
$new_array = array_unique(array_merge($a,$b));
}else{
$new_array = $a;
}
print_r($new_array);
?>
I soon realized my code was highly dysfunctional in that it does not cater for all possible combinations of arrays while excluding the null variables.
How do I solve this. Ensuring that all the variables that are arrays are merged a nd those that are not are discarded.
Thanks
how about this? putting all the array's in an array, so you can loop through them all easily, and use a custom in_array() function to check if they are already existing?
The good thing about this way is that it doesn't limit the script to just four array's, as it will loop through all the array's you get together.
$a = [1,2,3,4,5];
$b = null;
$c = [5,4,3,2,1];
$d = [1,2];
$array_stack = array($a, $b, $c, $d);
$new_array = array();
foreach($array_stack as $stack){
if(!is_array($stack)) continue;
foreach($stack as $value){
if(!in_array($value, $new_array)) $new_array[] = $value;
}
}
print_r($new_array);
maybe something like this :
<?php
$a = [1,2,3,4,5];
$b = null;
$c = [5,4,3,2,1];
$d = [1,2];
$new_array;
if(is_array($a)) $new_array = $a;
if(is_array($b)) $new_array = array_unique(array_merge($new_array,$b));
if(is_array($c)) $new_array = array_unique(array_merge($new_array,$c));
if(is_array($d)) $new_array = array_unique(array_merge($new_array,$d));
?>
Old question, but going to give my input anyways. A more universal approach:
function multiple_array_merge() {
$args = func_get_args();
$array = [];
foreach ($args as $i) {
if (is_array($i)) $array = array_merge($array, $i);
}
return array_unique($array);
}
$a = [1, 2, 3, 4, 5];
$b = null;
$c = [5, 4, 3, 2, 1];
$d = [1, 2];
$merged = multiple_array_merge($a, $b, $c, $d);

Categories