1

I have an array that looks like this...

Array
(
    [0] => Array
        (
            [code] => AS34
            [2014-12-10] => 32
        )

    [1] => Array
        (
            [code] => AS34
            [2014-12-11] => 42
        )

    [2] => Array
        (
            [code] => AS34
            [2014-12-12] => 40
        )

    [3] => Array
        (
            [code] => AS34
            [2014-12-15] => 44
        )

    [4] => Array
        (
            [code] => AH98
            [2014-12-10] => 1
        )

    [5] => Array
        (
            [code] => AT78
            [2014-12-12] => 1
        )

    [6] => Array
        (
            [code] => AL44
            [2014-12-10] => 23
        )

    [7] => Array
        (
            [code] => AL44
            [2014-12-11] => 27
        )

    [8] => Array
        (
            [code] => AL44
            [2014-12-13] => 25
        )

    [9] => Array
        (
            [code] => AL44
            [2014-12-15] => 26
        )
)

I am trying to turn it into an array that looks like this...

var $example_data = array(
            array(
                'ID'        => 1,
                'code'     => 'AS34',
                '09/12/14'    => '0',
                '10/12/14'    => '32',
                '11/12/14'    => '42',
                '12/12/14'    => '40',
                '13/12/14'    => '0',
                '14/12/14'    => '0',
                '15/12/14'    => '44',
            ),
            array(
                'ID'        => 2,
                'code'     => 'AH98',
                '09/12/14'    => '0',
                '10/12/14'    => '1',
                '11/12/14'    => '0',
                '12/12/14'    => '0',
                '13/12/14'    => '0',
                '14/12/14'    => '0',
                '15/12/14'    => '0',
            ),
            array(
                'ID'        => 3,
                'code'     => 'AT78',
                '09/12/14'    => '0',
                '10/12/14'    => '0',
                '11/12/14'    => '0',
                '12/12/14'    => '1',
                '13/12/14'    => '0',
                '14/12/14'    => '0',
                '15/12/14'    => '0',
            ),
            array(
                'ID'        => 4,
                'code'     => 'AL44',
                '09/12/14'    => '0',
                '10/12/14'    => '23',
                '11/12/14'    => '27',
                '12/12/14'    => '0',
                '13/12/14'    => '25',
                '14/12/14'    => '0',
                '15/12/14'    => '26',
            ),
        );

So basically it sets up an array for each 'code' and then the previous 7 days. Can anybody point me in the direction of a similar example or some reading on the right method I should be looking at using?

fightstarr20
  • 8,600
  • 24
  • 103
  • 203

3 Answers3

1

Something like (fixed):

$example_data = array();
foreach($data as $id => $row) {
    $code = $row['code'];
    unset($row['code']);

    $karr = array_keys($row);
    $date = current($karr);

    $example_data[$code]['ID'] = $id;
    $example_data[$code]['code'] = $code;
    $example_data[$code][$date] = $row[$date];
}
print_r($example_data);

Test online

Vladimir Vukanac
  • 835
  • 14
  • 27
1

I figured this:

$data = Array
(
    0 => Array
        (
            "id" => 1,
            "code" => "AS34",
            "2014-12-10" => 32
        ),

    1 => Array
        (
            "id" => 1,
            "code" => "AS34",
            "2014-12-11" => 42
        ),

    2 => Array
        (
            "id" => 1,
            "code" => "AS34",
            "2014-12-12" => 40
        ),

    3 => Array
        (
            "id" => 1,
            "code" => "AS34",
            "2014-12-15" => 44
        ),

    4 => Array
        (
            "id" => 1,
            "code" => "AH98",
            "2014-12-10" => 1
        ),

    5 => Array
        (
            "id" => 1,
            "code" => "AT78",
            "2014-12-12" => 1
        ),

    6 => Array
        (
            "id" => 1,
            "code" => "AL44",
            "2014-12-10" => 23
        ),

    7 => Array
        (
            "id" => 1,
            "code" => "AL44",
            "2014-12-11" => 27
        ),

    8 => Array
        (
            "id" => 1,
            "code" => "AL44",
            "2014-12-13" => 25
        ),

    9 => Array
        (
            "id" => 1,
            "code" => "AL44",
            "2014-12-15" => 26
        )
);

function in_array_r($needle, $haystack, $strict = false) { //taken from http://stackoverflow.com/a/4128377/4263082

    foreach ($haystack as $item) {

        if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
            return true;
        }

    }

    return false;
}

$codes = Array();
$counter = -1;

foreach($data as $key => $value) {

    if(!in_array_r($data[$key]["code"], $codes, true)) {

        $codes[++$counter] = Array();

        foreach($data[$key] as $subkey => $subvalue) {$codes[$counter][$subkey] = $subvalue;}

    }
    else{

        foreach($data[$key] as $subkey => $subvalue) {

            if($subkey != "code" && $subkey != "id") {$codes[$counter][$subkey] = $subvalue;}

        }

    }

}

print_r($codes);

OUTPUT

Array
(
    [0] => Array
        (
            [id] => 1
            [code] => AS34
            [2014-12-10] => 32
            [2014-12-11] => 42
            [2014-12-12] => 40
            [2014-12-15] => 44
        )

    [1] => Array
        (
            [id] => 1
            [code] => AH98
            [2014-12-10] => 1
        )

    [2] => Array
        (
            [id] => 1
            [code] => AT78
            [2014-12-12] => 1
        )

    [3] => Array
        (
            [id] => 1
            [code] => AL44
            [2014-12-10] => 23
            [2014-12-11] => 27
            [2014-12-13] => 25
            [2014-12-15] => 26
        )

)
Verhaeren
  • 1,655
  • 7
  • 10
  • I'm getting an Invalid argument supplied for foreach() for $data – fightstarr20 Dec 16 '14 at 00:18
  • I tested that here: http://writecodeonline.com/php/ and the **OUTPUT** that I posted in the answer is taken from the result of that test in that site. Let me see if I typoed something while copy/pasting. – Verhaeren Dec 16 '14 at 00:20
  • No, it's perfectly find. Do this: copy all the code in the first block of code (not the ouput code of course), enter this site: http://writecodeonline.com/php/ , paste the code there, and hit runcode. That's what I did. – Verhaeren Dec 16 '14 at 00:21
  • But it doesnt work if you use my original data. I think it is the formatting of the array? – fightstarr20 Dec 16 '14 at 00:33
  • 1
    I used the array that you have in the first block of code in your question. Of course, I made some changes to pass the parsing like removing `[]` wrapping keys with quotes, providing commas, etc. But the data is the same. You have to name that array `$data`. – Verhaeren Dec 16 '14 at 00:35
  • Then I think I need to create a new stack question on how to format my array like yours! – fightstarr20 Dec 16 '14 at 00:37
  • 1
    Again: it is your array, I just provide it a name `$data` and fix the parsing errors. Plus, a different array shouldnt be a problem either as long as it is a 2d array. – Verhaeren Dec 16 '14 at 00:40
  • 1
    If you need to parse the output of print_r - here is a solution for it here: http://stackoverflow.com/questions/14218698/is-there-a-way-to-parse-the-output-of-print-r see function print_r_reverse in the comments here: http://php.net/manual/en/function.print-r.php – weinerk Dec 16 '14 at 01:29
0

I got this working:

<?php
header ('Content-type: text/plain; charset=utf-8');

//source data from the question
$sourceArr = array(
  array('id'            => 1
        ,'code'         => 'AS34'
        ,'2014-12-10'   => '32'
  )
  ,array('id'           => 1
         ,'code'        => 'AS34'
         ,'2014-12-11'  => '42'
  )
  ,array('id'           => 1
         ,'code'        => 'AS34'
         ,'2014-12-12'  => '40'
  )
  ,array('id'           => 1
         ,'code'        => 'AS34'
         ,'2014-12-15'  => '44'
  )
  ,array('id'           => 1
         ,'code'        => 'AH98'
         ,'2014-12-10'  => '1'
  )
  ,array('id'           => 1
         ,'code'        => 'AT78'
         ,'2014-12-12'  => '1'
  )
  ,array('id'           => 1
         ,'code'        => 'AL44'
         ,'2014-12-10'  => '23'
  )
  ,array('id'           => 1
         ,'code'        => 'AL44'
         ,'2014-12-11'  => '27'
  )
  ,array('id'           => 1
         ,'code'        => 'AL44'
         ,'2014-12-13'  => '25'
  )
  ,array('id'           => 1
         ,'code'        => 'AL44'
         ,'2014-12-15'  => '26'
  )
);

foreach($sourceArr as $k => $v)
{
  //get dates for last seven days
  $d0 = date('Y-m-d',time() - 60 * 60 * 24 * 0);
  $d1 = date('Y-m-d',time() - 60 * 60 * 24 * 1);
  $d2 = date('Y-m-d',time() - 60 * 60 * 24 * 2);
  $d3 = date('Y-m-d',time() - 60 * 60 * 24 * 3);
  $d4 = date('Y-m-d',time() - 60 * 60 * 24 * 4);
  $d5 = date('Y-m-d',time() - 60 * 60 * 24 * 5);
  $d6 = date('Y-m-d',time() - 60 * 60 * 24 * 6);

  //if we have a valid element - then save it in temp array
  if(array_key_exists($d0,$v)){ $tmpArr[$v['code']][$d0] = "'$v[$d0]'"; }
  if(array_key_exists($d1,$v)){ $tmpArr[$v['code']][$d1] = "'$v[$d1]'"; }
  if(array_key_exists($d2,$v)){ $tmpArr[$v['code']][$d2] = "'$v[$d2]'"; }
  if(array_key_exists($d3,$v)){ $tmpArr[$v['code']][$d3] = "'$v[$d3]'"; }
  if(array_key_exists($d4,$v)){ $tmpArr[$v['code']][$d4] = "'$v[$d4]'"; }
  if(array_key_exists($d5,$v)){ $tmpArr[$v['code']][$d5] = "'$v[$d5]'"; }
  if(array_key_exists($d6,$v)){ $tmpArr[$v['code']][$d6] = "'$v[$d6]'"; }

}

//create the result array
$cnt = 0;
foreach($tmpArr as $k => $v)
{
  $resultArr[$cnt]['ID'] = $cnt;
  $resultArr[$cnt]['code'] = "'$k'";
  asort($v);
  foreach($v as $k2 => $v2)
  {
    $resultArr[$cnt][$k2] = $v2;
  }
  $cnt++;
}

//output
echo "Old array: ".print_r($sourceArr,1);
echo "New array: ".print_r($resultArr,1);

?>
weinerk
  • 404
  • 5
  • 9