0

I got the following array

array(3) {
  [0] =>
  array(1) {
    'Investment' =>
    array(15) {
      'id' =>
     string(36) "53d64bec-031c-4732-b2e0-755799154b1b" ...

I would like to remove the Investment key and do the new array should be

    array(3) {
  [0] =>
  array(15) {
      'id' =>
     string(36) "53d64bec-031c-4732-b2e0-755799154b1b" ...

how do I do it?

1
  • 5
    $array[0]=$array[0]['Investment']; Commented Jul 28, 2014 at 13:14

3 Answers 3

1

I would pass the array to array_map as such:

$array = [
    [ 'Investment' => [ 'id' => '13d64bec-031c-4732-b2e0-755799154b1b' ] ],
    [ 'Investment' => [ 'id' => '23d64bec-031c-4732-b2e0-755799154b1b' ] ],
    [ 'Investment' => [ 'id' => '33d64bec-031c-4732-b2e0-755799154b1b' ] ],
    [ 'Investment' => [ 'id' => '43d64bec-031c-4732-b2e0-755799154b1b' ] ]
];

$mappedArray = array_map(function($val) {
    return $val['Investment'];
}, $array);
Sign up to request clarification or add additional context in comments.

Comments

0

You can use array_map() and return the value of the 'Investment' key for each item.

$newArray = array_map(function($item) {
    return $item['Investment'];
}, $oldArray);

If you do not want to copy the array, you can possibly try to use array_walk().

Edit: solution using array_walk()

array_walk($oldArray, function(&$item) {
    $item = $item['Investment'];
});

Comments

0

store investment array in temp variable and unset as similar as given below

$tempArr = // 0 index of your array
foreach($tempArr as $key=>$val){
     if(!empty($val['Investment'])){
       $temp = $val['Investment'];
    unset($val['Investment']);
    $val[] = $temp;
}

}

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.