-2

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?

9
  • 1
    This feels like a job for "not an array of arrays" but a proper data structure? Commented Sep 29, 2020 at 18:13
  • Unfortunately I can't go that way... I really need to use arrays. Commented Sep 29, 2020 at 18:15
  • 1
    Have you tried anything? Have any code? Feels like I am doing your homework. Commented Sep 29, 2020 at 18:15
  • Yes, I've tried with a lot of foreach's, but the code became "unredable". So I was looking for any idea with array_filter or something like that. Commented Sep 29, 2020 at 18:19
  • Does your task mean that all array on the deepest level have the same size? Commented Sep 29, 2020 at 20:45

1 Answer 1

0

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:

$foo = array(
    "0" => array(
        "0" => array(
            '0' => 'value',
            '1' => '',
            '2' => '',
        ),
        "1" => array(
            '0' => 'abc',
            '1' => 'lorem',
            '2' => '',
        )
    ),
    "1" => array(
        "0" => array(
            '0' =>  '',
            '1' => 'hgjklo',
            '2' => ''
        ),
        "1" => array(
            '0' => 'abcdef',
            '1' => 'value',
            '2' => ''
        )
    ),
);

You could then do something like:

<?php
$uids = array_fill_keys(array_keys($foo[0][0]), 0);

$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($foo));
foreach($it as $k =>$v) {
  if (!empty($v)) $uids[$k]++;
}
var_dump($uids);

/*
The value here shows the number of non empty values in the set per key.
array(3) {
  [0]=>
  int(3)
  [1]=>
  int(3)
  [2]=>
  int(0)
}

Clean the array up more and it just gets easier.

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

1 Comment

Thank you for your answer. Indeed this could be solution, but besides key "value" I've another keys, and the array cannot be reduced as in your example.

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.