1

I want to remove only key from multidimensional associative array preserving its value in PHP.

I've tried with foreach loop.

foreach($collections as $k=>$v){
            foreach($v as $k1=>$v1){
                foreach($v1 as $k2=>$v2){
                    if($k2 == 'attr_id'){
                        unset($collections[$k][$k1][$k2]);
                    }
                }
            }
        }

I've an array like below:

$collections = [
            0 => [
                0 => [
                    "attr_id" => 23,
                    "value" => "Single Side"
                ],
                1 => [
                    "attr_id" => 23,
                    "value" => "Double Side"
                ],
            ],
            1 => [
                0 => [
                    "attr_id" => 31,
                    "value" => "A4"
                ],
                1 => [
                    "attr_id" => 31,
                    "value" => "12x18"
                ],
                2 => [
                    "attr_id" => 31,
                    "value" => "B5"
                ],
                3 => [
                    "attr_id" => 31,
                    "value" => "A5"
                ]
            ]
        ];

And I want output Like this:

$collections = [
            23 => [ "Single Side", "Double Side" ],
            31 => [ "A4", "12x18", "B5", "A5" ]
        ];

Please Help!

0

3 Answers 3

2

Short solution:

$expected = [];
foreach ($collections as $collection) {
    $expected[$collection[0]['attr_id']] = array_column($collection, 'value');
}
Sign up to request clarification or add additional context in comments.

1 Comment

certainly much better
1

You can do this with simply using two foreach() loop and push the value to attr_id

$expected = [];        
foreach($collections as $k=>$v){
    foreach($v as $k1=>$v1){
        $expected[$v1['attr_id']][] = $v1['value'];
    }
}
print_r($expected); 

Output:

Array (
   [23] => Array ( 
            [0] => Single Side 
            [1] => Double Side 
          ) 
   [31] => Array ( 
            [0] => A4 
            [1] => 12x18
            [2] => B5
            [3] => A5 
         )
    )

DEMO: https://3v4l.org/JlsIl

8 Comments

Can't we remove key? So the array looks like: Array([23]=>Array('Single', 'Double'))
actually there is no key like 0,1 or 0,1,2,3 but when we do print_r() it shows that
@AlwaysSunny There is always a key/index behind the scene or upfront in an array. Array does not make any sense without key/index.
@DipenChand .its a not key .its a array index .its can't be removed
@AniketSahrawat see the previous comment of prasanth I mentioned key not index
|
0

Do with array_values and array_reduce

  1. first merge all subArray's to one array
  2. The use reduce to apppend value based with attr_id matching

Sandbox demo

$res = call_user_func_array('array_merge',array_values($arr));
$res = array_reduce($res,function($a,$b){
    $a[$b['attr_id']] = isset($a[$b['attr_id']]) ? $a[$b['attr_id']] : [] ;
    array_push($a[$b['attr_id']],$b['value']);
    return $a;
},[]);
    print_r($res);

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.