kirchner20
kirchner20

Reputation: 97

Create new array from existing array in php

Good Day I have an array containing data seperated by a comma:

array (
       [0]=>Jack,140101d,10
       [1]=>Jack,140101a,15
       [2]=>Jack,140101n,20
       [3]=>Jane,141212d,20
       [4]=>Jane,141212a,25
       [5]=>Jane,141212n,30
      )

There is a lot of data and I would like the data to be set out as:

array(
      [Jack]=>
          [140101]
              =>[d] =>10
              =>[a] =>15
              =>[n] =>20
      )

My code:

           foreach ($out as $datavalue) {
                    $dat = str_getcsv($datavalue,',');
                    $datevalue = substr($dat[1],2,-1);
                    $shiftvalue = substr($dat[1],-1);
                    $totalvalue = $dat[2];
                    $sval[$shiftvalue] = $totalvalue;
                    $dval[$datevalue] = $sval;
                    $opvalue = $dat[0];
                    $final[$opvalue] = $dval;
           }

Now it seems the array is populated even if there is no data from the original string, so my output shows results for Jack on the other dates even though there was no data for him originally. Hope this makes sense. Could anyone point out or suggest a solution please?

Upvotes: 1

Views: 94

Answers (1)

Andresch Serj
Andresch Serj

Reputation: 37328

As mentioned in the comments, explode is what you need. See this working here.

<?php

$input = array (
  0 => 'Jack,140101d,10',
  1 => 'Jack,140101a,15',
  2 => 'Jack,140101n,20',
  3 => 'Jane,141212d,20',
  4 => 'Jane,141212a,25',
  5 => 'Jane,141212n,30',
);

$result = array();

foreach ($input as $key => $value) {
  $valueParts = explode(',',$value); // now valueparts is an array like ('Jack','140101d','10')
  $namePart     = $valueParts[0];
  $idPart   = substr($valueParts[1],0,-1); // we need to strip the letter from the id
  $charPart     = substr($valueParts[1],-1); // and the id from the letter
  $nrPart   = $valueParts[2]; // you could use intval() to make this an integer rather than a string if you want

  // Now we fill the array
  if(!array_key_exists($namePart, $result)) {
    $result[$namePart] = array();
  }
  if(!array_key_exists($idPart, $result[$namePart])) {
    $result[$namePart][$idPart] = array();
  }
  if(!array_key_exists($charPart, $result[$namePart][$idPart])) {
    $result[$namePart][$idPart][$charPart] = $nrPart;
  }
}

var_dump($result);

Upvotes: 1

Related Questions