Arun
Arun

Reputation: 37

Fetching a multidimensional array

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?

Upvotes: 1

Views: 381

Answers (2)

Uday Sawant
Uday Sawant

Reputation: 5798

I know its not so efficient but still

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

Upvotes: 0

Mark Baker
Mark Baker

Reputation: 212412

$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

Upvotes: 3

Related Questions