0
array (size=10)
  'image' => 
    array (size=3)
      0 => string 'BlackLingerie(42).jpg' (length=21)
      1 => string 'BlackLingerie(43).jpg' (length=21)
      2 => string 'BlackLingerie(44).jpg' (length=21)
  'text' => 
    array (size=3)
      0 => string '' (length=0)
      1 => string '' (length=0)
      2 => string '' (length=0)
  'author' => 
    array (size=3)
      0 => string '' (length=0)
      1 => string '' (length=0)
      2 => string '' (length=0)
  'date' => 
    array (size=3)
      0 => string '' (length=0)
      1 => string '' (length=0)
      2 => string '' (length=0)
  'verImage' => 
    array (size=3)
      0 => string 'upload' (length=6)
      1 => string 'upload' (length=6)
      2 => string 'upload' (length=6)
  'imagePicsPath' => 
    array (size=3)
      0 => string 'http://127.0.0.1/develop/mvc/public/images/pics/BlackLingerie(42).jpg'/' (length=77)
      1 => string 'http://127.0.0.1/develop/mvc/public/images/pics/BlackLingerie(43).jpg'/' (length=77)
      2 => string 'http://127.0.0.1/develop/mvc/public/images/pics/BlackLingerie(44).jpg'/' (length=77)
  'imageThumbPath' => 
    array (size=3)
      0 => string 'http://127.0.0.1/develop/mvc/public/images/thumbs/BlackLingerie(42).jpg'/' (length=79)
      1 => string 'http://127.0.0.1/develop/mvc/public/images/thumbs/BlackLingerie(43).jpg'/' (length=79)
      2 => string 'http://127.0.0.1/develop/mvc/public/images/thumbs/BlackLingerie(44).jpg'/' (length=79)
  'imagePath' => 
    array (size=3)
      0 => string 'http://127.0.0.1/develop/mvc/public/images/pics/BlackLingerie(42).jpg'/' (length=77)
      1 => string 'http://127.0.0.1/develop/mvc/public/images/pics/BlackLingerie(43).jpg'/' (length=77)
      2 => string 'http://127.0.0.1/develop/mvc/public/images/pics/BlackLingerie(44).jpg'/' (length=77)
  'imageID' => 
    array (size=3)
      0 => string '0' (length=1)
      1 => string '1' (length=1)
      2 => string '2' (length=1)
  'submitUploadImages' => string 'Ladda upp bilder till databas' (length=29)

Want to rebuild this array to an more useful array. Like this

array
   ( [image0] (
       'name' => 
       'text' => 
       'author' => 
       'date' => 
       'verImage' => 
       'imagePicsPath' => 
       'imageThumbPath' => 
       'imagePath' => 
       'imageID' => 
      )
      [image1] (
       'name' => 
       'text' => 
       'author' => 
       'date' => 
       'verImage' => 
       'imagePicsPath' => 
       'imageThumbPath' => 
       'imagePath' => 
       'imageID' => 
      )

And so on depending on how many pictures there is, the keys inside the image array holds the values for each image. Like name, path so on. The incoming array is a $_POST that holds multiple form input data. Need some help to crack this one guys. Need to iterate trough the $_POST array, get the contents and transform to a new array ?

I want unique image arrays that holds the image information before doing my stuff with the database =)

3 Answers 3

2

I haven't tested this but it should work:

$incomingArray = $_POST['array'];
$sortedArray = array();

for($i = 0; $i < count($incomingArray); $i++){
    foreach($incomingArray as $key => $value){
        $sortedArray["image".$i][$key] = $value[i];
    }
}

Doing it this way means you don't have to write $sortedArray["image".$i]['NAME'] = $incomingArray['NAME'][$i] for each image value (name, test, author etc.).

Sign up to request clarification or add additional context in comments.

2 Comments

for($i = 0; $i < count($incomingArray['imageID']); $i++){ foreach ($incomingArray as $key => $value) { $sortedArray["image".$i][$key] = $value[$i]; } } var_dump($sortedArray);
This solved my problem ! Thanks. Some spelling errors in your example but okej =)
2

Try

foreach( $array as $1st_level_key => $1st_level_value ) {
    foreach ( $1st_level_value as $2nd_level_key => $2nd_level_value ) {
        $new_array['image'.$2nd_level_key][$1st_level_key] = $2nd_level_value;
    }
}

Comments

0

Short answer yes, pretty much like this:

for($i = 0; $i < count($YourArray); $i++)
{
    $NewArray["image".$i]["name"] = $YourArray["name"][$i];
    ...
}

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.