I've an array with the following structure:
array(
"0" => array(
"0" => array(
'0' => array('value' => 'value'),
'1' => array('value' => ''),
'2' => array('value' => ''),
),
"1" => array(
'0' => array('value' => 'abc'),
'1' => array('value' => 'lorem'),
'2' => array('value' => ''),
)
),
"1" => array(
"0" => array(
'0' => array('value' => ''),
'1' => array('value' => 'hgjklo'),
'2' => array('value' => ''),
),
"1" => array(
'0' => array('value' => 'abcdef'),
'1' => array('value' => 'value'),
'2' => array('value' => ''),
)
),
)
and what I'm trying to accomplish is to remove all keys for the "empty" value, but only if the value is empty in all* child-arrays inside the main-array.
Expected output:
array(
"0" => array(
"0" => array(
'0' => array('value' => 'value'),
'1' => array('value' => ''),
),
"1" => array(
'0' => array('value' => 'abc'),
'1' => array('value' => 'lorem'),
)
),
"1" => array(
"0" => array(
'0' => array('value' => ''),
'1' => array('value' => 'hgjklo'),
),
"1" => array(
'0' => array('value' => 'abcdef'),
'1' => array('value' => 'value'),
)
),
)
NOTE: child-array position 2 was removed, because it was empty in all the main-array positions.
Does anyone have a great idea how this can be done without a complex logic of foreach's?
As expressed, the structure of the data is not helpful. I agree
array_
functions are generally great, but they work better with structures that make sense.If for example you removed just a little of the useless cruft, to achieve an array like the following one:
You could then do something like:
Clean the array up more and it just gets easier.