1

I'm currently trying the following:

I have multiple arrays defined. They all are filled by fetching data from the database, so they all contain the same columns/data structure, but with different data. For example, lets say the arrays are different schools, so there is:

//pseudoCode:
array1 = ({"Name: Peter", "Surname: not peter"},{"Name: doe", "Surname: john"});
array2 = ({"Name: asfwe", "Surname: qwfqwf"},{"Name: asfas", "Surname: fsbng"});
array3 = ({"Name: weqw", "Surname: wqeqewqw"},{"Name: doqweqwee", "Surname: wewe"});

Now, for all these arrays, I want to do the same things. In my case, I have multiple if else cases, checking the length of the array and doing some stuff.

So far I'm only doing it for array 1 though. Now my first idea was to simply copy the logic and refactor all variable names to array2 respectively to array3, but this wouldn't make sense, because in my real case, its 10 arrays instead of 3 and the logic is about 150 lines of code, so I would have a lot of duplicate code and would need to change it everywhere, if something in the logic changes.

Now the question is: How can I do the same procedure for every array?

So what I would need is something like:

//pseudoCode again
foreach(array in array1, array2, array3, array4, array5,....){
//do something with variable "array", which is actually one of the defined arrays
}

A hint in the right direction would be great.

Thank you in advance.

7
  • Using array_merge() ? Commented May 28, 2019 at 19:57
  • 2
    export all your logic to function that encapsulate your array modification? Commented May 28, 2019 at 19:58
  • @AymDev I still need to be able to differ between the arrays... Also I don't know the lengths of each array, only the structure Commented May 28, 2019 at 19:58
  • Then like @dWinder said, use a function. Commented May 28, 2019 at 20:00
  • 2
    foreach ($all_your_arrays as $arr) { do_stuff_to_process_array($arr); }? Basically, just add [] around your array1, array2...arrayN in your pseudocode to group them all in an array to create $all_your_arrays. Commented May 28, 2019 at 20:03

1 Answer 1

1

Procedure code demand exporting logic to function:

function foo($array) {
    // modify the array or do what ever you need
    return $array; // in case it has been modify
}

Now can you function as:

foreach($arrays as &$arr)
    $arr = foo($arr); // calling the function on each array
    // the re-assign is just in case it has been modify

In this example $arrays stand for array with all your inner arrays as:

$arrays = array($array1, $array2, $array3, ...) 

if your array as getting dynamicly from some other function / SQL code do:

$arrays = []; // init empty array
while (@SOME_CONDITION@) {
    $arrays[] = getAnotherArrayFunction(); // append the array to your array
}
Sign up to request clarification or add additional context in comments.

3 Comments

oh man, I'm stupid, if this works, then it's really simpler then I thought.. The question is, how many dimensions can PHP handle? Would be be a problem, to access one item from $array1 (lets say the surname of peter) by then calling $arrays[0][0]["Surname"]? This would work right?
Yes. PHP can handle multi-dimension array with a lot of deeper level. and yes - you can access them as $arrays[0][0]["Surname"]
alright, thank you very much! :) Basically I was just missing the part to assign all arrays to one big array, so I didn't know how to work with the function for every array...

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.