rearrange a multidimensional array

hi,

i have a  $_POST array like so

array(6) {
["same_info"]=> string(3) "yes"
["image_name"]=> array(2) { [0]=> string(12) "gopr0012.jpg" [1]=> string(9) "photo.jpg" }
["image_location"]=> array(2) { [0]=> string(4) "calgary" [1]=> string(0) "canada" }
["image_fav"]=> array(2) { [0]=> string(2) "no" [1]=> string(0) "yes" }
["image_desc"]=> array(2) { [0]=> string(0) "description1" [1]=> string(0) "description2" }
["image_submit"]=> string(12) "Image Submit"
 }

essentially this array is used to verify image data so a user can change the name of files and update some data i would like to rearrange the array so that it puts all of the relevant data is contained within one array for each file

for the array above it would like to arrange it like so

array(1) {
["same_info"]=> string(3) "yes"
}
array(4) {
["image_name"]=> string(12) "gopr0012.jpg"
["image_location"]=> string(0) "calgary"
["image_fav"]=> string(0) "no"
["image_desc"]=> string(0) "description1"
}

array(4) {
["image_name"]=> string(12) "photo.jpg"
["image_location"]=> string(0) "canada"
["image_fav"]=> string(0) "yes"
["image_desc"]=> string(0) "description2"
}
array(1){
["image_submit"]=> string(12) "Image Submit"
}

the image_submit and same_info key only need to contain one piece of data while there could be a lot of the middle arrays containing the rearranged file data

thanks a lot for your help
LVL 6
J NUnicorn wranglerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
I started trying to copy the information in the question and reformat it for use in a test case, but it appears to be bogus.  How is it that we got a var_dump() output with this?

string(4) "calgary"

Obviously, "calgary" is a 7-character string.  If you can post some of the real test data, along with the expected output (the SSCCE) I'll be glad to try to help.

Best regards, ~Ray
0
Ray PaseurCommented:
Actually, nevermind about the test data.  I've got some that might be OK.  But I gotta say, this sounds like it might make sense to talk to a data architect about these array structures.  An array of objects might make a lot more sense than this!

<?php // RAY_temp_jaymenagy.php
error_reporting(E_ALL);

// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28230037.html

$arr = array
( "same_info"      => "yes"
, "image_name"     => array( 0 => "gopr0012.jpg",  1 => "photo.jpg"    )
, "image_location" => array( 0 => "calgary",       1 => "canada"       )
, "image_fav"      => array( 0 => "no",            1 => "yes"          )
, "image_desc"     => array( 0 => "description1",  1 => "description2" )
, "image_submit"   => "Image Submit"
)
;

// SAVE A COPY OF THE INITIAL STATE
$old = $arr;

// REMOVE STRINGS
foreach ($arr as $key => &$sub)
{
    if (is_string($sub)) unset($arr[$key]);
}

// MATRIX-INVERT ARRAYS
$cnt = 0;
while (TRUE)
{
    foreach ($arr as $key => $sub)
    {
        $new[$cnt][$key] = $sub[$cnt];
        unset($arr[$key][$cnt]);
    }
    $cnt++;
    if (empty($arr[$key][$cnt])) break;
}

// KEEP STRINGS, USE NEW ARRAYS
$cnt = 0;
foreach ($old as $key => $sub)
{
    if (is_string($sub))
    {
        $out[][$key] = $sub;
    }
    elseif (!empty($new[$cnt]))
    {
        $out[] = $new[$cnt];
        $cnt++;
    }
}

// SHOW THE WORK PRODUCT
echo '<pre>';
print_r($out);

Open in new window

HTH, ~Ray
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

J NUnicorn wranglerAuthor Commented:
Sorry about the test data i just realised that i had left it blank.

i basically just did a var_dump on the post and by mistake left some fields empty on the form so i went bank and edited it.

i dont have time to work on it right now but i will be in touch

thanks a lot!
0
J NUnicorn wranglerAuthor Commented:
THANKS RAY BIG HELP YET AGAIN!
0
Ray PaseurCommented:
Thanks for the points, ~Ray
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.