0

I'm having an array key-value pair. I need to create new array from this key and value pair. for eg

I tried with foreach loop

    foreach($array as $key => $val){
// here i m getting key and value i want to combine key and value in single array
    }

Array
(
'146' => Array
    (
        'sam' => Array (
                'dex',
                'max'
        )
    )
'143' => Array
    (
         'tim' => Array (
                'thai',
                'josh'
        )           
    )
 )

and the expected output is push key as first element

$out = [
    [ 'sam', 'dex', 'max'],
    [  'tim','thai', 'josh']
];
4
  • Will it always just be 2 levels deep? Commented Jun 4, 2019 at 13:42
  • 1
    @vivek_23 nope. just for eg I have written Commented Jun 4, 2019 at 13:45
  • 1
    Where are the 146 and 143 gone? shouldn't it be [146,sam,dex,max],[... ? Commented Jun 4, 2019 at 13:53
  • @sGig So, it's just 2 levels deep? Commented Jun 4, 2019 at 14:18

3 Answers 3

2

You can use array-merge as:

foreach($array as $key => $val)
    $out[] = array_merge([$key], $val);

Notice that in your example you also have another level of keys ("146", "143") -> you need to remove it before using this code.

Edited:

foreach($arr as $val) {
    $key = key($val);
    $out[] = array_merge([$key], $val[$key]);
}

Live example: 3v4l

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

Comments

0

What about this?

$output = [];
foreach($array as $key => $val) {
    $data = $val;
    array_unshift($data, $key);
    $output[] = $data;
}

2 Comments

Have you tried this before posting, its not as per expected o/p
@sGig - you indicated you wanted to combine $key and $val from your example foreach loop into a single array - this does that (and so does @dWinder's answer)
0
  • So, we need to flatten the array for each key in your array.
  • We make a function which recursively calls the further deep arrays and we collect all these results in a new array which is passed as a second parameter.

Code:

<?php

$arr = Array(
'146' => Array
    (
        'sam' => Array (
                'dex',
                'max'
        )
    ),
'143' => Array
    (
         'tim' => Array (
                'thai',
                'josh'
        )           
    )
 );


$result = [];

foreach($arr as $key => $value){
    $flat_data = [];
    flattenArray($value,$flat_data);
    $result[] = $flat_data;
}


function flattenArray($data,&$flat_data){
    foreach ($data as $key => $value) {
        if(is_array($value)){
            $flat_data[] = $key;
            flattenArray($value,$flat_data);
        }else{
            $flat_data[] = $value;
        }   
    }
}

print_r($result);

Demo: https://3v4l.org/bX9R3

  • Since, we are passing a new array to collect the results as second parameter, this would perform better than returning an array of results on each function call and then doing an array merge(which would have made a bit inefficient).

  • This would work independent of the depth of levels.

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.