dongmaqiu6084 2014-10-21 22:54
浏览 29

搜索数组并获得结果

Okay, So I am wanting to find information in an array and get a block returned based on the credentials passed. The way I am doing it right now is not working, I'm looking for a shorter process and a more fool proof process.

Right now I have this:

public function get_product($product_id, $color, $size)
{
    $results = $this->pf->get('products/'.$product_id);

    $vars = $results['variants'];

    $details = array();

    foreach($vars as $var)
    {
        if(!in_array($product_id, $details))
        {
            if($var['product_id'] == $product_id)
            {
                if($var['size'] == $size)
                {
                    if($var['color'] == $color)
                    {
                        $details[$var['id']] = array(
                            'id' => $var['id'],
                            'name' => $var['name'],
                            'image' => $var['image'],
                            'price' => $var['price'],
                        );
                    }
                }
            }
        }
    }

    return $details;
}

This receives a product_id, a color, and a size. Sometimes $color is null, Sometimes $size is null, and sometimes both $color and $size are null and we just need to find the one array that matches the $product_id.

What I am wanting returned is this:

$details[$var['id']] = array(
     'id' => $var['id'],
     'name' => $var['name'],
     'image' => $var['image'],
     'price' => $var['price'],
);

Right now nothing gets returned. $results returns this for an example: (This is what I need to search.)

{
"code": 200,
"result": {
    "product": {
        "id": 1,
        "type": "POSTER",
        "brand": null,
        "model": "Poster",
        "image": "https://d1yg28hrivmbqm.cloudfront.net/products/poster_18x24.jpg",
        "variant_count": 9,
        "files": [
            {
                "id": "default",
                "title": "Print file",
                "additional_price": null
            },
            {
                "id": "preview",
                "title": "Mockup",
                "additional_price": null
            }
        ],
        "options": []
    },
    "variants": [
        {
            "id": 4464,
            "product_id": 1,
            "name": "Poster 12×12",
            "size": "12×12",
            "color": null,
            "color_code": null,
            "image": "https://d1yg28hrivmbqm.cloudfront.net/products/1/4464.jpg",
            "price": "9.00"
        },

Notice how color is returned as null. size can be that way to. So basically I am wanting a quicker and better way to search the returned array for the specified product_id, size, and color. So I need returned and matching the corresponding variants block that matches the variables submitted.

I hope I've made sense of what I'm trying to accomplish.

UPDATE

This is what I am needing.

So on my site the customers chooses a product, in this case a poster. Before adding it to the cart they are prompted to select a size. Let's say a 12x12. The way the API works is that it has a "top" item and then has smaller items "variants" that include the size and color. Each variant is a poster with a different size. The only way to obtain the poster product, is by receiving every variant for the poster. But each "variant" has a different "id" to send to the api to order the correct product.

So, I receive the product and it's variants in bulk or every color and size as it's own variant.

"variants": [
        {
            "id": 4464,
            "product_id": 1,
            "name": "Poster 12×12",
            "size": "12×12",
            "color": null,
            "color_code": null,
            "image": "https://d1yg28hrivmbqm.cloudfront.net/products/1/4464.jpg",
            "price": "9.00"
        },
        {
            "id": 1349,
            "product_id": 1,
            "name": "Poster 12×16",
            "size": "12×16",
            "color": null,
            "color_code": null,
            "image": "https://d1yg28hrivmbqm.cloudfront.net/products/1/1349.jpg",
            "price": "11.00"
        },

But remember the customer wanted a poster that was 12x12? We only need to send the demand to print a 12x12 poster. So we need to send to the api the ID for the variant that matches the 12x12 size.

I need a way to search through each variant for a product and find the correct variant that matches the product_id of the poster, and the size requirements of 12x12.

        {
            "id": 4464,
            "product_id": 1,
            "name": "Poster 12×12",
            "size": "12×12",
            "color": null,
            "color_code": null,
            "image": "https://d1yg28hrivmbqm.cloudfront.net/products/1/4464.jpg",
            "price": "9.00"
        },

Once I find that correct variant, I need to collect all that information into a new array and return it.

//Get the product based on the supplied product_id. ($results)
//Break that array down into just the variants. ($vars)
//Search the $vars array for a block that matches the product_id.
//Search those $vars blocks for a single one that matches the size. 
//If color is supplied, search those $vars blocks for a single one that matches the color.

//If size and color are supplied, a single block should be returned that matches all three variables (product_id, size, and color). Sometimes size and/or color is `null`. But a product_id is always supplied.

I hope the clears up what I am needing a little better.

  • 写回答

1条回答 默认 最新

  • dongzhuo3376 2014-10-22 04:55
    关注

    Try this. Am also ssuming you are using php. If you have a question, asking me directly. I think I can help you but I don't know exactly what you want.

    <?php
    function get_product($object){
        $result = json_decode($object);
    
        $product_id = $result->result->product->id;
        $variants = $result->result->variants;
    
        $details = array();
    
        foreach($variants as $variant):
            $details[] = $variant;
        endforeach;// foreach
    
        return $details;
    }
    
    $json_obj = '{
        "code": 200,
        "result": {
            "product": {
                "id": 1,
                "type": "POSTER",
                "brand": null,
                "model": "Poster",
                "image": "https://d1yg28hrivmbqm.cloudfront.net/products/poster_18x24.jpg",
                "variant_count": 9,
                "files": [
                    {
                        "id": "default",
                        "title": "Print file",
                        "additional_price": null
                    },
                    {
                        "id": "preview",
                        "title": "Mockup",
                        "additional_price": null
                    }
                ],
                "options": []
            },
            "variants": [
                {
                    "id": 4464,
                    "product_id": 1,
                    "name": "Poster 12×12",
                    "size": "12×12",
                    "color": null,
                    "color_code": null,
                    "image": "https://d1yg28hrivmbqm.cloudfront.net/products/1/4464.jpg",
                    "price": "9.00"
                }
            ]
        }
    }';
    
    $array = json_decode($json_obj);
    echo '<pre>';
    print_r(get_product($json_obj));
    echo '</pre>';
    
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向