Dipesh Parmar
Dipesh Parmar

Reputation: 27364

convert array into key value pairs

I have following array,

Array
(
    [Char100_1] => Array
        (
            [0] => Array
                (
                    [Char100_1] => Mr S Kumar
                )

            [1] => Array
                (
                    [Char100_1] => Mr S Kumar2
                )
        )

    [Char100_13] => Array
        (
            [0] => Array
                (
                    [Char100_13] => 159.9
                )

            [1] => Array
                (
                    [Char100_13] => 119.9
                )
        )

    [Char100_14] => Array
        (
            [0] => Array
                (
                    [Char100_14] => 191.88
                )

            [1] => Array
                (
                    [Char100_14] => 143.88
                )
        )
)

which is created dynamically from a database query result and some loops.

Now I wanted to convert this array into something like below,

Array
(
    [0] => Array
    (
        [Char100_1] => Mr S Kumar
        [Char100_13] => 159.9
        [Char100_14] => 191.88
    )
    [1] => Array
    (
        [Char100_1] => Mr S Kumar2
        [Char100_13] => 119.9
        [Char100_14] => 143.88
    )
)

I have tried looping through them but its not working.

<?php
    /* database process to create array */
    $contentArray = array();
    foreach($newData['DataField'] as $ndata) :
        $responsedata = getAppContent($appid, $ndata);
        while($haveresult = mysql_fetch_assoc($responsedata))
        {
             $contentArray[$ndata][] = $haveresult;
        }
    endforeach;

    /* for getting resulting array start */
    $newdataArray = array();

    foreach($contentArray as $field => $value):
        $newdataArray[$field] = array();
        foreach( $value as $val ) :
             $newdataArray[$field] = $val;
        endforeach;

    endforeach;
?>

Upvotes: 1

Views: 1393

Answers (3)

Ryan Vincent
Ryan Vincent

Reputation: 4513

I observe that you are transposing the arrays. i.e all the zero subscript values together and all the one subscript values together.

Therefore your outer subscript should be the '0' and '1'. These are available in the inner loop. So, the inner loop index becomes the outer array index. And the inner loop value, which is an array, you need to take the 'current' value of.

/* for getting resulting array start (PHP 5.3.18) */
$newdataArray = array();

foreach($contentArray as $field => $value):
    foreach( $value as $idx => $val ): // $idx takes value 0 or 1. $val is an array
      $newdataArray[$idx][$field] = current($val);
    endforeach;

endforeach;
print_r($newdataArray);

Upvotes: 1

billyonecan
billyonecan

Reputation: 20250

If you can't change the query (as suggested in the comments), then the following should work:

$output = array();
foreach ($array as $a) {
  foreach ($a as $k => $b) {
    if (empty($output[$k])) {
      $output[$k] = array();
    }
    $output[$k] += $b;
  }
}

Upvotes: 1

Kevin
Kevin

Reputation: 204

As long as all of your arrays have the same amount of values containing them a for loop will do:

$NewDataArray = array();

for ($a = 0; $a < $Numberofvaluesineacharray; $a++){
$NewDataArray[$a] = $NewDataArray[$array1[$a], $array2[$a],.....arrayn[$a];
}

Upvotes: 0

Related Questions