0
Array
(
    [1] => dani : developer
    [2] => john : not_developer
)

i want to split this array at ":" and create in each place another array.

expected output :

Array
    (
        [1] => array(
               "name" => "dani",
               "value" => "developer"
               )
        [2] => array(
               "name" => "john",
               "value" => "not_developer"
               )
     )

i have tried using explode(":", $attribute); but the explode wont work on array.

3
  • 2
    Use a foreach loop and explode each element. Commented Jan 17, 2022 at 18:58
  • A very similar question: stackoverflow.com/q/31335930/2943403 Commented Jul 31, 2022 at 8:26
  • I'd likely use this in a professional project: 3v4l.org/TOPro Commented Aug 10, 2022 at 12:14

2 Answers 2

1

Use array_map, with a custom callback, to map the original into what you want:

<?php

$original =[
    'dani : developer',
    'john : not_developer',
];

$converted = array_map(
    function ($element) {
        list($name, $value) = explode(':', $element);

        return [
            'name' => trim($name),
            'value' => trim($value),
        ];
    },
    $original
);

var_dump($converted);

You can use this variation that uses array_map again to do the exploding and trimming together:

<?php

$original =[
    'dani : developer',
    'john : not_developer',
];

$converted = array_map(
    function ($element) {
        list($name, $value) = array_map(
            'trim',
            explode(':', $element)
        );

        return [
            'name' => $name,
            'value' => $value,
        ];
    },
    $original
);

var_dump($converted);

Or this variation that further uses a preset array of the desired output's sub-keynames, and array_combine:

<?php

$original =[
    'dani : developer',
    'john : not_developer',
];

$conversion_keys = ['name', 'value'];

$converted = array_map(
    function ($element) use ($conversion_keys) {
        return array_combine(
            $conversion_keys,
            array_map(
                'trim',
                explode(':', $element)
            )
        );
    },
    $original
);

var_dump($converted);

They all output your desired result of:

array(2) {
  [0]=>
  array(2) {
    ["name"]=>
    string(4) "dani"
    ["value"]=>
    string(9) "developer"
  }
  [1]=>
  array(2) {
    ["name"]=>
    string(4) "john"
    ["value"]=>
    string(13) "not_developer"
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0
$attributes_array = array();
    foreach( $hebrew_attributes_array as $attribute ){
        $attribute_array = explode(":", $attribute);
        $attributes_array[] = Array(
                                    "attribute_name" =>  $attribute_array[0],
                                    "attribute_value" => $attribute_array[1] 
                                    );
    }
    // $hebrew_string = explode(":", $hebrew_string);
    return $attributes_array;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.