1

I have two same-sized arrays $array1 and $array2, both with the usual consecutive numerical keys. $array1 contains numbers, $array2 contains text. I cannot change this structure to accommodate multi-dimensional arrays or what.

Without going through the whole array, how do I get the keys i of the elements in $array2 where

  1. $array1[i] is a number; BUT
  2. $array2[i] is empty?

For example:

// numbers
$array1 = array(NAN, NAN, 1, 0, 3.5, NAN, 2, 4, 0.5);

// text
$array2 = array(FALSE, FALSE, "abc", "abc", FALSE, FALSE, "text", "abc", FALSE);

expected result:

// keys of $array2 where $array1[i] is a number and
// $array2[i] is empty/null/false

Array
(
    [0] => 4
    [1] => 8
)

I've been trying to implement array_keys() and array_udiff() and other PHP array functions to do this but I just can't.

Help, guys, thanks!

Ana Ban
  • 1,395
  • 4
  • 21
  • 29

3 Answers3

2

This will perform in linear O(n) time.

$keys = array();
foreach ($array1 as $i => $v1) {
    if (is_numeric($v1) && !$array2[$i])
        $keys[] = $i;
}

is_numeric() accepts a little more than what most people consider "numbers", but if that's a problem just replace with another function.

I also assumed your definition of "empty" is a value that php would convert to boolean false. Again, adjust as necessary.

goat
  • 31,486
  • 7
  • 73
  • 96
  • lol, was about to update with my similar but less elegant answer. awesome, dude, thanks! and btw, `is_numeric()` and `false` are spot on. – Ana Ban Dec 28 '11 at 16:03
0

i'd do something like this:

<?php
    $keys = array();
    foreach($array1 as $iterator){
        $key = array_search($iterator,$array1);
        $elem_in_array1 = $array1[$key]; //= $iterator
        $elem_in_array2 = $array2[$key];
        if(is_numeric($elem_in_array1) && emtpy($elem_in_array2)){
            $keys[] = $key;
        }
    }
?>
Andreu Ramos
  • 2,858
  • 2
  • 25
  • 36
  • thanks @Andreu, i'm so sorry, i should've included that i was trying to avoid going through the whole array. but if there's no other way, i'll definitely give this a spin. any thoughts? thanks man – Ana Ban Dec 28 '11 at 15:01
0

This will grab the first value in $arr1 that matches $value, as well as the corresponding value in $arr2 at the index of the value found in $arr1.

<?php

function array_magic ($value, $arr1, $arr2) {
    $index = array_search($value, $arr1);

    if ($index === false) {
        return false;
    }

    if(!isset($arr2[$index])) {
        $value2 = null;
    } else {
        $value2 = $arr2[$index];
    }

    return array( $value, $value2 );
}
Jonathan Rich
  • 1,740
  • 10
  • 11
  • hi @Jonathan, thanks, i'm sorry i just updated the question: i was trying to avoid going through the whole array. that possible? – Ana Ban Dec 28 '11 at 15:10
  • 1
    I noticed your update and have updated my response in turn. array_search still iterates through the array, but it does so in C so it's faster than a foreach. – Jonathan Rich Dec 28 '11 at 15:11