1

I have the following array:

$arr = [
    "elem-1" => [ "title" => "1", "desc" = > "" ],
    "elem-2" => [ "title" => "2", "desc" = > "" ],
    "elem-3" => [ "title" => "3", "desc" = > "" ],
    "elem-4" => [ "title" => "4", "desc" = > "" ],
]

First I need to change the value from [ "title" => "1", "desc" = > "" ] to 1 (title's value).

I did this using array_walk:

array_walk($arr, function(&$value, $key) {
    $value = $value["title"];
});

This will replace my value correctly. Our current array now is:

$arr = [
    "elem-1" => "1",
    "elem-2" => "2",
    "elem-3" => "3",
    "elem-4" => "4",
]

Now, I need to transform each element of this array into its own subarray. I have no idea on how to do this without a for loop. This is the desired result:

$arr = [
    [ "elem-1" => "1" ],
    [ "elem-2" => "2" ],
    [ "elem-3" => "3" ],
    [ "elem-4" => "4" ],
]
3
  • You could create a new array and do foreach($arr as $value) { $newarray[] = array($value)} but I'm sure there's a built in function that could do that. I just don't know of it Commented Aug 8, 2018 at 20:46
  • 1
    Why can't you use a for loop? Commented Aug 8, 2018 at 20:48
  • A for loop is straightforward. Since I'm already using array_* functions, I figured it would be nice to have a solution like that. Commented Aug 8, 2018 at 20:53

4 Answers 4

5

You can change your array_walk callback to produce that array.

array_walk($arr, function(&$value, $key) {
    $value = [$key => $value["title"]];
});

Run the transformed array through array_values if you need to get rid of the string keys.

$arr = array_values($arr);
Sign up to request clarification or add additional context in comments.

Comments

2

To offer an alternative solution you could achieve all of this with array_map

 <?php

$arr = [
    "elem-1" => [ "title" => "1", "desc" => "" ],
    "elem-2" => [ "title" => "2", "desc" => "" ],
    "elem-3" => [ "title" => "3", "desc" => "" ],
    "elem-4" => [ "title" => "4", "desc" => "" ],
];



function convertToArray($key,$elem){
   return [$key => $elem['title']];
}

$arr = array_map("convertToArray", array_keys($arr), $arr);
echo '<pre>';
print_r($arr);
echo '</pre>';
?>

outputs

  Array
(
    [0] => Array
        (
            [elem-1] => 1
        )

    [1] => Array
        (
            [elem-2] => 2
        )

    [2] => Array
        (
            [elem-3] => 3
        )

    [3] => Array
        (
            [elem-4] => 4
        )

)

2 Comments

I was wondering why you deleted it. I was going to suggest the same thing as an alternative.
I realised my first iteration had the wrong output so originally I was just going to delete the whole thing bearing in mind you'd already provided a selected answer. But it was a simple enough fix so I reinstated it just so others could see alternative methods afterall thats how we all learn right?
0

It doesn't make much sense to use array_walk() and modify by reference because the final result needs to have completely new keys on both levels. In other words, the output structure is completely different from the input and there are no salvageable/mutable parts. Mopping up the modified array with array_values() only adds to the time complexity cost.

array_map() has to bear a cost to time complexity too because array_keys() must be passed in as an additional parameter.

If you want to use array_walk(), use use() to modify the result array. This will allow you to enjoy the lowest possible time complexity.

More concise than array_walk() and cleaner to read, I would probably use a classic foreach() in my own project.

Codes: (Demo)

$result = [];
array_walk(
    $arr,
    function($row, $key) use(&$result) {
        $result[] = [$key => $row['title']];
    }
);

Or:

$result = [];
foreach ($arr as $key => $row) {
    $result[] = [$key => $row['title']];
}
var_export($result);

Or:

var_export(array_chunk(array_map(fn($row) => $row['title'], $arr), 1, true));

1 Comment

-2

You need to use array_map like

$new_arr= array_map(function($key,$val){
return [$key => $val['title']];},array_keys($arr),$arr); 

2 Comments

array_map doesn't get the keys by default. You need to pass them in as another array. Check out the other answer from TommyBs
Yes sure.. I forgot to add that bcoz I am typing on mobile sorry

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.