Dear gurus, help me figure it out; I don’t understand how the algorithm will make up: there is an array:

$massive = array( '0' => array('propertyname' => 'цвет','value' => 'red'), '1' => array('propertyname' => 'версия', 'value' => 'android') ); 

From the $ massive array you need to collect this:

  $result = array( 'цвет' => array('value' => 'red'), 'версия' => array('value' => 'android') ); 

that is, it will separate the propertyname into a separate array in the foreach loop in the $ massive array there may be other parameters, for example:

 '2' => array('propertyname' => 'память', 'value' => '16') 

and of course it will add this to $ result as a separate parameter:

 'память' => array('value' => '16') 

I hope clearly explained.

  • Well, it is logical that we run through the cycle on one array and assemble as needed in the other - Aleksey Shimansky

2 answers 2

 $result = array(); foreach ($massive as $elem) { $result[$elem['propertyname']] = array( 'value' => $elem['value'] ); } 
  • Have you worked? - ultimatum
  • Thanks helped. It is a pity that I do not really know how to build an algorithm. - mega94
  • Yes, it worked. You do not have? - Kernel Panic
  • @ mega94 there is no algorithm and there is nothing to build here. Just need to think and do. The task is the simplest. - ilyaplot
  • ok then, I didn’t unfold - ultimatum

It's very simple

 <?php $massive = [ '0' => ['propertyname' => 'color','value' => 'red'], '1' => ['propertyname' => 'version', 'value' => 'android'] ]; $result = []; foreach ($massive as $item) { $result[$item['propertyname']] = $item; unset($result[$item['propertyname']]['propertyname']); } print_r($result); /** Array ( [color] => Array ( [value] => red ) [version] => Array ( [value] => android ) ) */