PHP - 根据原始数组中的爆炸名称创建新的数组值

PHP  - 根据原始数组中的爆炸名称创建新的数组值

问题描述:

I have array with this format:

$components = [
    [
        'name'   => 'ADIPIC ACID',
        'cas'    => '123',
        'einecs' => '321'
    ],
    [
        'name'   => 'ADIPIC ACID/DIMETHY- LAMINOHYDROXY- PROPYL DIETHYLENE- TRIAMINE COPOLYMER',
        'cas'    => '456',
        'einecs' => '654'
    ]
]

I need to find each name which has a / character, break it and create a new entry in the $components array with cas and einecs being empty string.

Also the first part of the name will have cas and einecs values from the original entry.

Expected array:

$components = [
        [
            'name'   => 'ADIPIC ACID',
            'cas'    => '123',
            'einecs' => '321'
        ],
        [
            'name'   => 'ADIPIC ACID',
            'cas'    => '456',
            'einecs' => '654'
        ],[
            'name'   => 'DIMETHY- LAMINOHYDROXY- PROPYL DIETHYLENE- TRIAMINE COPOLYMER',
            'cas'    => '',
            'einecs' => ''
        ]
    ]

How can I do this?

我有这种格式的数组: p>

  $ components =  [
 [
'name'=>  'ADIPIC ACID',
'cas'=>  '123',
'einecs'=>  '321'
],
 [
'name'=>  'ADIPIC ACID / DIMETHY-LAMINOHYDROXY-PROPYL DIETHYLENE-TRIAMINE COPOLYMER',
'cas'=>  '456',
'einecs'=>  '654'
] 
] 
  code>  pre> 
 
 

我需要找到每个 name code>,其中包含 / code> 字符,打破它并在 $ components strong>数组中创建一个新条目,其中 cas strong>且 einecs strong>为空字符串。 p> \ n

此名称的第一部分还将包含原始条目中的 cas code>和 einecs code>值。 p>

预期数组: p>

  $ components = [
 [
'name'=>  'ADIPIC ACID',
'cas'=>  '123',
'einecs'=>  '321'
],
 [
'name'=>  'ADIPIC ACID',
'cas'=>  '456',
'einecs'=>  '654'
],[
'name'=>  'DIMETHY-LAMINOHYDROXY-PROPYL DIETHYLENE-TRIAMINE COPOLYMER',
'cas'=>  '',
'einecs'=>  ''
] 
] 
  code>  pre> 
 
 

我该怎么做? p> div>

Quite crude I admit and it doesn't account for multiple / characters in a value but it does return the result expected.

        foreach( $components as $index=> $arr ){
            foreach( $arr as $key => $value ){
                if( $key=='name' && strstr( $value, '/' ) ){
                    list($pre,$post)=explode('/',$value);
                    $components[$index][$key]=$pre;
                    $components[]=array('name'=>$post,'cas'=>'','einecs'=>'');
                }
            }
        }

<?php

$components = [
    [
        'name'   => 'ADIPIC ACID',
        'cas'    => '123',
        'einecs' => '321'
    ],
    [
        'name'   => 'ADIPIC ACID/DIMETHY- LAMINOHYDROXY- PROPYL DIETHYLENE- TRIAMINE COPOLYMER',
        'cas'    => '456',
        'einecs' => '654'
    ]
];

$new = [];

foreach ($components as &$component) {
    if ($items = explode('/', $component['name'])) {
        $component['name'] = array_shift($items);
        $new = array_merge($new, $items);
    }
}

foreach ($new as $item) {
    $components[] = ['name' => $item, 'cas' => '', 'einecs' => ''];
}

var_dump($components);

foreach($components as $component)
{
   if(strpos($component["name"],"/") !== false){
      $temp = explode("/",$component["name"]);
      $components[] = new array("name"=>$temp[1], "cas"=>"", "einecs"=>"");
   }
}

I would try something like this where I use the explode function using the '/' character on the name of each component. Then I'd create a new array of all the new components taking the values of the component being evaluated.

$newComponents = array();
foreach($components as $component) {
  foreach(explode('/', $component['name']) as $newComponentName) {
    $newComponents[] = array('name'   =>$newComponentName, 
                             'cas'    => $component['cas'],
                             'einecs' => $component['einecs']);
  }
}