PHP concatenation multiple times to generate a new array

I have several arrays that look like below

 array(
    [0] =>
        array( ['direction'] =>  '0.000,0.160,0.123,0.104,0.000' )
    [1] =>
        array( ['direction'] =>  '0.000,0.101,0.237,0.101,0.000' )
    [2] =>
        array( ['direction'] =>  '0.000,0.160,0.125,0.163,0.000' )
  )

      

with for loop, I would like to create a new array that looks like

data1 = [0.000, 0.000, 0.000]
data2 = [0.160, 0.101, 0.160]
data3 = [0.123, 0.237, 0.125]
data4 = [0.104, 0.101, 0.163]
data5 = [0.000, 0.000, 0.000]

      

which means that for a for loop, I would like to get each column of the value and then assign that value to each new array.

what i tried to do is

$cnt = count($array);
$buildCnt = count($array['direction']);
for($i = 0; $i < $cnt; $i++){
    for($j = 0; $j < buildCnt; $j++){
     'i need to do something here
    }
}

      

anyone help me? Thanks to

+3


source to share


5 answers


I don't know if there is a better way through array functions, you can just use a regular foreach loop. Like this:

$original_array = array(
    array('direction' => '0.000,0.160,0.123,0.104,0.000'),
    array('direction' => '0.000,0.101,0.237,0.101,0.000'),
    array('direction' => '0.000,0.160,0.125,0.163,0.000'),
);
$new_array = array();
foreach($original_array as $sub_array) {
    // loop each sub array
    $pieces = explode(',', $sub_array['direction']);
    // explode by comma
    foreach($pieces as $key => $piece) {
        // each piece, push by data(n)
        $new_array["data".($key+1)][] = $piece;
        // if you don't want to use extract
        // ${"data".($key+1)}[] = $piece;
    }
}

extract($new_array); // import newly created sub arrays
echo '<pre>';
print_r($data1); // $data2, $data3, ... and so on

      



$data1

should look like this:

Array
(
    [0] => 0.000
    [1] => 0.000
    [2] => 0.000
)

      

+1


source


$arr = array(array( 'direction' =>  '0.000,0.160,0.123,0.104,0.000' ),array( 'direction' =>  '0.000,0.101,0.237,0.101,0.000' ),array( 'direction' =>  '0.000,0.160,0.125,0.163,0.000' ));
foreach($arr AS $dirArr){
  $dirs = explode(",",$dirArr['direction']);
  for($i = 0; $i < count($dirs); $i++) $data[$i][] = $dirs[$i];
}

      

and if you really need these variable names ...



$data1 = $data[0];
$data2 = $data[1];
$data3 = $data[2];
$data4 = $data[3];
$data5 = $data[4];

      

+1


source


function loop_array($input) {
    foreach ($input as $val) {
        $mydata = explode(',', $val['direction']);
        foreach ($mydata as $k => $v) {
            $output[$k][] = $v;
        }
    }

    return $output;
}

      

+1


source


$data = array(
    array('direction' => '0.000,0.160,0.123,0.104,0.000'),
    array('direction' => '0.000,0.101,0.237,0.101,0.000'),
    array('direction' => '0.000,0.160,0.125,0.163,0.000')
);

      

When each value is exploded

, the keys will already match the indices you want to put in the last array. The first item in each will be 0, the next 1, etc.

Example:

explode(',','0.000,0.160,0.123,0.104,0.000');
array (size=5)
  0 => string '0.000' (length=5)
  1 => string '0.160' (length=5)
  2 => string '0.123' (length=5)
  3 => string '0.104' (length=5)
  4 => string '0.000' (length=5)

      

So, then it's enough to simply re-use those indices in your output.

$output = array();
foreach ($data as $row) {
    // Skip row, handling error from potential missing key.
    // Could also first validate the return from explode.
    if (!isset($row['direction'])) continue;

    foreach (explode(',',$row['direction']) as $key => $value) {
        $output[$key][] = $value;
    }
}
var_dump($output);

      

Results:

array (size=5)
  0 => 
    array (size=3)
      0 => string '0.000' (length=5)
      1 => string '0.000' (length=5)
      2 => string '0.000' (length=5)
  1 => 
    array (size=3)
      0 => string '0.160' (length=5)
      1 => string '0.101' (length=5)
      2 => string '0.160' (length=5)
  2 => 
    array (size=3)
      0 => string '0.123' (length=5)
      1 => string '0.237' (length=5)
      2 => string '0.125' (length=5)
  3 => 
    array (size=3)
      0 => string '0.104' (length=5)
      1 => string '0.101' (length=5)
      2 => string '0.163' (length=5)
  4 => 
    array (size=3)
      0 => string '0.000' (length=5)
      1 => string '0.000' (length=5)
      2 => string '0.000' (length=5)

      

geez, beat him by 5 people!

+1


source


This solution first creates an intermediate array and calculates the largest possible array of "directions". This is necessary if the number of data points in the "direction" is not always the same (so we know how many arrays need to be created). You can probably simplify this (see other answers) if the "direction" values ​​always have the same number of data points.

$data = array(
    array( 'direction' => '0.000,0.160,0.123,0.104,0.000' ),
    array( 'direction' => '0.000,0.101,0.237,0.101,0.000' ),
    array( 'direction' => '0.000,0.160,0.125,0.163,0.000' )
);

$maxLength = 0;
$tempData = array();
foreach( $data as $datum ) {
    $tempDatum = isset( $datum[ 'direction' ] ) ? explode( ',', $datum[ 'direction' ] ) : array();
    $maxLength = max( $maxLength, count( $tempDatum ) );
    $tempData[] = $tempDatum;
}

$newData = array();
for( $i = 0; $i < $maxLength; $i++ ) {
    $new = array();
    for( $j = 0, $len = count( $tempData ); $j < $len; $j++ ) {
        $new[] = isset( $tempData[ $j ][ $i ] ) ? $tempData[ $j ][ $i ] : null;
    }
    $newData[] = $new;
}

var_dump( $newData );

      

+1


source







All Articles