开发者

comparing values of two arrays in php

开发者 https://www.devze.com 2022-12-18 05:01 出处:网络
i have two arrays which keys are book id( 61, 78 ,...etc) (1) book_width ( [61] => 8.3 [72] => 8286.1

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
      )


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


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;

        }
    }
}


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号