0

I am trying to move 2 elements in my array. When I output the current array, this is my result:

 array:["Date" => "2016-09-25"
      "emp" => "12345  "
      "work" => "coding"
      "Hours" => "6.00"
      "L" => "L"
      "IBEW" => "IBEW"
      ]

What I'm trying to achieve is to move the two values (L and IBEW) to the second and third place, like this:

array:["Date" => "2016-09-25"
      "emp" => "12345"
      "L" => "L"
      "IBEW" => "IBEW"
      "work" => "coding"
      "Hours" => "6.00"
       ]

How is this possible?

10
  • you have to change the fields in table of database Commented Oct 21, 2016 at 8:17
  • why you need it so ?? Commented Oct 21, 2016 at 8:18
  • for your expected output because arrays comes according to fields saved in table of database Commented Oct 21, 2016 at 8:19
  • "L" => "L" "IBEW" => "IBEW" these two columns are not in database this vitrual columns we are pushing in array Commented Oct 21, 2016 at 8:21
  • are you using hidden fields, are you using dynamic or static Commented Oct 21, 2016 at 8:28

2 Answers 2

2

Here you have a working and tested answer for that.

You just have to change $output to the right variable name.

foreach ($output as &$outputItem) {

  // Here we store the elements to move in an auxiliar array
  $arrayOfElementsToMove = [
      "L" => $outputItem["L"],
      "IBEW" => $outputItem["IBEW"]
  ];

  // We remove the elements of the original array
  unset($outputItem["L"]);
  unset($outputItem["IBEW"]);

  // We store the numeric position of CostCode key
  $insertionPosition = array_search("CostCode", array_keys($outputItem));

  // We increment in 1 the insertion position (to insert after CostCode)
  $insertionPosition++;

  // We build the new array with 3 parts: items before CostCode, "L and IBEW" array, and items after CostCode
  $outputItem = array_slice($outputItem, 0, $insertionPosition, true) +
          $arrayOfElementsToMove +
          array_slice($outputItem, $insertionPosition, NULL, true);
}
Sign up to request clarification or add additional context in comments.

6 Comments

@Murali Then it would be great if you edit your question to add the code of the database connection, just for us to check the method you are using and the field names.
i was attached my screen shot image. without the parent array your answer is correct but hear parent array is coming that is the pblm for me
@Murali Edited my answer. Hope it helps!
only one record your answer is woking fine.. but i have more records in your answer is taking last record
@Murali In the picture showed before you edited the question there was a main array with two associative arrays inside. That main array is my $output variable. The foreach loop goes through all the arrays inside and swap the keys to the order you wanted.
|
0
For Example Have append the 'L' AND 'IDEW' key values after the work key,

<?php

# For Example Have append the 'L' AND 'IDEW' key values after the work key
$main_array = [
"Date" => "2016-09-25",
"emp" => "12345  ",
"work" => "coding",
"Hours" => "6.00",
"L" => "L",
"IBEW" => "IBEW"
];

$split_values = array("L"=>$main_array["L"], "IBEW"=>$main_array["IBEW"]);

unset($main_array['L']);
unset($main_array['IBEW']);

$array_decide = 0;

foreach($main_array as $k=>$v){
    if ($array_decide == 0){
        $array_first[$k] = $v;
    } elseif ($array_decide == 1) {
        $array_final[$k] = $v;
    }
    if ($k=='work'){
        $array_final = array_merge($array_first,$split_values);
        $array_decide = 1;
    }
}

echo "<pre>";
print_r($array_final);
echo "</pre>";

/*OUTPUT:
Array
(
    [Date] => 2016-09-25
    [emp] => 12345  
    [work] => coding
    [L] => L
    [IBEW] => IBEW
    [Hours] => 6.00
)*/


?>

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.