3

I want to combine multiple arrays to make specific format.

$fieldArray=[];
$fieldArray['field_val']['key_1']=array('1');
$fieldArray['field_val']['key_2']=array('1','2','3','4');
$fieldArray['field_val']['key_3']=array('5','6','7','8');
$fieldArray['field_val']['key_4']=array('9','10','11','12');
$fieldArray['field_val']['key_5']=array('30');

Result Should be

1,1,5,9,30 
1,2,6,10,30 
...and so on

I have tried the following code. But it not give me the correct results.

echo '<pre>';
$i=0;
$newArray=[];
foreach($fieldArray['field_val'] as $key=>$values){
    if($i==0){
        $orderId=$values[0];
    }
    array_unshift($values,$orderId);
    $newArray[]=$values;
$i++;
}

array_shift($newArray);
array_pop($newArray);
print_r($newArray);

I need the following output.

1,1,5,9,30
1,2,6,10,30 and so on
13
  • 2
    Why the second element is 1,2,6,10,30 and not 2,6,10? Commented Jun 4, 2019 at 11:29
  • 1
    What if an array has two elements? And not one or four? If the number is either 1 or N, this is quite straightforward, but what if its 1, N-1 and N elements in the arrays? Commented Jun 4, 2019 at 11:30
  • i want to combine first element of first array with another array so that it will become 1,2,6,10,30 Commented Jun 4, 2019 at 11:31
  • @SurajKumar again the same thing -> what about 30? it will be add also everytime? Commented Jun 4, 2019 at 11:32
  • better to explain logic behind what you are trying to achieve, because it's hard for us to understand that Commented Jun 4, 2019 at 11:33

1 Answer 1

2

Once check the output of this, As I can see, you are kinda transposing array. I first transposed and wrote a snippet for your requirement.

$temp = array_map(null, ...$fieldArray['field_val']); // transposing array
foreach ($temp as $key => &$value) {
    foreach ($value as $key1 => &$value1) {
        // checking if empty
        if(empty($value1)){ 
            // fetching key1 value from first array and 
            // mapping it to empty values for all other arrays except first
            $value1 = $temp[0][$key1]; 
        }
    }
}
echo implode("\n", array_map(function ($value) { // replace with br if web
  return implode(",", $value);
}, $temp));

Demo

Note: Must have data from the initial index.

Explanation:

  1. Please see this link how I transposed it,
  2. Then I took the first array as my base array to play with other values
  3. Then I mapped with first and last values with other values with empty values.

EDIT

function transpose($array) {
    array_unshift($array, null);
    return call_user_func_array('array_map', $array);
}
$temp = transpose($fieldArray['field_val']); // transposing array

function flipDiagonally($arr) {
    $out = array();
    foreach ($arr as $key => $subarr) {
        foreach ($subarr as $subkey => $subvalue) {
            $out[$subkey][$key] = $subvalue;
        }
    }
    return $out;
}
Sign up to request clarification or add additional context in comments.

7 Comments

Thank you for your answer. Please explain the code.
Isn't inline doc enough or shall I explain more?
No problem, I will try to understand. Thank you so much for your help
I wrote some inline doc, gave some explanation list out explanation, does it solves your problem?
This problem is occur when running this code Cannot unpack array with string keys
|

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.