doushui20090526 2018-04-17 06:37
浏览 118
已采纳

提取第一个数组的子字符串以从PHP中获取第二个数组的匹配值

Given as an example:

$store = {A, B, C, D, E}

$price = {2, 10, 1, 500, 20}

Store[0]'s value is 2, [1] is 10, and so on. I'd like to know how to do that. I tried using min() but to no avail (unless I missed something out). Here's what I've done so far:

$x;

for ($x = $price.Count - 1; $x >= 0; $x--)

{

//this is the part where I can't figure it out
//compare prices here
//get the lowest price
//x = theSubstringOfTheLowestPrice

}

echo $store[x];
  • 写回答

4条回答 默认 最新

  • dscuu86620 2018-04-17 06:43
    关注
    1. Your PHP code is not valid: $price = {A} means you must have constant A and php arrays are enclosed with array() or [].

    2. Use array_combine() to create key-value pairs (demo):

    $store = ['A', 'B', 'C', 'D', 'E'];
    
    $price = [2, 10, 1, 500, 20];
    
    $range = array_combine($store, $price);
    
    var_dump($range['D']); // 500
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?