我有以下数组:
Array
(
[0] => Array
(
[id] => 4
[rate] => 82.50
[pounds] => 2
[ounces] => 3
[mailtype] => Package
[country] => UNITED KINGDOM (GREAT BRITAIN)
[svccommitments] => 1 - 3 business days
[svcdescription] => Global Express Guaranteed (GXG)
[maxdimensions] => Max. length 46", width 35", height 46" and max. length plus girth combined 108"
[maxweight] =>30
)
[1] => Array
(
[id] => 6
[rate] => 82.50
[pounds] => 2
[ounces] => 3
[mailtype] => Package
[country] => UNITED KINGDOM (GREAT BRITAIN)
[svccommitments] => 1 - 3 business days
[svcdescription] => Global Express Guaranteed Non-Document Rectangular
[maxdimensions] => Max. length 46", width 35", height 46" and max. length plus girth combined 108"
[maxweight] => 70
)
我想使用CakePHP的Set:extract工具在'maxweight'上过滤这个数组,所以所有元素的'maxweight'都超过X并得到一个由'rate'和'svcdescription'组成的数组字段即:
Array (
[82.50] => Global Express Guaranteed Non-Document Rectangular
...
etc
)
这一切都可能吗?
答案 0 :(得分:2)
在我看来,为了从Set::extract
中获得最大价值,最好从一个结构更像下面的结构开始(否则我相信你必须运行Set::extract
在循环中):
$array = array(
'Shipping' => array(
array (
"id" => 4,
"rate" => 82.50,
"pounds" => 2,
"ounces" => 3,
"mailtype" => "Package",
"country" => "UNITED KINGDOM (GREAT BRITAIN)",
"svccommitments" => "1 - 3 business days",
"svcdescription" => "Global Express Guaranteed (GXG)",
"maxdimensions" => 'Max. length 46", width 35", height 46" and max. length plus girth combined 108"',
"maxweight" => 30
),
array (
"id" => 6,
"rate" => 82.50,
"pounds" => 2,
"ounces" => 3,
"mailtype" => "Package",
"country" => "UNITED KINGDOM (GREAT BRITAIN)",
"svccommitments" => "1 - 3 business days",
"svcdescription" => "Global Express Guaranteed Non-Document Rectangular",
"maxdimensions" => 'Max. length 46", width 35", height 46" and max. length plus girth combined 108"',
"maxweight" => 70
)
)
);
现在,您可以使用Set::extract()
的路径语法来提取maxweight大于$x
的元素。
$extracted = Set::extract($array, '/Shipping[maxweight>'.$x.']');
使用此数据,您可以使用费率作为键来构建您正在寻找的数组,并使用Set::combine()
将svcdescription作为值。
$combined = Set::combine($extracted, '{n}.Shipping.rate', '{n}.Shipping.svcdescription');
答案 1 :(得分:0)
我之前从未使用过这个,但感谢鼓励我阅读它。 您是否尝试过使用Set :: combine()?
答案 2 :(得分:0)
为什么不能只使用foreach循环来处理数组。
$returnArray = array();
// Where $x is weight amount you're testing against
foreach ($yourData as $eachData) {
if ($eachData['maxweight'] > $x) {
$returnArray[$eachData['rate']] = $eachData['svcdescription'];
}
}