在 Cakephp 中使用 Set Extract 和条件创建数组

发布于 2024-09-06 09:58:16 字数 1345 浏览 2 评论 0原文

我有以下数组:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

倾城泪 2024-09-13 09:58:16

在我看来,为了从 Set::extract 中获得最大价值,最好从一个结构类似于以下的数组开始(否则我相信你必须运行 < code>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');

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 run Set::extract inside a loop):

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

Now you can use the path syntax for Set::extract() to extract elements that have a maxweight greater than $x.

$extracted = Set::extract($array, '/Shipping[maxweight>'.$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().

$combined = Set::combine($extracted, '{n}.Shipping.rate', '{n}.Shipping.svcdescription');
静水深流 2024-09-13 09:58:16

我以前从未使用过这个,但感谢您鼓励我阅读它。
您是否尝试过使用 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

ι不睡觉的鱼゛ 2024-09-13 09:58:16

为什么不能只使用 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'];
    }
}

Why can't you just use a foreach loop to process through the array.

$returnArray = array();

// Where $x is weight amount you're testing against
foreach ($yourData as $eachData) {
    if ($eachData['maxweight'] > $x) {
        $returnArray[$eachData['rate']] = $eachData['svcdescription'];
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文