我如何在 php 中即兴创作这个嵌套的 for 循环


How can I improvise this nested for loop in php?

现在我正在尝试打印一个看起来像这样的json对象

{
    "name" : "Zeninjor Enwemeka",
    "title" : "At Boston Forum, Federal And Local Officials Discuss Regions Transportation Future",
    "published" : "Friday, February 19, 2016",
    media: [
    {audio : http://foo.bar.mp4, duration : 00:04:14}
    {image : http://s3.amazonaws.com/media.wbur.org/wordpress/1/files/2015/08/0213_am-traffic02.jpg, credit : Jesse Costa/WBUR, caption : The Boston area ranks sixth for gridlock-plagued commutes in 2014. Here's morning traffic on Route 1 into Boston in February.}
    ]
},
{
    "name" : "Zeninjor Enwemeka",
    "title" : "Bostons Transportation Future? City Releases Report Detailing Publics Transit Goals",
    "published" : "Friday, February 19, 2016",
},
{
    "name" : "Ryan Caron King",
    "title" : "Sen. Murphy Seeks Feedback From Fed Up Car Commuters",
    "published" : "Friday, February 19, 2016",
},
{
    "name" : "Patrick Skahill",
    "title" : "Solar Installations Skyrocket, But Connecticut Consumers Still Need To Do Their Homework",
    "published" : "Thursday, October 15, 2015",
    media: [
    {image : http://foo.bar.jpg, credit : AP, caption : Some news story caption}
    ]
}

这是我的代码:

if (mysqli_num_rows($result) > 0) {
    $data_array = array();
    while($row = mysqli_fetch_assoc($result)) {
        //echo "Story id:" . $row["id"] . "<br>";
        $data_array[$row["id"]]['name']=ucwords($row["name"]);
        $data_array[$row["id"]]['title']=ucwords($row["title"]);
        $date=date_create($row['published']);
        $edited_date=date_format($date,"l, F d, Y");
        $data_array[$row["id"]]['published']=$edited_date; 
    }
} else {
    echo "0 results";
} 
$json_media = json_decode($media,true);
foreach($json_media as $row) {
    if ($data_array[$row['story']]['media'] == 0) {
        $data_array[$row['story']]['media'] = array();
    }
    array_push($data_array[$row['story']]['media'], $row);
}
//print_r($data_array);
echo "'n'n";
$numItems = count($data_array);
$i=0;
foreach ($data_array as $key => $stories) {
echo "{'n";
    foreach ($stories as $entity => $data) {
        if ($entity != "media") {
           echo "'t'"" . $entity."'" : '"".$data."'",'n";
        } else {
            echo "'tmedia: ['n";
            foreach($data as $media => $media_element) {
                foreach($media_element as $media_entities => $media_entity) {
                    if($media_entity == 'jpg') {
                        echo "'t{image : ".$media_element['href'].",";
                        echo " credit : ".$media_element['credit'].",";
                        echo " caption : ".$media_element['caption']."}'n";
                    }
                    else if($media_entity == 'mp4') {
                        echo "'t{audio : ".$media_element['href'].",";
                        echo " duration : ".gmdate("H:i:s",$media_element['duration'])."}'n";
                    }
                }
            }
            echo "'t]'n";
        }
    }
    if(++$i === $numItems) {
    echo "}'n'n";
    }
    else {
    echo "},'n'n";
    }
}

我觉得,我使用了太多的 for 循环,但我不确定我还能做些什么来避免在我的代码中嵌套 4 个 for 循环?PHP专家有什么建议吗?

媒体查询的外观如下:

/*$media = '[
    {
        "story": 1,
        "type": "mp4",
        "duration": 254,
        "href": "http://foo.bar.mp4"
    },
    {
        "story": 1,
        "type": "jpg",
        "caption": "The Boston area ranks sixth for gridlock-plagued commutes in 2014. Here''s morning traffic on Route 1 into Boston in February.",
        "credit": "Jesse Costa/WBUR",
        "href": "http://s3.amazonaws.com/media.wbur.org/wordpress/1/files/2015/08/0213_am-traffic02.jpg"
    },
    {
        "story": 4,
        "type": "jpg",
        "caption": "Some news story caption",
        "credit": "AP",
        "href": "http://foo.bar.jpg"
    }

这应该得到相同的结果:

$json_media = json_decode($media,true);
$data_array = array();
if (mysqli_num_rows($result) > 0) {
    while($row = mysqli_fetch_assoc($result)) {
        //echo "Story id:" . $row["id"] . "<br>";
        $data_array[$row["id"]]['name']=ucwords($row["name"]);
        $data_array[$row["id"]]['title']=ucwords($row["title"]);
        $date=date_create($row['published']);
        $edited_date=date_format($date,"l, F d, Y");
        $data_array[$row["id"]]['published']=$edited_date;
   }
} else {
    echo "0 results";
}
foreach($json_media as $row) {
    $id = $row['story'];
    if ( ! isset( $data_array[ $id ]['media'] ) ) {
        $data_array[ $id ]['media'] = array();
    }
    $type = ( $row['type'] == 'jpg' ) ? 'image': 'audio';
    // for more types, you could use a switch() here
    $row[ $type ] = $row['href'];
    if ( isset( $row['duration'] ) ) {
        $row['duration'] = gmdate("H:i:s", $row['duration'] );
    }
    // remove all not needed data
    unset( $row['story'], $row['type'], $row['href'] );
    array_push( $data_array[ $id ]['media'], $row );
}
echo json_encode( $data_array );

基本上,我正在改进/设置媒体数据,因为它被添加到$data_array中,这样,您就不必再次循环访问所有内容:)