0

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?

5
  • 1
    what is your expected result? Commented Nov 13, 2015 at 18:02
  • I need to insert all the components in the db. In this case name can have multiple components, and I need them all. Commented Nov 13, 2015 at 18:03
  • I think what he is asking is for you to produce what you expect the example array to look like so that we can compare. It's currently a little bit unclear what you are asking for. Commented Nov 13, 2015 at 18:04
  • base on your given array, give us your expected array. Commented Nov 13, 2015 at 18:05
  • Expected result added. Commented Nov 13, 2015 at 18:07

4 Answers 4

1

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'=>'');
                }
            }
        }
Sign up to request clarification or add additional context in comments.

Comments

1
<?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);

Comments

1

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

Comments

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

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.