dongzhenqi2015 2018-08-11 18:19
浏览 34
已采纳

PHP - 数组搜索不返回任何内容

Currently working on a project in C but I have to generate a large struct which I figured would generate in PHP since I'm more familiar with PHP.

I have 2 arrays. The first one is quite simple (is way larger than this but I assume this is enough to replicate the issue):

$vehicles = [
    'vehicleSuper' => [ "adder", "autarch", "banshee2", "bullet"  ],
    'vehicleSport' => [ "alpha", "banshee", "bestiagts", "blista2" ],
    //...
];

The second array looks like this:

$textures = [
    'candc_apartments' => [
        "limo2_b", "limo2",
    ],
    'candc_default' => [
        "marshall_flag18_b", "mesa_b", "rentbus", "marshall_flag21_b", "crusader", "boxville4", "buzzard_b", "dukes2_b", "dukes",
    ],
    'lgm_default' => [
        "hotknife", "coquette", "voltic_tless", "vacca", "infernus", "cogcabri_b", "stinger_b", "banshee_b", "ztype", "ninef", "jb700", "superd", "monroe_b", "rapidgt2_b", "khamel", "comet2_b", "cheetah_b", "rapidgt_b", "stinger", "carbon_b", "surano_convertable_b", "rapidgt2", "infernus_b", "jb700_b", "ninef_b", "stingerg", "superd_b", "bullet_b", "ztype_b", "hotknife_b", "cogcabri", "surano_convertable", "rapidgt", "stingerg_b", "coquette_b", "bullet", "carbon", "ninef2", "carboniz", "cheetah", "adder_b", "entityxf", "adder", "feltzer",
    ],
];

Now I generate the list like this using the 2 arrays above:

echo '<pre>';

foreach($vehicles as $category => $val) {
    echo "vehicleSpawner " . $category . "[] = {
";
    foreach($val as $item) {
        echo "  { \"" . $item . "\", \"" . array_search($item, array_column($textures, $item)) . "\", \"\" },
";
    }
    echo '}';
echo '<hr>';
}

This outputs something similar to:

{ "adder", "", "" },

The last 2 values are empty. What I want to achieve: Fill these out with the values from $textures array. In this case, I'm trying to fill it up like this:

// arg 1: name from $vehicles
// arg 2: key from $textures
// arg 3: val from $textures
{ "adder", "lgm_default", "adder" },

Currently my array_search($item, array_column($textures, $item)) method doesn't seem to work. How would I go and get this working? Help is appreciated, thanks all!

  • 写回答

2条回答 默认 最新

  • doutucui0133 2018-08-11 18:36
    关注

    array_column($textures, $item) will search in array $textures key named $item, but there is no key with that name so it returns false, what you need to do is loop on $textures array and search if value $item exists:

    foreach($val as $item) {
        foreach($textures as $k => $v) {
            //this will return array index or false if not exists
            $pos = array_search($item, $v);
            if ( $pos !== false )
                echo "  { \"" . $item . "\", \"" . $k . "\", \"".$v[$pos]."\" },
    ";
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮