在 Cakephp 中使用 Set Extract 和条件创建数组
我有以下数组:
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
)
这可能吗?
I have the following array:
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
)
And I want to use CakePHP's Set:extract tools to filter this array on the 'maxweight', so all elements that have a 'maxweight' more than X and get an array made up of the 'rate' and 'svcdescription' fields ie:
Array (
[82.50] => Global Express Guaranteed Non-Document Rectangular
...
etc
)
Is this at all possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在我看来,为了从
Set::extract
中获得最大价值,最好从一个结构类似于以下的数组开始(否则我相信你必须运行 < code>Set::extract 在循环内):现在您可以使用
Set::extract()
的路径语法来提取 maxweight 大于$x 的元素
。有了这些数据,您就可以使用
Set::combine()
将费率作为键、将 svcdescription 作为值来构建您要查找的数组。In my opinion, to get the most value out of
Set::extract
, it would be better to start with an array with a structure more like the following (otherwise I believe you'd have to runSet::extract
inside a loop):Now you can use the path syntax for
Set::extract()
to extract elements that have a maxweight greater than$x
.With this data, you can build the array you're looking for using the rates as keys and svcdescription as values using
Set::combine()
.我以前从未使用过这个,但感谢您鼓励我阅读它。
您是否尝试过使用 Set::combine() ?
http://book.cakephp.org/view/662/combine
I've never used this before, but thanks for encouraging me to read up on it.
Have you tried using Set::combine() ?
http://book.cakephp.org/view/662/combine
为什么不能只使用 foreach 循环来处理数组。
Why can't you just use a foreach loop to process through the array.