2

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?

3 Answers 3

2

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');
Sign up to request clarification or add additional context in comments.

2 Comments

Set::extract doesn't care about "preferred formats", it simply works on arrays.
its helpful though.. tnx a lot !
0

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

Comments

0

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'];
    }
}

2 Comments

Obviously I could, I was just thinking it could be done a different way.
There are probably a lot of ways to do it. If I could offer advice, don't get caught trying to overoptimize or spend too much time trying to find the perfect tool when a good-enough tool will do....well.... good enough. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.