作为数组值的函数
问题描述:
I have a function with the data I received from json and the student name and surname. I don't have any problems when there is only one student number, but if there are two students in the incoming data, the student number comes in the array. What should I do in this case?
if($adi =="Turkce")
{
$tip = "DuzMantik";
$values = array();
foreach($olan->selections as $solan){
$og_numarasi = $solan->description;
$notum = $solan->note;
$ogrneci_adi = ogrenciadial($yil,$hafta,$og_numarasi);
$values[] = array("tur"=>$og_numarasi,"tid"=>$ogrneci_adi,"not"=>$notum);
}
$array2 = array(
"tip" => $tip,
"id" => 1,
"notlar" => $values,
);
array_push($kod, $array2);
}
ogrenciadial($yil,$hafta,$og_numarasi);
ogrenciadial(1998,5,?);
我有一个函数,包含我从json收到的数据以及学生姓名和姓氏。 当只有一个学号时,我没有任何问题,但如果传入数据中有两个学生,则学生编号会出现在数组中。 在这种情况下我该怎么做? p>
if($ adi ==“Turkce”)
{
$ tip =“DuzMantik”;
$ values = array ();
foreach($ olan->选择为$ solan){
$ og_numarasi = $ solan-> description;
$ notum = $ solan-> note;
$ ogrneci_adi = ogrenciadial($ yil,$ hafta,$ og_numarasi);
$ values [] = array(“tur”=> $ og_numarasi,“tid”=> $ ogrneci_adi,“not”=> $ notum);
}
$ array2 = array(
“tip”=> $ tip,
“id”=> 1,
“notlar”=> $ values,
);
array_push($ kod,$ array2);
}
ogrenciadial($ yil,$ hafta,$ og_numarasi);
nogrenciadial(1998,5,?);
code > pre>
div>
答
You can use explode() to turn the comma delimited string into an array.
$og_numarasi = explode(',', $solan->description);
You can then loop through the $og_numarasi
array to add each as a new entry into your $values
array.
if($adi =="Turkce") {
$tip = "DuzMantik";
$values = array();
foreach($olan->selections as $solan) {
$og_numarasi = explode(',', $solan->description);
$notum = $solan->note;
foreach($og_numarasi as $number) {
$ogrneci_adi = ogrenciadial($yil,$hafta,$number);
$values[] = array("tur"=>$number,"tid"=>$ogrneci_adi,"not"=>$notum);
}
}
$array2 = array(
"tip" => $tip,
"id" => 1,
"notlar" => $values,
);
array_push($kod, $array2);
}