我在根据爆炸获取数据时遇到了一些问题。
示例,如果我有一个$cuisines_category
的数组:
string(26) "Breakfast/Cake - Accept voucher/New Restaurant""
从该数组中有2个内容数据,Breakfast/Cake
为$cuisines_name_all
,Accept voucher/New_Restaurant
为$facility_name_all
。
到目前为止,我可以返回爆炸数据,以下代码:
$split=array_map('trim', explode("-",$cuisines_category));
$cuisines_name_all = (isset($split[0]) === TRUE ? $split[0] : 0);
$facility_name_all = (isset($split[1]) === TRUE ? $split[1] : 0);
$cuisines_name = explode("/", $cuisines_name_all);
$facility_name = explode("/", $facility_name_all);
我的问题是,如果数组$cuisines_category
只为$facility_name_all
作为此数组的内容怎么样:
string(30) "Online payment available/Deals"
所以我用这段代码更改了我的爆炸代码:
$split=array_map('trim', explode("-",$cuisines_category));
$cuisines_name_all = (isset($split[0]) === TRUE ? $split[0] : 0);
if(isset($split[1])){
$facility_name_all = $split[1];
}else{
$facility_name_all = $split[0];
}
$cuisines_name = explode("/", $cuisines_name_all);
$facility_name = explode("/", $facility_name_all);
是的我可以返回$facility_name
,但$cuisines_name
与$facility_name
的内容相同$cuisines_name
应该没有任何内容
因为我需要返回$cuisines_name
和$facility_name
来从数据库中获取新数据。
如果内爆-
没有退出,你知道如何区分数据吗?
答案 0 :(得分:2)
这就是你在寻找什么?
情景1
$cuisines_category = "Breakfast/Cake - Accept voucher/New Restaurant";
$split = array_map('trim', explode("-", $cuisines_category));
$cuisines_name_all = (count($split) > 1 ) ? $split[0] : 0;
$facility_name_all = (count($split) > 1 ) ? $split[1] : $split[0];
$cuisines_name = explode("/", $cuisines_name_all);
$facility_name = explode("/", $facility_name_all);
<强>输出强>
cuisines_name: Array ( [0] => Breakfast [1] => Cake )
facility_name: Array ( [0] => Accept voucher [1] => New Restaurant )
场景2
$cuisines_category = "Accept voucher/New Restaurant";
$split = array_map('trim', explode("-", $cuisines_category));
$cuisines_name_all = (count($split) > 1 ) ? $split[0] : 0;
$facility_name_all = (count($split) > 1 ) ? $split[1] : $split[0];
$cuisines_name = explode("/", $cuisines_name_all);
$facility_name = explode("/", $facility_name_all);
<强>输出强>
cuisines_name: Array ( [0] => 0 )
facility_name: Array ( [0] => Accept voucher [1] => New Restaurant )