2

I have an array like so:

Array (
    [0] => - :description: Rate the Code
    [1] => :long-description: ""
    [2] => :points: !float 5
)

I would like to use PHP to change my array structure to look like this:

Array (
    [- :description] => Rate the Code
    [:long-description] => ""
    [:points] => !float 5
)

Can anybody help me out with this? Here is what I have for code so far:

for ($j = 0; $j < sizeof($array[$i]); $j++) {
    $pieces = explode(": ", $array[$i][$j]);

    $key = $pieces[0];
    $value = $pieces[1];

    $array[$i][$j] = $array[$i][$key];
}

This code throws an Undefined index: - :description error for all of my indexes. The - :description changes in each error to the index that it is on however.

1
  • Also, if you end up keeping the for loop, keep in mind that the conditional ($j < sizeof($array[$i])) is evaluated every iteration. To be much more efficient, you would evaluate the size of directly before the loop and then reference a new variable ($size) in the conditional. Commented Aug 22, 2014 at 20:40

2 Answers 2

4

You were very close, try this:

$initial = array(
    '- :description: Rate the Code',
    ':long-description: ""',
    ':points: !float 5'
);

$final = array();
foreach($initial as $value) {
    list($key, $value) = explode(": ", $value);
    $final[$key] = $value;
}

print_r($final);
// Array
// (
//     [- :description] => Rate the Code
//     [:long-description] => ""
//     [:points] => !float 5
// )

The big problem came in your attempt to modify the current array. This will prove more difficult than it is worth, when you can just create a new array and set the key/value combos based on the exploded value from the initial array. Also, notice my shortcut with the use of list(). Here is another example:

$array = array('foo', 'bar');

// this
list($foo, $bar) = $array;

// is the same as 
$foo = $array[0];
$bar = $array[1];
Sign up to request clarification or add additional context in comments.

3 Comments

Do you have an example that modifies the old array instead of creating a new one? And also, could this work using a for() instead?
@Nicolas I can do that, yes. May I ask why you want to use a for loop instead of a foreach loop? Unless you plan to jump around within the array, a foreach loop is designed to loop through an array from beginning to end.
$key comes from the list() statement, which I tried to describe down below. list() "assign variables as if they were an array". So list($key, $value) = explode(": ", $value); will assign $key = $result[0] and $value = $result[1]; if $result = explode(": ", $value);.
0
$array = [
    [
        '- :description: Rate the Code',
        ':long-description: ""',
        ':points: !float 5'
    ],

    [
        '- :description: Rate the Code',
        ':long-description: ""',
        ':points: !float 5'
    ],


    [
        '- :description: Rate the Code',
        ':long-description: ""',
        ':points: !float 5'
    ]
];

foreach($array as $key => $values) :
    $tmp = [];

    foreach($values as $k => $value) :
        $value = explode(': ', $value);
        $k = $value[0];
        unset($value[0]);
        $tmp[$value[0]] = implode(': ', $value);
    endforeach;

    $array[$key] = $tmp;
endforeach;

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.