Homer_J
Homer_J

Reputation: 3323

PHP Searching Array for Start and End point of values

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.

Upvotes: 1

Views: 327

Answers (4)

Zemistr
Zemistr

Reputation: 1049

$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
    )
)

Upvotes: 1

Alma Do
Alma Do

Reputation: 37365

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.

Upvotes: 1

javier_domenech
javier_domenech

Reputation: 6263

$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;

Upvotes: 0

Katch
Katch

Reputation: 180

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
    }
  }
}

Upvotes: 1

Related Questions