duangou2046 2012-03-28 19:49
浏览 11
已采纳

数组操作的想法

I have two arrays.

$Array1 = array("Maza", "Nissan","Tacoma","Cobalt","Explorer");

and second array is

array(
    (int) 0 => array(
        (int) 0 => 'In Stock',
        (int) 1 => 'Cars'
    ),
    (int) 1 => array(
        (int) 0 => 'In stock',
        (int) 1 => 'Cars/Toyota/Tacoma'
    ),
    (int) 2 => array(
        (int) 0 => 'Out of Stock',
        (int) 1 => 'Cars/Toyota/Celica'
    ),
    (int) 3 => array(
        (int) 0 => 'In Stock',
        (int) 1 => 'Cars/Ford/Fusion'
    ),
    (int) 4 => array(
        (int) 0 => 'Out of Stock',
        (int) 1 => 'Cars/Ford/Explorer'
    ),
    (int) 5 => array(
        (int) 0 => 'In Stock',
        (int) 1 => 'Cars/Chevy/Cobalt'
    ),
    (int) 6 => array(
        (int) 0 => 'In Stock',
        (int) 1 => 'Cars/Nissan'
    )

)

Now I want to see what cars in stock or not based on the first and second array. So for Cobalt, it will return me In Stock, where as for Explorer, it will return me out of stock. And for Mazda it can retun "Do not carry" Where I am confused is wheher or not to explode the array[1] by slash"/" and then lookinto it. Is there another easier/faster way to do it. thanks

  • 写回答

2条回答 默认 最新

  • douhuigan8063 2012-03-28 20:42
    关注

    Try this:

    $cars = array("Maza", "Nissan","Tacoma","Cobalt","Explorer");
    $stocks=array(
        (int) 0 => array(
            (int) 0 => 'In Stock',
            (int) 1 => 'Cars'
        ),
        (int) 1 => array(
            (int) 0 => 'In stock',
            (int) 1 => 'Cars/Toyota/Tacoma'
        ),
        (int) 2 => array(
            (int) 0 => 'Out of Stock',
            (int) 1 => 'Cars/Toyota/Celica'
        ),
        (int) 3 => array(
            (int) 0 => 'In Stock',
            (int) 1 => 'Cars/Ford/Fusion'
        ),
        (int) 4 => array(
            (int) 0 => 'Out of Stock',
            (int) 1 => 'Cars/Ford/Explorer'
        ),
        (int) 5 => array(
            (int) 0 => 'In Stock',
            (int) 1 => 'Cars/Chevy/Cobalt'
        ),
        (int) 6 => array(
            (int) 0 => 'In Stock',
            (int) 1 => 'Cars/Nissan'
        )
    
    );
    $output=array();
    foreach ($cars as $car) {
       foreach ($stocks as $stock) {
        if(in_array($car, explode('/', $stock[1]))){
            $output[$car]=$stock[0]; 
            break;
            }else{$output[$car]='Do not care';}
    } 
    }
    echo '<pre>';
    print_r($output);
    

    This will print:

    Array
    (
        [Maza] => Do not care
        [Nissan] => In Stock
        [Tacoma] => In stock
        [Cobalt] => In Stock
        [Explorer] => Out of Stock
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程