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 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成