1

I am trying to edit a plugin that is fetching a multidimensional array, then breaking it out into a foreach statement and doing stuff with the resulting data.

What I am trying to do is edit the array before it gets to the foreach statement. I want to look and see if there is a key/value combination that exists, and if it does remove that entire subarray, then reform the array and pass it to a new variable.

The current variable

$arrayslides 

returns several subarrays that look like something like this (I remove unimportant variables for the sake of briefness):

Array ( 
  [0] => Array ( 
    [slide_active] => 1 
  ) 
  [1] => Array ( 
    [slide_active] => 0 
  )
) 

What I want to do is look and see if one of these subarrays contains the key slide_active with a value of 0. If it contains a value of zero, I want to dump the whole subarray altogether, then reform the multidimensional array back into the variable

$arrayslides 

I have tried a few array functions but have not had any luck. Any suggestions?

2
  • 6
    sounds like a prima facie case for array_filter() with a simple callback to test slide_active Commented Jan 31, 2012 at 11:44
  • Can't you change the SQL query that fetches the data instead? Would be much cleaner. Commented Jan 31, 2012 at 11:47

2 Answers 2

3
$arrayslides = array(0 => array (  'slide_active' => 1, 'other_data' => "Mark" ),
                     1 => array (  'slide_active' => 0, 'other_data' => "ABCDE"  ),
                     2 => array (  'slide_active' => 1, 'other_data' => "Baker"  ),
                     3 => array (  'slide_active' => 0, 'other_data' => "FGHIJ"  ),
                    );


$matches = array_filter($arrayslides, function($item) { return $item['slide_active'] == 1; } );

var_dump($matches);

PHP >= 5.3.0

Sign up to request clarification or add additional context in comments.

Comments

0

I know its not so efficient but still

foreach ($arraySlides as $key => $value)
{
    if(in_array('0', array_values($value))
      unset($arraySlides[$key]);
}

1 Comment

Thanks Uday, even though its not so efficient still its an option.

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.