Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php中将数组信息转换为语句?_Php_Arrays_Xhtml_Arraylist - Fatal编程技术网

如何在php中将数组信息转换为语句?

如何在php中将数组信息转换为语句?,php,arrays,xhtml,arraylist,Php,Arrays,Xhtml,Arraylist,我在一个网站上工作,假设比较产品。因此,我已到达以下数组 Array ( [iPhone 4 8GB Black] => 319 [iPhone 4S] => 449 [iphone 5] => 529 ) 数组的键是product name,数组的值是price。现在我想把这个数组转换成如下语句 iPhone4 8GB黑色是最便宜的 iPhone48GB黑色比iPhone4S便宜130英镑(计算:449-319) iPhone48GB黑色比iPhone5便宜210英镑(计

我在一个网站上工作,假设比较产品。因此,我已到达以下数组

Array ( [iPhone 4 8GB Black] => 319 [iPhone 4S] => 449 [iphone 5] => 529 ) 
数组的键是product name,数组的值是price。现在我想把这个数组转换成如下语句

iPhone4 8GB黑色是最便宜的

iPhone48GB黑色比iPhone4S便宜130英镑(计算:449-319)

iPhone48GB黑色比iPhone5便宜210英镑(计算:529-319)

iPhone4S比iPhone5便宜80英镑(计算:529-449)

iPhone5是您选择的列表中最昂贵的产品


请帮助我如何从数组中输出这些语句。在比较方面,您建议对该数组执行其他操作也将非常有用。谢谢。

首先,您必须使用
asort
对数组进行排序(以保持索引和值之间的关联,并根据值进行排序)

然后,在对数组进行排序时,可以隔离price和name

$names = array_keys($yourArray);
$prices = array_values($yourArray);
此时,您有两个包含标签和价格的数字索引数组,这两个数组是同步的

最后,您只需从0循环到数组的长度(其中一个,大小相同),并使流程:

for($i = 0 ; $i < count($names) ; $i++)
{
    if ($i == 0)
    {
        // First product -> cheapest
        echo "The product " . $names[$i] . " is cheapest";
    }
    else if ($i == (count($names) - 1))
    {
        // Last product, the most expensive
        echo "The product " . $names[$i] . " is the most expensive product of the list";
    }
    else
    {
        // calculate the diff between current product and first product
        $diff = $price[$i] - $price[0];
        echo "The product " . $names[$i] . " is " . $diff . " more expensive than " . $names[0];
    }
}
for($i=0;$i最便宜
echo“产品”。$names[$i]。“是最便宜的”;
}
else if($i==(计数($name)-1))
{
//最后的产品,最贵的
echo“产品”。$names[$i]。“是列表中最昂贵的产品”;
}
其他的
{
//计算当前产品和第一个产品之间的差异
$diff=$price[$i]-$price[0];
echo“产品”。$names[$i]”比“$names[0]”贵“$diff.”;
}
}
此示例与第一个产品进行了所有比较

如果您需要所有的组合,它会稍微复杂一些,您必须创建一个双循环:

// Hard print the first product
echo "The product " . $names[0] . " is the cheapest";

// Make all possible comparisions
for($j = 0 ; $j < (count($names) - 1) ; $j++)
{
    for($i = ($j+1) ; $i < count($names) ; $i++)
    {
        // calculate the diff between current product and first product
        $diff = $price[$i] - $price[$j];
        echo "The product " . $names[$i] . " is " . $diff . " more expensive than " . $names[$j];
    }
}

// Hard print the last product
echo "The product " . $name[count($names) - 1] . " is the more expensive";
//硬打印第一个产品
呼应“产品”$名称[0]。“是最便宜的”;
//做所有可能的比较
对于($j=0;$j<(计数($name)-1);$j++)
{
对于($i=($j+1);$i
我还没有尝试过任何东西。到目前为止,我只能输出:iphone 4 8GB价格319。iPhone4S使用foreach循环的成本为449等等。这是一个简单的
for
循环和一些
if
,我还认为学习PHP应该是一个很好的练习,我建议您尝试单独做,请查阅
for
if
的PHP文档。输出将有
和(1..l-1)+2行,它是
(l-1)*(l-2)/2+2
,其中
l
是数组的长度。因此,对于10种产品列表,这将是38行。你确定你想这样做吗?是的,我想我想这样做。没有其他方法可以比较这样的数组,是吗?谢谢!我在找这样的东西。你太棒了!!事实上,我的解决方案通过一个简单的循环将所有产品与第一个产品进行比较。如果您想进行所有比较组合,只需将此循环包装在父循环(2级循环)中,并稍微更改测试部分。祝你好运,非常感谢你。我真的很感谢你的帮助
// Hard print the first product
echo "The product " . $names[0] . " is the cheapest";

// Make all possible comparisions
for($j = 0 ; $j < (count($names) - 1) ; $j++)
{
    for($i = ($j+1) ; $i < count($names) ; $i++)
    {
        // calculate the diff between current product and first product
        $diff = $price[$i] - $price[$j];
        echo "The product " . $names[$i] . " is " . $diff . " more expensive than " . $names[$j];
    }
}

// Hard print the last product
echo "The product " . $name[count($names) - 1] . " is the more expensive";