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!

6
  • 1
    Please post some more data. and have you tried this $var as $key => $value Commented Dec 28, 2011 at 14:51
  • If you need to check if a value exists for $array2[ i ] you can use the isset directive php.net/manual/en/function.isset.php Commented Dec 28, 2011 at 14:54
  • thanks, @ThinkingMonkey. question updated. any thoughts now? Commented Dec 28, 2011 at 15:17
  • "Without going through the whole array". Impossible unless you convert to another datastructure. To convert to another datastructure you have to "go through the whole array", though. Commented Dec 28, 2011 at 15:37
  • thanks, @chris, what's the fastest/most efficient way to do this, then? Commented Dec 28, 2011 at 15:50

3 Answers 3

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.

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

1 Comment

lol, was about to update with my similar but less elegant answer. awesome, dude, thanks! and btw, is_numeric() and false are spot on.
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;
        }
    }
?>

1 Comment

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

2 Comments

hi @Jonathan, thanks, i'm sorry i just updated the question: i was trying to avoid going through the whole array. that possible?
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.

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.