3

I have some arrays which looks 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 generate new array which would 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 with for loop, I would like to get each column of value 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 please?? Thanks

5 Answers 5

1

I don't know if there is a better way thru array functions by you can just also use a normal 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:

Array
(
    [0] => 0.000
    [1] => 0.000
    [2] => 0.000
)
Sign up to request clarification or add additional context in comments.

Comments

1
$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 them to be those variable names...

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

Comments

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

    return $output;
}

Comments

1

This solution creates an intermediate array first and calculates the longest possible 'direction' array. This is necessary if the amount of data points in 'direction' isn't always the same (so that we know how many arrays to create). You can probably simplify this (see other answers) if the 'direction' values always have the same amount 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 );

Comments

1
$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 indexes you're wanting to put them at in the final array. First item in each will be 0, 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 fairly straight forward to reuse those indexes on 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, beaten to it by 5 people!

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.