0

I have array in this format...

[User] => Array
    (
        [Oct 09] => Array
            (
                [10] => 1
                [8] => 0
                [9] => 0
                [7] => 0
                [6] => 0
            )

        [Oct 10] => Array
            (
                [10] => 0
                [8] => 1
                [9] => 0
                [7] => 0
                [6] => 0
            )

        [Oct 11] => Array
            (
                [10] => 1
                [8] => 0
                [9] => 1
                [7] => 1
                [6] => 1
            )
 )

I want to parse the array without using any loop so that it converts in the form

[User]=>array
( 
  [0] =>array(      
    [date] => OCt 09
    [user_id] => 10
    [attendance] => 1
   )
  [1] =>array(
    [date] => OCt 09
    [user_id] => 8
    [attendance] => 0
   ) 
)

Like this I dont' want to use foreach or for loop as it take time to parse. I want to use inbuilt array function. What I tried was

    public function parseAttendance($y){
        return array("date", "user_id", "attendane"); // I want to return something like in this format.
    }
    public function admin_attendance() {
         $result = array_map(array($this, 'parseAttendance'), $arr['User']);
         print_r($result);
         die();
    } 
5
  • 3
    You're just going to make it complexer and uglier, just use a foreach loop and make it simple for future users who would (re)view your code ... Also if you're going to use array_map() please don't forget to check the manual, there are useful examples. Commented Oct 9, 2013 at 7:13
  • I am not using foreach and for as these are slower than array_map or array_walk Commented Oct 9, 2013 at 7:18
  • 1
    who said it's slower ? Have you benchmarked it ? When you've done it then check the difference, mostly you won't even notice the difference and then you should go for readable/maintainable code Commented Oct 9, 2013 at 7:20
  • 2
    You need to make a compromise between speed and readability. If you can't figure out how to do it without a foreach, you need to see the guy that will read the code. So maybe it is slower (and define slower, probably it will take more time to make a db conn then the foreach), but you atleast understand the code. Also pre mature optimalsation is the root of evil Commented Oct 9, 2013 at 7:28
  • How would I implement it with for or foreach loop. I tried with those also but I couldn't. Can you please help ? Commented Oct 9, 2013 at 7:31

1 Answer 1

1

You first create a function that is able to build the array based on the input:

function add_entries(array &$entries, $key, $values) {
    $count = 0;
    foreach ($values as $user_id => $attendance) {
        $count++;
        $entries[] = [
            "date" => $key,
            ...
        ];
    }
    return $count;
}

Then you test that function if it does what you need to for the input. If everything is alright, you only need to use it with your input array:

$input  = obtain_input_array_however_you_do_it();
$output = array();
$count  = 0;

foreach ($input as $key => $values) {
    $count += add_entries($output, $key, $values)
}

Sure this is only one way to solve it, but hopefully helpful anyway. BTW with the kind of transposing you can not just use array_map or array_walk because you switch axes here in the 2d-plane. That means you need to create a new array (temporarily at least) otherwise you would loose information within the transformation.

And if the data comes from some specific data-store, you should consider to query it differently so that you do not need to make a complicated transformation later on. In any case I would wrap the whole transformation again into a function of it's own if not even inside a class.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.