1

I not find something similar on Stackoweflow and need help.

I have an array like this:

$array = array(
    array(
        1 => false,
        2 => false,
        3 => true,
        4 => true,
        5 => false,
        6 => false,
        7 => false,
        8 => false,
        9 => false,
        10 => false,
    ),
    array(
        1 => false,
        2 => false,
        3 => false,
        4 => true,
        5 => true,
        6 => true,
        7 => true,
        8 => false,
        9 => false,
        10 => false,
    ),
    array(
        1 => false,
        2 => false,
        3 => false,
        4 => false,
        5 => false,
        6 => false,
        7 => false,
        8 => false,
        9 => true,
        10 => true,
    ),
);

and I need result like this:

array(
    1 => false,
    2 => false,
    3 => true,
    4 => true,
    5 => true,
    6 => true,
    7 => true,
    8 => false,
    9 => true,
    10 => true,
)

Generaly this is one calendar functionality and I counting fom 1-31 but this is not realy important.

I try with call_user_func_array and array_merge_recursive, try some custom foreach but I stack here in logic.

Like you see, this is NOT 2 multidimensional arrays. This is only one multidimensinal array and I need to transform it in one simple array like example above.

Boolean true is most important and need to be on the right place like value.

EDIT:

Thanks to @MacBooc i get working results:

$r = array();
foreach($array as $k=>$a){
    foreach($a as $n=>$b){
        if($b===true)
            $r[$n] = $b;
    }
}
$result = array_replace($array[0], $r);
ksort($result);
var_dump($result);

NOTE:

-Admins, please read questions carefully before you close or downvote something.

15
  • which inner array will have higher preference ? Commented Dec 13, 2016 at 13:49
  • Generaly reading from top to bottom is my goal and value need to be merged also. My goal is to merge complete array in one standard array where is key day (number int.) and value boolean true/false for each day to I can do other things. Commented Dec 13, 2016 at 13:52
  • if all the arrays will be of same size then u can just pick last inner array, Commented Dec 13, 2016 at 13:55
  • 1
    a for loop with you number of key .. a boolean variable in the for loop with a false value .. in the foreach of your multiarray you check if the value at the for statement key is true, then your boolean variable is true, else you do nothing, and after the foreach you create each line of a final array with the key of the for loop and the value of your boolean.. really sorry about my english and it's not really optimize but it's work Commented Dec 13, 2016 at 14:09
  • 1
    yeah I think it's pretty sad for this kind of website.. you are welcome and good luck Commented Dec 13, 2016 at 14:14

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.