0

This code should explain it all..

foreach($this->sections as $k => $section){ 

    $preview = array();

    foreach ($section['fields'] as $k => $type) {                               
       $preview[] = $type['type'];
    }

    if ($preview == 'header_preview'){
        echo $preview;
        // I will loop content here based on the $section loop NOT the $type loop
    }

}

I just need to get each $section['fields'] then outside that loop, which again gets a list of all the $section['fields'] then use one of those field types to create an if statement. The above is non working, I will show you working code here.

foreach($this->sections as $k => $section){ 

    foreach ($section['fields'] as $k => $type) {                               
        if ($type['type'] == 'header_preview'){
           //I work but im in a nested loop I need out
        }
     }

  //The main loop here.. the above loop is just to setup data to use inside this loop? Make sense? I hope!

}

I hope this makes sense...

Snippet of var_dump $this->sections

array(26) { ["general"]=> array(3) { ["title"]=> string(7) "General" ["icon"]=>    string(106) "img/icons/sub.png" ["fields"]=> array(5) { [0]=> array(6) { ["id"]=>    string(10) "responsive" ["type"]=> string(6) "switch" ["title"]=> string(35) "Responsive" ["desc"]=> string(10) "Responsive" ["options"]=> array(2) { [1]=> string(2) "On"    [0]=> string(3) "Off" }
8
  • your first code is definitely wrong but can you explain please what are you trying to do? Commented Aug 24, 2013 at 14:40
  • well I am looping each $section there are field types and I am trying to exclude a field type but the only way to access the types is with a foreach loop Commented Aug 24, 2013 at 14:45
  • 1
    can you add var_dump($this->sections) before your parent loop and show us the result and give us an example of the output you need the loop to set in the var $preview Commented Aug 24, 2013 at 14:47
  • Yeah, but that returns a lot of data, I think it would be better to show you the $section['fields'] dump or is it essential you know the $this->sections? Commented Aug 24, 2013 at 14:52
  • 1
    i don't need the full list of data inside $this->sections i just need to see the structure of this array and to know what you are trying to do and why you need this variable outside the child loop Commented Aug 24, 2013 at 14:54

3 Answers 3

1

It could be possible $k has been duplicated and therefore the loop doesn't know what to do. You can try changing $k to $x, see if it works.

foreach($this->sections as $k => $section){ 

    $preview = array();

    foreach ($section['fields'] as $x => $type) {                               
       $preview[] = $type['type'];
    }

    foreach($preview as $elem){
        if ($elem == 'header_preview'){
            echo($elem);
        }
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

$preview is an array, you can NOT echo an array, if don't know what you r typing then please don't give wrong answers!!!
Well that was his code. I didn't type, The first thing I noticed was $k being used twice.
Yes dear but you can't correct just one point and leave others this will cause an issue and it will output an error!, better to understand what he is willing to do with his code then answer the question ;)
It's hard to articulate what I am doing super clearly, but dont over think it, im just trying to access an array that has a list of field types so I can use that to prevent a field type from being in the loop.. My terminology may be off, and I think my logic is obvious but the wording may be off..
This works William Yang good write up but this contradicts my point, I dont want the if statement in a nested foreach, because in the live code that will mess up my loop... The loop is essential to loop through each section, but I want to limit a section that has the type, header_preview.. maybe I need to reconstruct some logic.. me brain hurts lol
1

Hmm... Maybe

foreach($this->sections as $k => $section){ 

    $preview = array();

    foreach ($section['fields'] as $x => $type) {                               
       $preview[] = $type['type'];
    }

    if(!in_array('header_preview', $preview)){

        // Here $preview DOES NOT contain 'header_preview'
        // Do stuff

    } 
}

4 Comments

Testing... Im going to need to add a not ! though.. because I am trying to exclude header_preview..
Ahh.. then just add ! in front of in_array
Yeah, nice looking good so far.. need to adjust some stuff.. post back here in a min.. thanks for the effort
William based on what I gave you helped out very well, nothing more you could have done.. I commented on MaveRicks answer and failed to mention I was using do_settings_sections inside my loop and that handled the loop, its a weird function to me, the above code would have worked had it not been the wordpress function do_settings_sections...
1
$header_preview=false;
foreach($this->sections as $k => $section){ 
    $header_preview=false;// reset the var $header_preview
    foreach ($section['fields'] as $k => $type) {                               
        if ($type['type'] == 'header_preview') $header_preview = true;
    }
    if($header_preview==true){
        //here you can add or extract from this array that has `header_preview` value for one of it's field type
    }
}

1 Comment

I thank you for your effort... I failed to mention I was using WordPress's do_settings_sections inside the loop, new to that hook/function, but I figured it out I played with it earlier but couldn't figure out the array I was reading, and couldn't access header_preview, but then I did another attempt and got it... Hope I didn't waste your time, I spent about 2 and a half hours too long trying to figure this thing out so I definitely wasted my time... I think by your code failing I realized something was wrong.. insanity defined by Albert Einstein is was I was doing lol.

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.