0

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?

5
  • Hello, can we look at the table you're trying to fetch? Is it always 3 values for every array? Commented Oct 10, 2016 at 12:35
  • 3
    Didn't you already ask this question? stackoverflow.com/questions/39882185/… Commented Oct 10, 2016 at 12:36
  • remove the array() from array(3 => array("<span class=\"spanBig\">A</span> ROW GREENS", array())), so it Looks like this array(3 => array("<span class=\"spanBig\">A</span> ROW GREENS")), and then in ur Loop use $da[$_id][] = $resultList; Commented Oct 10, 2016 at 12:38
  • @PatrickQ No, if you read about the topic, this question is different. Commented Oct 10, 2016 at 14:23
  • @RiggsFolly Thanks for the recomendation, I used to work in Java, but I have to make some changes in an old php application, We should try dont change anything if its possible Commented Oct 10, 2016 at 14:24

1 Answer 1

1

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;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.