3

I need to create multidimensional array from input form, because I would like pass information to mysql. I created it, but how can I make it associative one? My input:

echo "<input type='number' name='hours_to_save[$my_date][$project[$i]]'/>"

This is how it print now:

array(30) {
    ["2017-05-01"]=>
        array(3) {
            ["kompan"]    => string(1) "2"
            ["5 ogrodow"] => string(1) "3"
            ["karolowo"]  => string(1) "4"
    },
    ...
}

But I would like like this:

array(30) {
  ["2017-05-01"]=>
        array(3) {
            ["project"] => "kompan" , ["hours"] => string(1) "2"

            ["project"] =>"5 ogrodow" , ["hours"] => string(1) "3"

            ["project"] => "karolowo" , ["hours"] => string(1) "4"
    },
    ...
}
2
  • You have a key pointing to a key pointing to an array as your desired output, no one can create this array structure for you because it is impossible: ["date"] => "2017-05-01"=> array() Commented May 14, 2017 at 6:29
  • @mickmackusa of course, you're right. It can be like this: ` array(30) { ["2017-05-01"] => array(3) { ["project"] => "kompan" ... ` Commented May 14, 2017 at 7:39

2 Answers 2

1

Input:

$input=array(
    "2017-05-01"=>["kompan"=>"2","5 ogrodow"=>"3","karolowo"=>"4"],
    "2017-05-02"=>["kompan"=>"5","5 ogrodow"=>"6","karolowo"=>"7"]    
);

Method #1 (foreach() x2 *recommended)

foreach($input as $d=>$a){
    foreach($a as $k=>$v){
        $result[$d][]=['project'=>$k,'hours'=>$v];
    }
}
var_export($result);

This means:

$result                         // declare output array
       [$d]                     // use the date values as keys
           []=                  // autoincrement the keys in the subarray
              ['project'=>$k,   // each subarray's 1st element will use 'project' as the key and the input subarray's key as its value
               'hours'=>$v];    // each subarray's 2nd element will use 'hours' as the key and the input subarray's value as its value

Method #2 (foreach() & array_map()-array_keys() *purely for demonstration)

foreach($input as $k=>$a){
    $result[$k]=array_map(function($a,$b){return ['project'=>$a,'hours'=>$b];},array_keys($input[$k]),$input[$k]);
}
var_export($result);

Output (regardless of method):

array (
  '2017-05-01' => 
  array (
    0 => 
    array (
      'project' => 'kompan',
      'hours' => '2',
    ),
    1 => 
    array (
      'project' => '5 ogrodow',
      'hours' => '3',
    ),
    2 => 
    array (
      'project' => 'karolowo',
      'hours' => '4',
    ),
  ),
  '2017-05-02' => 
  array (
    0 => 
    array (
      'project' => 'kompan',
      'hours' => '5',
    ),
    1 => 
    array (
      'project' => '5 ogrodow',
      'hours' => '6',
    ),
    2 => 
    array (
      'project' => 'karolowo',
      'hours' => '7',
    ),
  ),
)
Sign up to request clarification or add additional context in comments.

5 Comments

Sorry for the code only answer, I'll be back asap to offer a full explanation, and probably check for refinements / alternative methods. (ran out of time -- Mother's Day and all!)
Perfect! It works. It's too complicated for me, this is why I didn't sort it out. If you can provide any explanation, I will be very grateful.
@WojciechB. I'll come back when I wake in the morning and write a simpler version with two loops. I'll explain everything.
@WojciechB. Simple answer and explanation provided. Comment to me if you have any questions or trouble with this.
Thank you mate! It's clear now for me. Now, I believe that helpful people exist on forums.
0

You can use foreach loop for insert to mysql. Foreach will continue to end of the arrays.

$newArr = [];
$i=0;
foreach ($yourArray as $key=>$val) {
   $newArr['date'] = $key;
   foreach ($val as $dKey=>$row) {
      $newArr['date'][$i]['project'] = $dKey;
      $newArr['date'][$i]['hours'] = $row;
      $i++;
   }
}

1 Comment

Thank you for fast reply, but I have error: Fatal error: Cannot use string offset as an array in C:\xampp\htdocs\aplikacja\gra.php on line 120 My 120 line is this line : $newArr['date'][$i]['project'] = $dKey;

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.