doujiang2641 2016-10-10 04:30
浏览 44
已采纳

从数据库结果中填充PHP数组

I need construct an array like this:

$data = array(
            array(1 => array("<span class=\"spanBig\">A</span> ROW GREENS", array(
                "A1" => "http://flexslider.woothemes.com/images/kitchen_adventurer_caramel.jpg",
                "A2" => "http://flexslider.woothemes.com/images/kitchen_adventurer_cheesecake_brownie.jpg",
                "A3" => "http://flexslider.woothemes.com/images/kitchen_adventurer_lemon.jpg"
            ))),
            array(2 => array("<span class=\"spanBig\">B</span> ROW BLUE",array(
                "B1" => "http://flexslider.woothemes.com/images/kitchen_adventurer_donut.jpg",
                "B2" => "http://flexslider.woothemes.com/images/kitchen_adventurer_caramel.jpg",
                "B3" => "http://flexslider.woothemes.com/images/kitchen_adventurer_cheesecake_brownie.jpg"
        ))));

Ok,I have manually this inital array:

$data = array(
            array(3 => array("<span class=\"spanBig\">A</span> ROW GREENS", array())),
            array(4 => array("<span class=\"spanBig\">B</span> ROW LIGHTS", array())),
            array(5 => array("<span class=\"spanBig\">C</span> ROW GOLD/BROWN", array())),
            array(6 => array("<span class=\"spanBig\">D</span> ROW GOLD/BROWN", array())),
            array(7 => array("<span class=\"spanBig\">E</span> ROW DARK", array())),
            array(2 => array("<span class=\"spanBig\">F</span> ROW DEALS", array())),
            array(8 => array("<span class=\"spanBig\">M</span> ROW MARBLE", array())),
            array(1 => array("<span class=\"spanBig\">OF</span> ROW LEATHER", array()))//array() is the marker
        );

This have more elements but this isn't important now. Same structure but final array of each element is empty.

I create a for loop that retrieve the list of elements from each category , save them in an array and finally save it in the initial empty array, like this:

foreach($data as $dat){
    foreach($dat as $key=>$da){
        $_id = $key;

        $sql = "SELECT  l.name, p.bigimage 
                FROM `LocationsCategory` lc, 
                     `NEProducts` p, 
                     `LocationsCategoryJoin` lcj, `Locations` l
                WHERE lcj.LocationsID = p.location 
                  and p.location is not null 
                  and l.LocationsID = lcj.LocationsID
                  and p.status = 1 
                  and lcj.LocationsCategoryID = " . $_id;

        $result = mysql_query($sql) or die ("ERROR");
        $resultList = array();
        while ($row = mysql_fetch_assoc($result)) {
            //I will adding each key value pair here
            $resultList[$row["name"]] = $row["bigimage"];
        }
        //I will save new constructed array in the marker**
        $da[1] = $resultList;
    }
}

I am having initial array (not desired array) again, Why I can't save my resultList in the desired position? I know that da[1] a local array from the for. I was trying some combitaions with $data but I only broke the array. Any help?

展开全部

  • 写回答

1条回答 默认 最新

  • doom910730 2016-10-10 04:40
    关注

    You are correct that $da is a local copy of the array you want to assign to. In order to assign to the original array, either use references in both foreach loops:

    foreach($data as &$dat){
        foreach($dat as $key => &$da){
    

    Or get the key in both foreach loops and assign to the original array using the keys:

    foreach($data as $i => $dat) {
        foreach($dat as $key => $da) {
            // ...
            $data[$i][$key][1] = $resultList;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?