English 中文(简体)
A. 比较实验室两种阵列的数值
原标题:comparing values of two arrays in php

i 有两个阵列,主要有书本(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
  )

现在需要一阵列,其中含有其高度大于或等于其宽度的id本。

手段一:

(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
      )
最佳回答

我认为应该这样做:

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

文件:

问题回答

这一点没有经过测试或编辑,我在上个问题的工作上是still/em>。

我补充说,根据希望的阵列类型,你可以采取两种做法。

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

        }
    }
}

让我假设,你确实必须在营地这样做。 你们目前有两个不同的阵列,或许你们可以做一些事情,把数据放在一起(因此,各阵列的每一部分都描述了一个要素/手册)。

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

现在只有一阵列,你才能使用array_filter(。 检查你不想要的所有内容。

e.g. with php 5.3:

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

在以前的版本中(不支持这种匿名职能):

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

a. 试验:

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

页: 1 我对此进行了测试。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...