hi all, i'm difficult to get values array
condition:
1. if value only one get values
2. if value same then get value with index or key most high
3. if value same and key most high get all

example:

$data = Array(Array
("1" => "12306")
, Array
("0" => "12106")
,Array
("1" => "12770")
,Array
("3" => "12770")
,Array
("3" => "12306")
,Array
("3" => "12770")
);

the result: 

Array
(
[1] => Array
(
[0] => 12106
)

[3] => Array
(
[3] => 12770
)

[4] => Array
(
[3] => 12306
)

[5] => Array
(
[3] => 12770
)

)

please help me :(

Member Avatar for diafol

Your array structure is very odd and your conditions don't make sense. Try again.

@cussel try this:

<?php

$data = array(
    array("1" => "12306"),
    array("0" => "12106"),
    array("1" => "12770"),
    array("3" => "12770"),
    array("3" => "12306"),
    array("3" => "12770")
);

$newdata = array();
$result = array();
$i  = 0;


# reduce and reverse the $data array
foreach($data as $key => $value)
{
    $newdata[][current($value)] = key($value);
}

# compare $data with $values
foreach($data as $key => $value)
{
    $matches    = array();
    $currentkey     = key($value);
    $currentvalue   = current($value);

    foreach($newdata as $newkey => $newvalue)
    {
        $thiskey    = key($newvalue);
        $thisvalue  = current($newvalue);

        if(in_array($thiskey, $value) && $thiskey == $currentvalue) $matches[$thisvalue] = $thiskey;
    }

    # get max key from matches
    $maxkey = max(array_keys($matches));

    if($currentkey == $maxkey) $result[$i][$maxkey] = $matches[$maxkey];
    $i++;
}

print_r($result);

It will output your same expected result. Here there is an extended version, a bit messy but which gives a clue of what is going on: http://pastebin.com/1D3ruVya

The output of the pasted link is:

For key #1 in $data[0] with value 12306

KEY VALUE1  VALUE2  COMPARE
#1   12306   12306  =
#0   12106   n/f
#1   12770   n/f
#3   12770   n/f
#3   12306   12306  +
#3   12770   n/f
            Matches: 2 {"1":12306,"3":12306}
            Discarded
--------------------------------------------------------

For key #0 in $data[1] with value 12106

KEY VALUE1  VALUE2  COMPARE
#1   12306   n/f
#0   12106   12106  =
#1   12770   n/f
#3   12770   n/f
#3   12306   n/f
#3   12770   n/f
            Match: 1 {"0":12106}
            Chosen key: #0
--------------------------------------------------------

For key #1 in $data[2] with value 12770

KEY VALUE1  VALUE2  COMPARE
#1   12306   n/f
#0   12106   n/f
#1   12770   12770  =
#3   12770   12770  +
#3   12306   n/f
#3   12770   12770  +
            Matches: 3 {"1":12770,"3":12770}
            Discarded
--------------------------------------------------------

For key #3 in $data[3] with value 12770

KEY VALUE1  VALUE2  COMPARE
#1   12306   n/f
#0   12106   n/f
#1   12770   12770  -
#3   12770   12770  =
#3   12306   n/f
#3   12770   12770  =
            Matches: 3 {"1":12770,"3":12770}
            Chosen key: #3
--------------------------------------------------------

For key #3 in $data[4] with value 12306

KEY VALUE1  VALUE2  COMPARE
#1   12306   12306  -
#0   12106   n/f
#1   12770   n/f
#3   12770   n/f
#3   12306   12306  =
#3   12770   n/f
            Matches: 2 {"1":12306,"3":12306}
            Chosen key: #3
--------------------------------------------------------

For key #3 in $data[5] with value 12770

KEY VALUE1  VALUE2  COMPARE
#1   12306   n/f
#0   12106   n/f
#1   12770   12770  -
#3   12770   12770  =
#3   12306   n/f
#3   12770   12770  =
            Matches: 3 {"1":12770,"3":12770}
            Chosen key: #3
--------------------------------------------------------

RESULT:
Array
(
    [1] => Array
        (
            [0] => 12106
        )

    [3] => Array
        (
            [3] => 12770
        )

    [4] => Array
        (
            [3] => 12306
        )

    [5] => Array
        (
            [3] => 12770
        )

)

Now, consider that your requests are different from the example of result that you pasted. To develop my example script I've considered only the expected output, otherwise it should include also the indexes 0 and 2 which, instead, are discarded by my script. To get everything just change the last IF statement (line 41) with:

$result[$i][$maxkey] = $matches[$maxkey];

Hope it makes sense. Bye!

!!!!! How would you know what is the index for a random input. I mean if you have $data[1000][100000] = 4566, will you loop 100000 times untill you find a value ?
Please be more specific in you question!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.