Php 创建数组并获取第一个值
我有一个名为“manufacturer”的数据库字段,其中一些数字由管道分隔 e、 g 1572 | 906 | 1573 我想选择第一个数字并将其存储为变量 这是我可悲的努力,但没有取得成功Php 创建数组并获取第一个值,php,arrays,Php,Arrays,我有一个名为“manufacturer”的数据库字段,其中一些数字由管道分隔 e、 g 1572 | 906 | 1573 我想选择第一个数字并将其存储为变量 这是我可悲的努力,但没有取得成功 $thisproductmansarray=array(); //declare the array $thisproductmans=$myfield["manufacturer"]; //For page title / breadcrumb etc $xxarray=implode("|", $t
$thisproductmansarray=array(); //declare the array
$thisproductmans=$myfield["manufacturer"]; //For page title / breadcrumb etc
$xxarray=implode("|", $thisproductmans[$key]);
foreach($thisproductmans as $key=>$val){
$thisproductmansarray[]=$xxarray++;
echo $thisproductmansarray[0];
}
谁能给我一个指针吗。
谢谢
将显示第一个数字。在您的示例“1572”中
将显示第一个数字。在您的示例“1572”中,
看起来像是您真正想要的爆炸。explode()接受带分隔符的字符串并转换为一个部件数组
$thisproductmans=$myfield["manufacturer"]; //For page title / breadcrumb etc
$xxarray=explode("|", $thisproductmans[$key]);
if(count($xxarray) > 1)
echo $xxarray[0];
如果需要更多信息,请查看explode()的列表。看起来explode才是您真正想要的。explode()接受带分隔符的字符串并转换为一个部件数组
$thisproductmans=$myfield["manufacturer"]; //For page title / breadcrumb etc
$xxarray=explode("|", $thisproductmans[$key]);
if(count($xxarray) > 1)
echo $xxarray[0];
如果需要更多信息,请查看for explode()。在代码中使用它的示例
<?php
$var = "1572|906|1573";
$array1 = explode("|", $var);
$first_value = $array1[0];
echo $first_value; // Output here is 1572
?>
在代码中使用它的示例
<?php
$var = "1572|906|1573";
$array1 = explode("|", $var);
$first_value = $array1[0];
echo $first_value; // Output here is 1572
?>
无需使用数组即可直接获取第一个数字:-
$var = "1572|906|1573";
list($first) = explode('|', $var);
$first
现在将=1572
它和
如果您的PHP V>=5.4,则可以执行以下操作:-
$var = "1572|906|1573";
$first = explode('|', $var)[0];
var_dump($first);
.您可以直接获取第一个数字,而无需使用数组:-
$var = "1572|906|1573";
list($first) = explode('|', $var);
$first
现在将=1572
它和
如果您的PHP V>=5.4,则可以执行以下操作:-
$var = "1572|906|1573";
$first = explode('|', $var)[0];
var_dump($first);
在这种情况下,内爆()是错误的。如果数据保存在现有数组中,则是正确的。在这种情况下,内爆()是错误的。如果数据保存在现有数组中,这是正确的。这里有很多答案,但这里有另一个解释…substr($string,0,strpos($string,“|”)代码>这里有很多答案,但这里有另一个解释…substr($string,0,strpos($string,“|”)代码>