0

This is a possible duplicate entry. Unfortunalty i can't figure this out from the other entries on Stackoverflow. So please help, if you can :)

I am playing with a mini statistics script in php, to show some basic statistics.

I have a multidimensional array that looks like this:

$found_data = 
array( 
    array('2011-11-02' => 'mobile'),
    array('2011-11-02' => 'mobile'),
    array('2011-11-04' => 'mobile'),
    array('2011-11-08' => 'Desktop'),
    array('2011-11-08' => 'mobile'),
    array('2011-11-08' => 'mobile'),
    array('2011-11-08' => 'mobile'),
    array('2011-11-15' => 'mobile'),
    array('2011-11-18' => 'mobile'),
    array('2011-11-21' => 'Desktop'),
    array('2011-11-23' => 'mobile'),
    array('2011-11-28' => 'Desktop'),
    array('2011-11-30' => 'mobile')
);

Now i want to represent this data like this:

2011-11-01: 0 from mobile and 0 from desktop
2011-11-02: 2 from mobile and 0 from desktop
...
2011-11-30: 1 from mobile and 0 from desktop

I'm pretty sure you got the idea.

I have tried the following in a lot of variations, but nothing works:

echo count(array_keys($found_data['2011-11-02'], "mobile"));

Any help is much appreciated :)

2 Answers 2

1

You cannot index your $found_data like this: $found_data['2011-11-02'] because you have no key named '2011-11-02' there. Your $found_data declaration is equivalent to:

$found_data = 
array( 
    0 => array('2011-11-02' => 'mobile'),
    1 => array('2011-11-02' => 'mobile'),
    2 => array('2011-11-04' => 'mobile'),
    ... // and so on
);

I know you're using multidimension to use the same key multiple times, but as the structure above shows, you can't simply use array_keys.

One way to get what you want is to:

  1. Create a result variable, initialize as empty array
  2. Get next item in $found_data
  3. Get the key and value of the item
  4. Check whether key exists in result, if yes, increment the respective value. Otherwise create a new array with mobile and desktop as keys, and use value to determine which one should be initialize with 1 (the other is then 0). Assign this array to result with key key

The expected result array structure is like this:

$result = array(
    '2011-11-02' => array(
        'mobile' => 2,
        'desktop' => 1,
    ),
    '2011-11-04' => array(
        'mobile' => 0,
        'desktop' => 1,
    ),
    '2011-11-05' => array(
        'mobile' => 2,
        'desktop' => 0,
    ),
);
Sign up to request clarification or add additional context in comments.

2 Comments

I am not quite sure what you mean.. Can you give me an example to point me in the right direction?
This is a great idea! I will test later today and write after, +1 for now :)
0

I have solved it in a different wy, than the above suggested.

If any one should be interested, this is the answer:

$found_data = 
array( 
    array('2011-11-02' => 'Mobile'),
    array('2011-11-02' => 'Mobile'),
    array('2011-11-04' => 'Mobile'),
    array('2011-11-08' => 'Desktop'),
    array('2011-11-08' => 'Mobile'),
    array('2011-11-08' => 'Mobile'),
    array('2011-11-08' => 'Mobile'),
    array('2011-11-15' => 'Mobile'),
    array('2011-11-18' => 'Mobile'),
    array('2011-11-21' => 'Desktop'),
    array('2011-11-23' => 'Mobile'),
    array('2011-11-28' => 'Desktop'),
    array('2011-11-30' => 'Mobile')
);



foreach($found_data as $single_data)
{
    foreach($single_data as $data => $key)
    {   
        $array_combined[$data]['Mobile'] = array();
        $array_combined[$data]['Tablet'] = array();
        $array_combined[$data]['Desktop'] = array();                
    }
}


foreach($found_data as $single_data)
{
    foreach($single_data as $data => $key)
    {
        if($key=='Desktop')
            array_push($array_combined[$data]['Desktop'], $key);
        else if($key=='Tablet')
            array_push($array_combined[$data]['Tablet'], $key);
        else if($key=='Mobile')
            array_push($array_combined[$data]['Mobile'], $key); 

    }
}


$startdate = strtotime('2011-11-01 00:00:01');
$days = 30;

echo 'Total hits the last 30 days: '.count($found_data).'<br />';

echo 'Desktop hits the last 30 days<br />';
for ($i = 0; $i <= $days; $i++) {
    $date = date('Y-m-d', $startdate+$i*60*60*24);
    printf("%s\t%s\n", $date, array_key_exists($date, $array_combined) ? count($array_combined[$date]['Desktop']) : 0);
    echo '<br />';
}

echo 'Tablet hits the last 30 days<br />';
for ($i = 0; $i <= $days; $i++) {
    $date = date('Y-m-d', $startdate+$i*60*60*24);
    printf("%s\t%s\n", $date, array_key_exists($date, $array_combined) ? count($array_combined[$date]['Tablet']) : 0);
    echo '<br />';
}

echo 'Mobile hits the last 30 days<br />';
for ($i = 0; $i <= $days; $i++) {
    $date = date('Y-m-d', $startdate+$i*60*60*24);
    printf("%s\t%s\n", $date, array_key_exists($date, $array_combined) ? count($array_combined[$date]['Mobile']) : 0);
    echo '<br />';
}

Propably not the best solution, but works as intented.

Comments

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.