Asked  7 Months ago    Answers:  5   Viewed   38 times

I'm designing an electrical engineering application. However, i'm stuck on this: I have the following array

<?php 
// Static Array
$GroupOfEight = array (
                      array(0,1,3,2,4,5,7,6),
                      array(4,5,6,7,16,12,13,14),
                      array(12,13,15,14,8,9,11,10),
                      array(2,6,14,10,3,7,15,11),
                      array(1,3,5,7,13,15,9,11),
                      array(0,4,12,8,1,5,13,9),
                      array(0,1,3,2,8,9,11,10)
                      );
?>

And I have another array, but this one is one dimensional.

<?php
$myStack = array(0,1,3,2,4,5,7,6); //Dynamic, gets value by POST method.
?>

What I want to do is to check if $myStack is equal to any sub array of $GroupOfEight array. ( Number ordering is not important. The script should just check if every elements contained. It's not important if their order is same or not. )

Here is what I've done to solve the issue so far:

<?php
//Check if stackArray contains 8group
for($i=0; $i<count($GroupOfEight);$i++)
for($j=0; $j<count($GroupOfEight[$i]); $j++){
    //$containsSearch = count(array_intersect($search_this,$all)) == count($search_this);
    $containsSearch = count(array_intersect($stackArray,$GroupOfEight[$j])) == count($stackArray);
    echo $containsSearch;
}
?>

Please help me correct my code or introduce me the solution of this issue, Thanks.

EDIT: It should give only 1 index number. for example stackArray is 0,1,3,2,4,1,2,3 and it should find GroupOfEight[N] that matches the same numbers, regardless of the order of the numbers. I should get the N if there is a matching case.

 Answers

18

Given your sample arrays, the output of this will be:

> 0

In case you HAD to have only one number output, this should do that:

<?php
//Check if stackArray contains 8group
$check=false;
for($i=0; $i<count($GroupOfEight);$i++){
    //$containsSearch = count(array_intersect($search_this,$all)) == count($search_this);
    $containsSearch = (count(array_intersect($stackArray,$GroupOfEight[$i])) == count($stackArray) && count(array_intersect($stackArray,$GroupOfEight[$i])) == count($GroupOfEight[$i]));
    if($containsSearch && !$check){
        echo $i; //This specifies which index in GroupOfEight contains a matching array
        $check=true;
    }
}
?>

EDIT: Made a function. Returns first matched index or -1 for no matches:

function searcheight($stackArray,$GroupOfEight){
    for($i=0; $i<count($GroupOfEight);$i++){
        $containsSearch = (count(array_intersect($stackArray,$GroupOfEight[$i])) == count($stackArray) && count(array_intersect($stackArray,$GroupOfEight[$i])) == count($GroupOfEight[$i]));
        if($containsSearch){
            return $i; //This specifies which index in GroupOfEight contains a matching array
        }
    }
    return -1;
}
echo searcheight($stackArray,$GroupOfEight);
Wednesday, March 31, 2021
 
Semirix
answered 7 Months ago
23

I am not sure if this will be useful. I have noticed that the ArrayObject class is 'interesting'...

I am not sure that this is even an 'answer'. It is more an observation about this class.

It handles the 'multidimensional array' stuff correctly as standard.

You may be able to add methods to make it do more of what you wish?

<?php //

class Config extends ArrayObject
{

//    private $data = array();

    public function __construct(array $data = array())
    {
        parent::__construct($data);
    }
}

$conf = new Config(array('a' => 'foo', 'b' => 'bar', 'c' => array('sub' => 'baz')));
$conf['c']['sub'] = 'notbaz';
$conf['c']['sub2'] = 'notbaz2';

var_dump($conf, $conf['c'], $conf['c']['sub']);

unset($conf['c']['sub']);

var_dump('isset?: ', isset($conf['c']['sub']));

var_dump($conf, $conf['c'], $conf['c']['sub2']);

Output:

object(Config)[1]
  public 'a' => string 'foo' (length=3)
  public 'b' => string 'bar' (length=3)
  public 'c' => 
    array
      'sub' => string 'notbaz' (length=6)
      'sub2' => string 'notbaz2' (length=7)

array
  'sub' => string 'notbaz' (length=6)
  'sub2' => string 'notbaz2' (length=7)

string 'notbaz' (length=6)

string 'isset?: ' (length=8)

boolean false

object(Config)[1]
  public 'a' => string 'foo' (length=3)
  public 'b' => string 'bar' (length=3)
  public 'c' => 
    array
      'sub2' => string 'notbaz2' (length=7)

array
  'sub2' => string 'notbaz2' (length=7)

string 'notbaz2' (length=7)
Saturday, May 29, 2021
 
Jubair
answered 5 Months ago
72
a = [5, 1, 6, 14, 2, 8]
b = [2, 6, 15]

a - b
# => [5, 1, 14, 8]

b - a
# => [15]

(b - a).empty?
# => false
Wednesday, June 9, 2021
 
daiscog
answered 5 Months ago
85

You could check that the larger of the arrays outer contains every element in the smaller one, i.e. inner:

public static boolean linearIn(Integer[] outer, Integer[] inner) {

   return Arrays.asList(outer).containsAll(Arrays.asList(inner));
}

Note: Integer types are required for this approach to work. If primitives are used, then Arrays.asList will return a List containing a single element of type int[]. In that case, invoking containsAll will not check the actual content of the arrays but rather compare the primitive int array Object references.

Monday, June 14, 2021
 
letrollpoilu
answered 5 Months ago
49

Use array_uintersect() to use a custom comparison function, like this:

$arr1 = array(
           array('name' => 'asdfjkl;', 'value' => 'foo'),
           array('name' => 'qwerty', 'value' => 'bar'),
           array('name' => 'uiop', 'value' => 'baz'),
        );

$arr2 = array(
           array('name' => 'zxcv', 'value' => 'stuff'),
           array('name' => 'asdfjkl;', 'value' => 'foo'),
           array('name' => '12345', 'value' => 'junk'),
           array('name' => 'uiop', 'value' => 'baz'),
        );

$intersect = array_uintersect($arr1, $arr2, 'compareDeepValue');
print_r($intersect);

function compareDeepValue($val1, $val2)
{
   return strcmp($val1['value'], $val2['value']);
}

which yields, as you would hope:

Array
(
    [0] => Array
        (
            [name] => asdfjkl;
            [value] => foo
        )

    [2] => Array
        (
            [name] => uiop
            [value] => baz
        )

)
Wednesday, June 23, 2021
 
Baba
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :