0

I have a multidimensional PHP array looking like that:

[14]=>
  array(2) {
    [0]=>
    int(29)
    [1]=>
    int(129)
  }

[193]=>
  array(3) {
    [0]=>
    int(6009231)
    [1]=>
    int(6324415)
    [2]=>
    int(5682922)
  }

EDIT: there can be more than 2 keys (here 14 and 193). There can be n keys.

My goal is to get the following strings in a new array:

14:29;193:6009231
14:29;193:6324415
14:29;193:5682922
14:129;193:6009231
14:129;193:6324415
14:129;193:5682922

The conditions are that the key, values are separated by : and the elements are separated by ; The first element should always be the first key (14) then we go through the values of this first key. The second element is always the second key, and again we go through the values of the second element. EDIT: the keys are always sorted ascending

I manage to get all possible combinations key:values in the desired format as follow:

$properties_values_combinations = []
foreach ($myarray as $property) {
    foreach ($property as $value) {
        $properties_values_combinations[] = (string)$property.":".(string)$value;
    }
}

However how can I combine elements of this array according to the result I want to achieve ?

3
  • your array will contain only 2 sub-arrays? or multiple sub-arrays can be there? if yes, then how's you know that through which index you have to start??? Commented Nov 1, 2019 at 5:59
  • yes only 2 sub array. The first level represent the property, the second level the value ( myarray[property][value]) Commented Nov 1, 2019 at 6:00
  • sorry I misunderstood what you mean by 2 sub array. If you mean the 2 are 14 and 193, then no, there can be n sub array. The index you start with is the smallest. It can be sorted ascending. Commented Nov 1, 2019 at 6:08

1 Answer 1

2

You can extract the keys of the array using array_keys and then iterate over each of the subarrays using those keys:

$result = array();
list($k1, $k2) = array_keys($myArray);
foreach ($myArray[$k1] as $v1) {
    foreach ($myArray[$k2] as $v2) {
        $result[] = "$k1:$v1;$k2:$v2";
    }
}
print_r($result);

Output:

Array
(
    [0] => 14:29;193:6009231
    [1] => 14:29;193:6324415
    [2] => 14:29;193:5682922
    [3] => 14:129;193:6009231
    [4] => 14:129;193:6324415
    [5] => 14:129;193:5682922
)

Demo on 3v4l.org

Update

If there can be more than 2 sub arrays, the problem needs to be solved using recursion. This function will do what you want:

function list_values($array) {
    $output = array();
    $k1 = array_keys($array)[0];
    if (count($array) == 1) {
        foreach ($array[$k1] as $v1) {
            $output[] = "$k1:$v1";
        }
    }
    else {
        foreach ($array[$k1] as $v1) {
            foreach (list_values(array_slice($array, 1, null, true)) as $k2v2) {
                $output[] = "$k1:$v1;$k2v2";
            }
        }
    }
    return $output;
}

Demo with 4 entry array at 3v4l.org. Output too long to show here.

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

5 Comments

Thanks Nick, that looks neat. However how would I do if I had a 3rd key,, or n keys ?
@VincentTeyssier hadn't seen that comment when I answered. What would be your expected output for 3 keys?
Let's say the 3rd key would be 256:3 and let's take a 4th key for example that would be 444:56, then the first value output would be 14:29;193:6009231;256:3;444:56
@VincentTeyssier see my edit. It includes a recursive function that will deal with any number of sub-arrays.
Genius! That's brilliant! Exactly what I need to achieve, thanks a lot mate

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.