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.

Ivijan Stefan Stipić
  • 6,249
  • 6
  • 45
  • 78
  • which inner array will have higher preference ? – Purushottam zende Dec 13 '16 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. – Ivijan Stefan Stipić Dec 13 '16 at 13:52
  • if all the arrays will be of same size then u can just pick last inner array, – Purushottam zende Dec 13 '16 at 13:55
  • @Purushottamzende NO. Look carefull. Multiarray contain in each new array diferent values and same key. I need merge keys and values to get all true statemants on the right place. Boolena `true` is important to be on the right place. Don't know how to explain. – Ivijan Stefan Stipić Dec 13 '16 at 13:57
  • i get the logic but it's hard to explain it in comment :/ but basically you need a for loop and a foreach loop inside – Frankich Dec 13 '16 at 14:00
  • Good @MacBooc how? I try many things today. :D – Ivijan Stefan Stipić Dec 13 '16 at 14:02
  • 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 – Frankich Dec 13 '16 at 14:09
  • English is good, this is great. I will try. Many thenks to you and not thanks for non-educated admins who not read first problems and close this topic for answers. – Ivijan Stefan Stipić Dec 13 '16 at 14:13
  • 1
    yeah I think it's pretty sad for this kind of website.. you are welcome and good luck – Frankich Dec 13 '16 at 14:14
  • Result `$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);` – Ivijan Stefan Stipić Dec 13 '16 at 14:21
  • your code works ? i think you'll never get false value right ? – Frankich Dec 13 '16 at 14:23
  • $r = array(); for($i = 0; $i< count($array[0]);$i++){$boolean = false; foreach($array as $row){ if($row[$i]===true){ $boolean = true ; } $r[$i] = $boolean; } .... i think this work fine too but maybe less optimize – Frankich Dec 13 '16 at 14:26
  • I get good results, try please, your result work but is offset 0 problem in for loop. – Ivijan Stefan Stipić Dec 13 '16 at 14:30
  • @IvijanStefanStipić, please, take a look at the [demo](http://sandbox.onlinephpfunctions.com/code/04019c14b2bd893a6022b27c36246b758069b78c). You can use `array_map`. Though it does not preserve keys. To preserve keys you can use `array_combine` additionally. – sevavietl Dec 13 '16 at 15:58
  • @sevavietl Yes, good point only this show form 0-9 but is same thing. GREAT! No think about this. – Ivijan Stefan Stipić Dec 13 '16 at 16:05

0 Answers0