0

I have a lot of record (element) in an array like this, basically from query with union all 'keyword' :

Array
(
[0] => Array
    (
        [id] => 16
        [no_surat] => E1679
        [level] => B
    )

[1] => Array
    (
        [id] => 16
        [no_surat] => E1679
        [level] => C
    )

[2] => Array
    (
        [id] => 169
        [no_surat] => E1736
        [level] => B
    )
)

I need to get those element into format like this :

Array
(
[16] => E1679 - B
[16] => E1679 - C
[169] => E1736 - B
)

So, I try to use array_map function like this :

$listNoEstimate = array_map(function ($insert) {
    return array(
        $insert['id'] => $insert['no_surat'] . ' - ' . $insert ['level']
    );
}, $listNoEstimate);

The result is like this :

Array
(
[0] => Array
    (
        [16] => E1679 - B
    )

[1] => Array
    (
        [16] => E1679 - C
    )

[2] => Array
    (
        [169] => E1736 - B
    )
)

Please advise

1
  • 4
    FYI: you can't have duplicate keys in your array, your desired results can't be achieved Commented Jun 20, 2017 at 4:53

2 Answers 2

2

This is not possible anyway,

Instead you can do something like this,

$result = [];
foreach($listNoEstimate as $k => $v){
     $result[$v['id']][] = $v['no_surat'].' - '.$v['level'];    
}

I hope this will help.

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

1 Comment

Okey, Seems I can use it to create optgroup
0

There is no direct way to do this thing directly, although you can do it like this

for($i=0;$i<count($listNoEstimate);$i++){
    $j=0;
    foreach($listNoEstimate[$i] as $key => $value){
        $temp[$j]=$value;
        $j++;
    }
    $listNoEstimate[$temp[0]]=$temp[1]-$temp[2];
}

Note: This method work properly, if your [id] => 16 is unique. Otherwise it will replace previous data.

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.