Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.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、MYSQL、EXPLODE和数组_Php_Mysql_Arrays_Explode_Implode - Fatal编程技术网

PHP、MYSQL、EXPLODE和数组

PHP、MYSQL、EXPLODE和数组,php,mysql,arrays,explode,implode,Php,Mysql,Arrays,Explode,Implode,上面列出的结果如下: $query_posts = " SELECT DISTINCT meta_value FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_key = 'Product' AND wposts.post_status = 'publish' AND wposts.post_type = 'post' O

上面列出的结果如下:

$query_posts = "
SELECT DISTINCT meta_value
FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
WHERE wposts.ID = wpostmeta.post_id
AND wpostmeta.meta_key = 'Product'
AND wposts.post_status = 'publish'
AND wposts.post_type = 'post'
ORDER BY wposts.post_date DESC";

$result = mysql_query($query_posts) or die(mysql_error());
while($row = mysql_fetch_array($result)){
    echo $row['meta_value'];
}
如果你没猜到的话,它们是轮胎尺寸。我需要将前三个值分成不同的关系菜单:tryewidth/Sidewall/Rim

我计算出如果我使用:

235/40/R18/95/ZR(Y)/XL £180.00
225/45/R17/94/W/XL £180.00
235/45/R17/97/Y/XL £180.00
245/45/R17/99/Y/XL £180.00
245/40/R17/91/Y £180.00
225/40/R18/92/W/XL £180.00
etc etc
我可以得到第一列($tyres_widths[0])、第二列($tyres_widths[1])或第三列($tyres_widths[2]),但这些值并不明显,当然,我还没有找到它们在菜单中的关系

我的方法正确吗?一定有更好的方法吗?有人能帮忙吗。我很高兴奖励正确的答案,当然其他人也能从中受益,这就是重点

提前谢谢


Stu

另一种方法是正则表达式,但我不推荐它,因为它非常“消耗”资源,您可以做的是爬升数组并在爬升时命名数组 将函数用作first、next和end

您可以在php.net/next alt.php.net/function name中找到不同的函数行为

p、 希望这能有所帮助

$query_posts = "
SELECT DISTINCT meta_value
FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
WHERE wposts.ID = wpostmeta.post_id
AND wpostmeta.meta_key = 'Product'
AND wposts.post_status = 'publish'
AND wposts.post_type = 'post'
ORDER BY wposts.post_date DESC";
$result = mysql_query($query_posts) or die(mysql_error());
while($row = mysql_fetch_array($result)){
    $tyres_widths = (explode("/",$row['meta_value']));
    echo ($tyres_widths[0]); 
}

etc继续爬升数组,并根据您的意愿命名它们。如果没有更多内容可获取,则返回false

您的方法是正确的。请注意,您可以使用
list
来“解压缩”阵列:

$array = (explode("/",$row['meta_value']));
$tires['width']= first($array);
$tires['height']= next($array);
$tires['depth']= next($array);
名为
$weedwall
的变量比名为
$tire\u widths[1]
的变量更清晰

编辑

更整洁的是,您可以使用以下方法将字段放回关联数组中:

list($width, $sidewall, $rim) = explode("/",$row['meta_value']);
编辑

根据注释中的请求,要为特定字段创建不同值的列表,必须创建一个单独的数组并使用
array\u unique

list($tires['width'], 
     $tires['sidewall'], 
     $tires['rim'])      = explode("/",$row['meta_value']);

您可以使用“array\u unique”函数从数组中删除重复的元素。

这很有帮助,当我解包时,如何才能得到唯一/不同的结果?不同的结果是什么意思?你把每一行分开包装。变量名将对应于不同的字段。如果我回显宽度(回显$width;),则结果不明显(唯一):235 225 245 245 225 225 245 255 255 265我如何实现这一点,一直在干扰数组_unique,但没有成功。您的代码工作得很好。我离得很近,但离得还不够近,不足以让它正确!我不知道我将如何使菜单关系化。。有什么想法吗?我现在有三个独特的阵列,$ar_宽度,$ar_侧壁和$ar_边缘。托马斯,我想给你一些啤酒/咖啡钱。我可以寄到什么地址?
$result = mysql_query($query_posts) or die(mysql_error());
$ar_widths = array(); // setup array of widths

while($row = mysql_fetch_array($result)){
    list($width, $sidewall, $rim) = explode("/",$row['meta_value']);

    $ar_widths[] = $width; // remember width
}

$ar_widths = array_unique($ar_widths); // remove duplicates
print_r($ar_widths);