0

I have an array that looks like

    [products] => Array
        (
            [0] => stdClass Object
                (
                    [order_product_id] => 91385
                    [order_id] => 5065
                    [nid] => 2140
                    [title] => Gi Treasure
                    [manufacturer] => 
                    [model] => giftcard
                    [qty] => 5
                    [cost] => 0.00000
                    [price] => 25.00000
                    [weight] => 0
                    [data] => Array
                        (
                            [gift_description] => HJello!
                            [gift_email] => dubccom
                            [gift_sender] => Hello
                            [gift_sendDate] => 2011-10-25
                            [gift_title] => Thesure
                            [gift_card] => 2130
                            [gift_price] => 25
                            [gift_qty] => 5
                            [gift_name] => Steveek
                            [module] => uc_product
                            [cert_code] => 8-x8mfqXyUYXze
                        )

                    [order_uid] => 1
                )

            [1] => stdClass Object
                (
                    [order_product_id] => 91386
                    [order_id] => 5065
                    [nid] => 2140
                    [title] => Gift asure 2
                    [manufacturer] => 
                    [model] => giftcard
                    [qty] => 1
                    [cost] => 0.00000
                    [price] => 35.00000
                    [weight] => 0
                    [data] => Array
                        (
                            [gift_description] => Hello There!
                            [gift_email] => dubcaom
                            [gift_sender] => Hello
                            [gift_sendDate] => 2011-10-25
                            [gift_title] => The Holida
                            [gift_card] => 2134
                            [gift_price] => 35
                            [gift_qty] => 1
                            [gift_name] => Steven
                            [module] => uc_product
                            [cert_code] => 9-8xsxgDW9yrMq
                        )

                    [order_uid] => 1
                )

        )

And I want to get the data array from array of products where the order_product_id (so if it was 91385 I would get

[data] => Array
                        (
                            [gift_description] => Hello
                            [gift_email] => dubccom
                            [gift_sender] => Hello
                            [gift_sendDate] => 2011-10-25
                            [gift_title] => Thesure
                            [gift_card] => 2130
                            [gift_price] => 25
                            [gift_qty] => 5
                            [gift_name] => Steveek
                            [module] => uc_product
                            [cert_code] => 8-x8mfqXyUYXze
                        )

Any help how I could do so?

3 Answers 3

2
function search_products($id,$products)
{
    $id = intval($id);
    foreach($products as $product)
    {
        if($product->order_product_id == $id)
        {
            return($product->data);
        }
    }
    return null;
}

An educated guess at what you're looking for. Call it like search_products(91385, $products). If it returns null, it hasn't found the product ID. I also added a call to intval so if you're relying on user input for this, it'll be an int regardless. If you already sanitize it to an int, this doesn't hurt.

EDIT: Misread original post. Updated from array syntax to object property syntax.

Sign up to request clarification or add additional context in comments.

2 Comments

Just FYI, there is no need to call return null; as the last line of a function, as this is what happens implicitly.
@DaveRandom: Hmm, so it does. Regardless, it can't hurt
1
function data_by_order ($arr, $orderId) {
  foreach ($arr as $item) { // Loop the array
    if ($item->order_product_id == $orderId) { // Test this item for the right order id
      return $item->data; // Return the data array
    }
  }
  return FALSE; // Return false if we didn't find it
}

// Example usage:
$data = data_by_order($array,91385);
print_r($data);

Comments

0
$prod_data = false;
foreach ($products as $product)
{
    if ($product->order_product_id == 91385)
    {
        $prod_data = $product->data;
        break;
    }
}
if ($prod_data) {
    // knock yourself out
}

Quite similar to plain English, huh?

Comments

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.