PHP-基于原始数组中的分解名称创建新数组值

PHP-基于原始数组中的分解名称创建新数组值,php,arrays,Php,Arrays,我有以下格式的数组: $components = [ [ 'name' => 'ADIPIC ACID', 'cas' => '123', 'einecs' => '321' ], [ 'name' => 'ADIPIC ACID/DIMETHY- LAMINOHYDROXY- PROPYL DIETHYLENE- TRIAMINE COPOLYMER',

我有以下格式的数组:

$components = [
    [
        'name'   => 'ADIPIC ACID',
        'cas'    => '123',
        'einecs' => '321'
    ],
    [
        'name'   => 'ADIPIC ACID/DIMETHY- LAMINOHYDROXY- PROPYL DIETHYLENE- TRIAMINE COPOLYMER',
        'cas'    => '456',
        'einecs' => '654'
    ]
]
我需要找到每个具有
/
字符的
名称,将其打断并在$components数组中创建一个新条目,其中caseinecs为空字符串

此外,名称的第一部分将包含原始条目中的
cas
einecs

预期阵列:

$components = [
        [
            'name'   => 'ADIPIC ACID',
            'cas'    => '123',
            'einecs' => '321'
        ],
        [
            'name'   => 'ADIPIC ACID',
            'cas'    => '456',
            'einecs' => '654'
        ],[
            'name'   => 'DIMETHY- LAMINOHYDROXY- PROPYL DIETHYLENE- TRIAMINE COPOLYMER',
            '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 $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'=>'');
                }
            }
        }

我承认这是非常粗糙的,它不能解释一个值中有多个
/
字符,但它确实返回了预期的结果

        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'=>'');
                }
            }
        }

我会尝试这样的方法,在每个组件的名称上使用“/”字符,使用
explode
函数。然后,我将创建一个包含所有新组件的新数组,其中包含正在计算的组件的值

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

我会尝试这样的方法,在每个组件的名称上使用“/”字符,使用
explode
函数。然后,我将创建一个包含所有新组件的新数组,其中包含正在计算的组件的值

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

您期望的结果是什么?我需要在数据库中插入所有组件。在本例中,name可以有多个组件,我需要所有组件。我认为他要求您生成您期望的示例数组,以便我们进行比较。目前有点不清楚你想要什么。根据你给定的数组,给我们你期望的数组。添加了期望的结果。你期望的结果是什么?我需要在数据库中插入所有组件。在本例中,name可以有多个组件,我需要所有组件。我认为他要求您生成您期望的示例数组,以便我们进行比较。目前有点不清楚你想要什么。根据你给定的数组,给我们你期望的数组。添加了期望的结果。