views:

229

answers:

4

i have two arrays which keys are book id( 61, 78 ,...etc)

(1) book_width
     (
        [61] => 8.3
        [72] => 8286.1
        [78] => 6.4
        [100] => 8407.0
        [102] => 0.7
     )

(2) book_height
   (
    [61] => 9.00
    [72] => 150
    [78] => 8.00
    [100] => 150
    [102] => 3.00
  )

now i want an array which contain those books id's which their height is greater than or equal to their width

means i need an array mention below

(2) book_dimension
       (     
 /*book id*/ [78] => 8.00 //along their height or width(anyone)
             [102] => 3.00
      )



 or only book id in a new array
    (2) book_dimension
           (     
            [0] => 78
            [1] => 102
      )
A: 

try this, i haven't tested it:

<?php

$book_width = array( 61 => 8.3,72 => 8286.1,78 => 6.4,100 => 8407.0,102 => 0.7);
$book_height = array(61 => 9.00, 72 => 150, 78 => 8.00, 100 => 150, 102 => 3.00);
$result = array();
foreach($book_height as $k => $v)
{
    if($v >= $book_width[$k])
    {
       $result[$k] = max($book_width[$k], $v);
    }
}

var_dump($result);

p.s. Changed the code to conform the height condition. I tested it.

Nedko
not doing anythingreturn blank array.where is the condition dear???see my question again please
diEcho
+1  A: 

This has not been tested or compiled, I am still at work from your previous question.

I added the two approaches you could take, depending on the type of array to desire.

$book_dimension = array(); //contents to be added

foreach($book_height as $heightKey => $heightValue) {
      foreach($book_width as $widthKey => $widthValue) {
          // if height is greater than or equal to width
        if ($heightValue >= $widthValue) {

          // would return: $book_dimension[78] => "8.00"
          $book_dimension[$heightKey] = $heightValue;

          // or this approach, which will return: $book_dimension[0] => 78
          $book_dimension[] = $heightKey;

        }
    }
}
Anthony Forloney
+3  A: 

I believe this should do it:

function compare_dimensions( $width, $height )
{
 return $height < $width;
}

$result = array_uintersect_assoc( $array_width, $array_height, 'compare_dimensions' );

var_dump( $result );

EDIT:
Oops: Comparing should be the other way around. (changed >= to < )

documentation: http://docs.php.net/array_uintersect_assoc

fireeyedboy
nice, I didn't know about the `callback $data_compare_func ` parameter of array\_uintersect\_assoc().
VolkerK
Honoustly: me neither ;-) I just like figuring out these kinds of puzzle questions. :-D
fireeyedboy
Very similar to `usort`, I like the flexibility of PHP where you can supply functions in args.
Anthony Forloney
Definately. As of PHP 5.3 you can even supply lambda functions and closures as arguments to these kinds of functions I believe.
fireeyedboy
A: 

Let's assume you really have to do this in php. You currently have two separate arrays, maybe you can do something about that and keep the data together (so that each element of the array describes one element/book).

$w = array(61 => 8.3, 72 => 8286.1, 78 => 6.4, 100 => 8407.0, 102 => 0.7);
$h = array(61 => 9.00,  72 => 150, 78 => 8.00,  100 => 150, 102 => 3.00);
$dimensions = array();
foreach($w as $key=>$width) {
  $dimensions[$key] = array('width'=>$width, 'height'=>$h[$key]);
}

Now having only one array you can use array_filter() to filter out all elements you do not want.

e.g. with php 5.3:

$result = array_filter($dimensions, function($x) { return $x['height']>=$x['width']; });
print_r($result);

in previous versions (that do not support this type of anonymous functions):

function heightGreateOrEqualWidth($x) {
  return $x['height']>=$x['width'];
}
$result = array_filter($dimensions, 'heightGreateOrEqualWidth');
print_r($result);
VolkerK