1

so for example i have something like

array:2 [▼
  "old" => array:2 [▼
    "uk" => "unk"
    "en" => "eng"
    "fr" => "fre"
  ]
  "new" => array:2 [▼
    "uk" => "united kingdom"
    "en" => "english"
    "fr" => "french"
  ]
]

atm i can only get one item according to a predefined key

foreach ($data as $status => $value) {
    $str .= $value['uk'];
}

which give

unk    united kingdom

instead i want to get all of them, so the result would be

unk    united kingdom
eng    english
fre    french

which could be translated into an array or combined values

array:3 [▼
  "uk" => [
    "unik",
    "united kingdom"
  ],
  "en" => [
    "eng",
    "english"
  ],
  "fr" => [
    "fren",
    "french"
  ]
]

so what's the best way to achive that ?

3
  • $code is an array or a string? Commented Oct 23, 2017 at 9:45
  • @hassan a string Commented Oct 23, 2017 at 9:56
  • @KrisRoofe not sure what u r after., $code is predefined, i updated the post to avoid confusion. Commented Oct 23, 2017 at 10:01

2 Answers 2

2

you may go with array_walk_recursive as follows:

$output = [];
array_walk_recursive($arr, function ($value, $key) use (&$output) {
    $output[$key][] = $value;
});

live example: https://3v4l.org/aKYsG .

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

1 Comment

focken legend, thanx
1

You just need to flatten the sub-arrays :

$output = array();
foreach ($data as $status => $codeArray) {
   foreach ($codeArray as $code=>$value){
     $output[$code][]=$value;
   }
}

1 Comment

awesome, thanx.

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.