Jump to content

Recommended Posts

An array comes in as this from a $_POST

 

Array
(
[fieldOne] => Array
(
[row1] => data1
[row2] => data2
[row3] => data3
[row4] => data4
[row5] => data5
)

[fieldTwo] => Array
(
[row1] => data1
[row2] => data2
[row3] => data3
[row4] => data4
[row5] => data5
)

[fieldThree] => Array
(
[row1] => data1
[row2] => data2
[row3] => data3
[row4] => data4
[row5] => data5
)

[fieldFour] => Array
(
[row1] => data1
[row2] => data2
[row3] => data3
[row4] => data4
[row5] => data5
)

)

 

 

I need to take row1, row2, row3, row4, row5 of each child array and push it into a new array using the keyname of the parent array as the keyname for each element in the new array

 

The example below will show you the exact output I need

 

[0] => Array
(
[fieldOne] => data1
[fieldTwo] => data1
[fieldThree] => data1
[fieldFour] => data1
)

[1] => Array
(
[fieldOne] => data2
[fieldTwo] => data2
[fieldThree] => data2
[fieldFour] => data2
)

[2] => Array
(
[fieldOne] => data3
[fieldTwo] => data3
[fieldThree] => data3
[fieldFour] => data3
)

[3] => Array
(
[fieldOne] => data4
[fieldTwo] => data4
[fieldThree] => data4
[fieldFour] => data4
)

[4] => Array
(
[fieldOne] => data5
[fieldTwo] => data5
[fieldThree] => data5
[fieldFour] => data5
)

 

I dont think there is a function to do this, may requre some foreach stuff but Im getting no where

 

Can anyone help?

Link to comment
https://forums.phpfreaks.com/topic/166721-solved-array/
Share on other sites

 

 

$newArr  = array();
foreach($array as $key => $val)
{	foreach($val as $vKey => $vVal)
{	$found = false;
	foreach($newArr as $nKey => &$nVal)
	{	if(in_array($vVal, $nVal))
		{ $nVal[$key] = $vVal;
			$found = true;
		}
	}
	if(!$found)
	{ $newArr[] = array($key => $vVal);
	}
}
}
print_r($newArr);

Link to comment
https://forums.phpfreaks.com/topic/166721-solved-array/#findComment-879417
Share on other sites

Actually, sorry. That does not work

 

Where I have used this dummy data for the example

 

data1

data2

data3

data4

 

Those values can be anything, but you have based your code on them being hard set as data*

 

 

Here is what the $_POST could come in as

 

 

Array
(
[fieldOne] => Array
(
[row1] => cheese
[row2] => red
[row3] => rain
)

[fieldTwo] => Array
(
[row1] => beans
[row2] => blue
[row3] => snow
)

)

 

Which I need converted to

 

 

[0] => Array
(
[fieldOne] => cheese
[fieldTwo] => beans
)

[1] => Array
(
[fieldOne] => red
[fieldTwo] => blue
)

[2] => Array
(
[fieldOne] => rain
[fieldTwo] => snow
)

 

 

So basically the [row1]'s must all be merged together in a new array, all the [row2]'s in another and so on... whilst using the original keyname of the array as each elements key name.

 

The example above shows exactly what is needed

Link to comment
https://forums.phpfreaks.com/topic/166721-solved-array/#findComment-879964
Share on other sites

<pre><?php
// Test array2:
$array = array( 
		'fieldOne' => array(
			'row1' => 'cheese',
			'row2' => 'red',
			'row3' => 'rain',
		),
		'fieldTwo' => array(
			'row1' => 'beans',
			'row2' => 'blue',
			'row3' => 'snow',
		),
	);


// actual script:
foreach($array as $key=>$sub) {
$i = 0;
foreach($sub as $value) {
	$final[$i][$key] = $value;
	$i++;
}
}
// end

// print results:
print_r($array);
print_r($final);
?></pre>

 

Outputs:

Array
(
    [fieldOne] => Array
        (
            [row1] => cheese
            [row2] => red
            [row3] => rain
        )

    [fieldTwo] => Array
        (
            [row1] => beans
            [row2] => blue
            [row3] => snow
        )

)
Array
(
    [0] => Array
        (
            [fieldOne] => cheese
            [fieldTwo] => beans
        )

    [1] => Array
        (
            [fieldOne] => red
            [fieldTwo] => blue
        )

    [2] => Array
        (
            [fieldOne] => rain
            [fieldTwo] => snow
        )

)

Link to comment
https://forums.phpfreaks.com/topic/166721-solved-array/#findComment-879977
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.