0

I have this array within array which contains a lot of values:

  183 => 
    array (size=3)
      0 => string 'DE' (length=2)
      1 => string '2015-06-09' (length=10)
      2 => string 'GK' (length=2)   
  184 => 
    array (size=3)
      0 => string 'DE' (length=2)
      1 => string '2015-06-08' (length=10)
      2 => string 'GL' (length=2)   
  185 => 
    array (size=3)
      0 => string 'FR' (length=2)
      1 => string '2015-06-09' (length=10)
      2 => string 'GN' (length=2)   
  186 => 
    array (size=3)
      0 => string 'FR' (length=2)
      1 => string '2015-09-08' (length=10)
      2 => string 'GO' (length=2)

0 is the country code. 1 is a date. 2 is a column on an Excel file.

I want to organize it in this way:

2015-06-09 => 
  array (size=3)
    DE => 
      array (size=2)
        column => GK
        download => 666 
    FR => 
      array (size=2)
        column => GN
        download => 777 

2015-06-08 =>
  array (size=3)
    DE => 
      array (size=2)
        column => GL
        download => 666 
    FR => 
      array (size=2)
        column => GO
        download => 777 

So the same date can show up more than once. if it gets to an array value with the same date - it inserts in it the country code with and its' column.

if it has more than 1 country - it adds a new country. (with the 'download' and column values).

I have this function:

function get_cols_to_array_by_date($array) { 

    $mainarr = array();

    $last_in_arr = count($array); 

    for ($i=0; $i<$last_in_arr; $i++){

            $mainarr[$array[$i][1]] = array( $array[$i][0]=> array('downloads'=> 666, 'col'=>$array[$i][2]) );


    }

    return $mainarr;


}

which outputs an array that runs over the country when it gets to the same date and doesn't give me an array of countries.

What part am I missing in my code? Is there a simpler way to do it? ( PHP syntax shortcuts ;) )

2 Answers 2

1

Assuming that the downloads is the key of the initial array, and each element has 3 elements(date and 2 countries):

Code:

//demo array
    $old = array(
        555=>array(
          0 => 'DE',
          1 => '2015-06-09',
          2 => 'GK'), 
        234=>array(
          0 => 'DE',
          1 => '2015-06-08',
          2 => 'GL'),   
        123=>array(
          0 => 'FR', 
          1 => '2015-06-09',
          2 => 'GN')
          );  

    $new = array();   
    foreach($old as $key=>$arrayValues)
    {
        if(!array_key_exists($arrayValues[1], $new)){ //check if there is already a key by date
            $new[$arrayValues[1]] = array();
        }
        $new[$arrayValues[1]][$arrayValues[0]] = array('column'=>$arrayValues[2], 'downloads'=>$key); //append formated array
    }
    echo "<pre>";
    var_dump($new);
    echo "</pre>";

Output:

array(2) {
  ["2015-06-09"]=>
  array(2) {
    ["DE"]=>
    array(2) {
      ["column"]=>
      string(2) "GK"
      ["downloads"]=>
      int(555)
    }
    ["FR"]=>
    array(2) {
      ["column"]=>
      string(2) "GN"
      ["downloads"]=>
      int(123)
    }
  }
  ["2015-06-08"]=>
  array(1) {
    ["DE"]=>
    array(2) {
      ["column"]=>
      string(2) "GL"
      ["downloads"]=>
      int(234)
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Try looping and checking if element exists, if not - add it.

$result = [];

foreach ($myArray as $key => $values) {
    if (!isset($result[$values[1]])) {
        $result[$values[1]] = [
            $values[0] => [
                'column' => $values[2],
                'download' => $key,
            ]
        ];
    } elseif (!isset($result[$values[1]][$values[0]])) {
        $result[$values[1]][$values[0]] = [
            'column' => $values[2],
            'download' => $key,
        ];
    }
}

Sandbox

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.