1

I have an array as follows:

Array (
[0] => Array (
  ...
)
[41] => Array (
  [name] => London 
  [company] => nhyt6t
  [top25_1] => 8.75912088
)
[42] => Array (
  [name] => Manchester
  [company] => gtr4rf
  [top25_1] => 6.56758398
)
[43] => Array (
  [name] => Leeds
  [company] => de3wsd6
  [top25_1] => 7.58675398
)
[44] => Array (
  [name] => Liverpool
  [company] => fe4rf56
  [top25_2] => 4.5697965
)
)

Is it possible (within PHP) to search the array and bring back the Array Index for the Start and End of top25_1 (Note: they are always grouped in a sequence) - so in this instance:

$start = 41;
$end = 43;

The position of top25_1 varies each time the array is generated, hence the need to search.

Any advice, feedback and assistance welcomed.

5
  • Check to see if key exists, see php.net/manual/en/function.array-key-exists.php Commented Jul 4, 2014 at 12:25
  • Will they always be grouped in a sequence, or could a different key like top25_99 interrupt the sequence? Commented Jul 4, 2014 at 12:27
  • @MichaelBerkowski always grouped in a sequence. Commented Jul 4, 2014 at 12:28
  • @dwhite.me - looking at that it merely returns True is the key exists, I need the Index Value returned. Commented Jul 4, 2014 at 12:30
  • @Airoude has what I would have put as an answer. Commented Jul 4, 2014 at 12:31

4 Answers 4

1

You may do it with:

$result = array_reduce(array_keys($array), function($c, $x) use ($array)
{
   if(!isset($c[$key = end(array_keys($array[$x]))]))
   {
      $c[$key] = array('start'=>$x, 'end'=>$x);
   }
   else
   {
      $c[$key]['end'] = $x;
   };
   return $c;
}, array());

So, result of statement above would be array, which has your top_XX as keys and ['start'=>Y, 'end'=>Z] as values, where Y and Z are corresponding groups starts and ends. For example, if input array is:

$array = array(
    41 => Array (
      'name' => 'London ',
      'company' => 'nhyt6t',
      'top25_1' => 8.75912088
    ),
    42 => Array (
      'name' => 'Manchester',
      'company' => 'gtr4rf',
      'top25_1' => 6.56758398
    ),
    43 => Array (
      'name' => 'Leeds',
      'company' => 'de3wsd6',
      'top25_1' => 7.58675398
    ),
    44 => Array (
      'name' => '--- ',
      'company' => 'nhyt6t',
      'top25_2' => 1
    ),
    45 => Array (
      'name' => '---',
      'company' => 'gtr4rf',
      'top25_2' => 1
    ),
    46 => Array (
      'name' => '??',
      'company' => 'de3wsd6',
      'top25_3' => 7.58675398
    )
);

Then result would be:

array(3) {
  ["top25_1"]=>
  array(2) {
    ["start"]=>
    int(41)
    ["end"]=>
    int(43)
  }
  ["top25_2"]=>
  array(2) {
    ["start"]=>
    int(44)
    ["end"]=>
    int(45)
  }
  ["top25_3"]=>
  array(2) {
    ["start"]=>
    int(46)
    ["end"]=>
    int(46)
  }
}

That means you can easily get desired key start and end with $result[$key] (so, $result['top25_1'] for your question)

Please, note that statement above relies on fact, that your elements are grouped - thus, is they're not - then result may be unexpected.

Sign up to request clarification or add additional context in comments.

2 Comments

That looks like it is exactly what I am after, however, your code stops mine from working and I cannot see why!
You mean it doesn't work? It may be short arrays syntax (so. [..]) - you may replace it to array(..) if you're on PHP 5.3
1

Loop through your array and check if the array contains your key, like so:

$start = null;
$end = 0;
foreach($arrays as $k => $arr) {
  if (array_key_exists('top25_1', $arr)) {
    if ($start === null) { // only update with the first key
       $start = $k; // 41
    }

    if ($k > $end) { // if the array key is bigger than the last end, update.
       $k = $end; // 43
    }
  }
}

4 Comments

But the // some code here is what is important to get the first and last. That's what the OP needs.
@Airoude - this is fine but it's pulling out the Array Index Value of the first and last time that top25_1 appears.
So you want the value of top25_1?
@Airoude - nope. As stated in the question. I need the Array Index Value of top25_1 when it starts (i.e. first appears in the Array) and where it ends (i.e. the last time it appears).
1
$start = null;
$end = null;

while(list($index, $item) = each($array)) {
    if(isset($item['top25_1'])) {
        if($start === null) {
            $start = $index;
        }

        $end = $index;
    }
}

Description:

Array (
    [0] => Array (
      ... // do nothing
    )
    [41] => Array (
        [name] => London 
      [company] => nhyt6t
      [top25_1] => 8.75912088 // set start and end
    )
    [42] => Array (
        [name] => Manchester
      [company] => gtr4rf
      [top25_1] => 6.56758398 // update end
    )
    [43] => Array (
        [name] => Leeds
      [company] => de3wsd6
      [top25_1] => 7.58675398 // update end
    )
    [44] => Array (
        [name] => Liverpool
      [company] => fe4rf56
      [top25_2] => 4.5697965 // do nothing
    )
)

4 Comments

Care to explain this?
Why? :D After all, it's simple. ;)
StackOverflow exists to help people improve, giving someone code without explanation might help this person but not necessarily the next person to come along and read it. This answer was flagged as low quality because of this.
Much better, take a look at the answer from @AlmaDo on how you could improve it even more.
0
$init = -1;
$end = -1;
for($i=0;$i<count($array);$i++) {
  if ($init == -1 and array_key_exists('top25_1', $array[$i])) {
    $init=$i;

  }
  if($init>=0 and !array_key_exists('top25_1', $array[$i]))
  {
     $end=$i - 1;
     break;
  }

}
if($init>=0 and $end==-1)
   $end = count($array) -1;

4 Comments

and what if the first or last top25_1 is at 0?
@vivoconunxino - Thanks for this - however, for some reason your code is causing mine to stop working.
FYI - PHP Syntax Check: Parse error: syntax error, unexpected ')', expecting ';' in your code on line 3 for($i=0;$i<count($array),$i++) {
well... easy to fix, here you go

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.