用作元素的数组值

时间:2019-06-11 08:18:37

标签: php arrays json

我有一个函数,该函数具有从json接收的数据以及学生姓名和姓氏。当只有一个学生编号时,我没有任何问题,但是如果传入数据中有两个学生,则学生编号进入数组。在这种情况下我该怎么办?

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,?);

1 个答案:

答案 0 :(得分:0)

您可以使用explode()将以逗号分隔的字符串转换为数组。

$og_numarasi = explode(',', $solan->description);

然后,您可以遍历$og_numarasi数组,将它们作为新条目添加到$values数组中。

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);
}