0

I have spent this afternoon trying to solve this question:

How can I check if the {from, to} elements in this array are the same? In words: I need to know how to match an array elements in a recursive function.

Example

This array must return FALSE because the $array[4][0]['from'] and $array[4][0]['to'] is NOT the same in all the $array[2] and $array[3].

Array
(
    [4] => Array
        (
            [0] => Array
                (
                    [from] => 1
                    [to] => 2.0000
                    [price] => 8.0000
                )

            [1] => Array
                (
                    [from] => 2.0000
                    [to] => 4.0000
                    [price] => 6.0000
                )

        )

    [2] => Array
        (
            [0] => Array
                (
                    [from] => 1
                    [to] => 3.0000
                    [price] => 70.0000
                )

            [1] => Array
                (
                    [from] => 3.0000
                    [to] => 5.0000
                    [price] => 60.0000
                )

            [2] => Array
                (
                    [from] => 5.0000
                    [to] => 9.0000
                    [price] => 50.0000
                )

        )

    [3] => Array
        (
            [0] => Array
                (
                    [from] => 1
                    [to] => 2.0000
                    [price] => 25.0000
                )

            [1] => Array
                (
                    [from] => 2.0000
                    [to] => 4.0000
                    [price] => 20.0000
                )

            [2] => Array
                (
                    [from] => 4.0000
                    [to] => 6.0000
                    [price] => 15.0000
                )

        )

)

This array must return TRUE because the $array[4][0]['from'] and $array[4][0]['to'] is the same in all the $array[2] and $array[3].

Array
(
    [4] => Array
        (
            [0] => Array
                (
                    [from] => 1
                    [to] => 3.0000
                    [price] => 7.0000
                )

            [1] => Array
                (
                    [from] => 3.0000
                    [to] => 5.0000
                    [price] => 6.0000
                )

            [2] => Array
                (
                    [from] => 5.0000
                    [to] => 9.0000
                    [price] => 5.0000
                )

        )

    [2] => Array
        (
            [0] => Array
                (
                    [from] => 1
                    [to] => 3.0000
                    [price] => 70.0000
                )

            [1] => Array
                (
                    [from] => 3.0000
                    [to] => 5.0000
                    [price] => 60.0000
                )

            [2] => Array
                (
                    [from] => 5.0000
                    [to] => 9.0000
                    [price] => 50.0000
                )

        )

    [3] => Array
        (
            [0] => Array
                (
                    [from] => 1
                    [to] => 3.0000
                    [price] => 170.0000
                )

            [1] => Array
                (
                    [from] => 3.0000
                    [to] => 5.0000
                    [price] => 160.0000
                )

            [2] => Array
                (
                    [from] => 5.0000
                    [to] => 9.0000
                    [price] => 150.0000
                )

        )

)

I would like to get as result just a True or False value.

1
  • Perhaps try wording your question a different way. I don't really understand what it is you are looking for... Commented Jan 2, 2012 at 20:15

2 Answers 2

2

try this (not tested, and it is definitely not the best way to do it) :

$match = true;

foreach ($main_array as $arrays) 
{
    foreach($arrays as $key => $array)
    {
         $from = $array['from'];
         $to = $array['to'];
         foreach($main_array as $tmp_array)
         {          
             if($tmp_array[$key]['from'] != $from || $tmp_array[$key]['to'] != $to) {               
                $match = false;
                break 3;
             }
         }
    }
}

return $match;
Sign up to request clarification or add additional context in comments.

3 Comments

I just tested this and it works, let me know if it works for you.
Hi @redmoon7777 I have tried your code and seems to me that it doesn't work, mybe I wrong something. Can you tell me where? codepad.org/bYA9b3gG thanks
your mistake is on line 18 it should be foreach($ranges as $tmp_array) not foreach($arrays as $tmp_array)
1

Here my example, I have tested. Here you can compare array of arrays by customizable fields and customizable $depth

Main function:

function array_compare(array $haystack, array $to = array(), array &$matches = array(), $depth = 1)
{
    $total = 0;
    if ($depth === 1) // We're in right depth, let's check the elements
    {
        foreach ($haystack as $key => $value)
        {
            $match = true;
            foreach ($to as $to_key => $to_value) // Check every key
            {
                if (!isset($value[$to_key]) || $value[$to_key] !== $to_value)
                {
                    $match = false; // If at least one doesn't match - break and block adding to $matches
                    break;
                }               
            }
            if ($match)
            {
                $matches[]  = $value; // OK, match, let's add to matches.
            }
            $total++; // Increase total searched elements
        }
    }
    else // We're not on the right depth level
    {
        foreach ($haystack as $key => $value)
        {
            $total += array_compare($value, $to, $matches, $depth - 1); // let's gather into
        }
    }

    return $total; // return total searched
}

And here how can you use it:

$test = array(
    0   => array(
        0   => array(
            'from'      => 1,
            'to'        => 3,
            'value'     => 25,
        ),
        1   => array(
            'from'      => 1,
            'to'        => 3,
            'value'     => 25,
        )
    ), 
    1   => array(
        0   => array(
            'from'      => 2,
            'to'        => 5,
            'value'     => 25,
        ),
        1   => array(
            'from'      => 1,
            'to'        => 3,
            'value'     => 25,
        )
    ),
    2   => array(
        1   => array(
            'from'      => 1,
            'to'        => 15,
            'value'     => 25,
        )
    ),
);

$matches    = array();
$total = array_compare($test, array('from' => 1, 'to' => 3), $matches, 2);
var_dump($matches, $total);

Your true must be in case if count($matches) === $total;

Hope that is exact what you need;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.