3

I have an array from which I want to remove the first level index and retain the value of it.
Is it possible to do without loops?

Input Array:

Array (
    [0] => Array(
             [2135] => Array(
                         [id] => 2135
                         [first_name] => John
                         [last_name] => Doe
                       )
            ),
    [1] => Array (
             [3245] => Array(
                         [id] => 3245
                         [first_name] => Sally
                         [last_name] => Smith
                       )
            )
)

Expected Output:

Array (
     [2135] => Array(
                 [id] => 2135
                 [first_name] => John
                 [last_name] => Doe
               ),


     [3245] => Array(
                 [id] => 3245
                 [first_name] => Sally
                 [last_name] => Smith
               )
)
8
  • can't be done without loops Commented Oct 19, 2015 at 12:29
  • Actually it can, but not clear to the developer. You can use an Iterator. You'll find more details on that answer, which is why I'm not answering. It's duplicated. Commented Oct 19, 2015 at 12:31
  • @AlanMachado iterator_to_array uses loops inside Commented Oct 19, 2015 at 12:32
  • 1
    Like I've said: not clear to the developer. It'll be not stating any for, while or foreach. But any function that takes array manupulation of multiple elements will have to iterate over it anyway, so... Commented Oct 19, 2015 at 12:33
  • 1
    Why do you need to "fix" the array? can't you build the array the proper way from the beginning? Commented Oct 19, 2015 at 15:41

3 Answers 3

1

Try this

$a = array (
    array(
        '2135' => array(
            'id' => 2135,
            'first_name' => 'John',
            'last_name' => 'Doe'
        )
    ),
    array (
        '3245' => array(
            'id' => 3245,
            'first_name' => 'Sally',
            'last_name' => 'Smith',
        )
    )
);
$reduce = function ($new = array(), $x) {
    $new[array_keys($x)[0]]=array_values($x)[0];
    return $new;
};

$output = array_reduce($a, $reduce);

// var_dump($output);
Sign up to request clarification or add additional context in comments.

5 Comments

Please, don't ask OP to "try" stuff without explaining how it works. Your answer can be read to other users searching for help and reading this question, and they might not be able to understand why your solution fits.
I thought it was fairly obvious how it worked. It uses array_reduce, and it does what the OP wanted, without using a loop (unless you count the loop that is implicit in array_reduce). I think it's also a little preferable to the array_filter answer, in that it doesn't require a new global variable.
Why you're so upset? I didn't tell your answer is wrong nor that you should withdraw it in behalf of mine, I just recommended (politely) that you'd offer some brief explanation on your code. It's community practice, only.
I think we're at risk of accumulating misunderstandings. I'm not upset, and I didn't interpret you as saying I was wrong, or that I should withdraw it. I saw that you'd offered an alternative answer which also works, and I'll upvote it now :). I'm not going to add explanations, though, because I can't really think of anything to add. Anyone can google array_reduce, which is all the explanation required.
Ok then, that's good sense. Sorry if I misunderstood your point. :)
1

Short (commented) answer:

<?php
// simulate array
$arr = [['2135' => ['id' => 2135,'first_name' => 'John','last_name' => 'Doe']],
        ['3245' => ['id' => 3245,'first_name' => 'Sally','last_name' => 'Smith']]];

// new indexed array
$newArr = [];

array_filter($arr, function($val) {     // array_filter checks if are arrays, 
    global $newArr;                     // then use its index as key to its val.
    return !is_array($val) ?: $newArr[key($val)] = $val[key($val)];
    });

echo '<pre>';
print_r($newArr);

// You could respect your data struct and use 'id' field to name keys as well.

Output:

Array
(
    [2135] => Array
        (
            [id] => 2135
            [first_name] => John
            [last_name] => Doe
        )

    [3245] => Array
        (
            [id] => 3245
            [first_name] => Sally
            [last_name] => Smith
        )

)

Comments

0

Without loops would be difficult but possible Solution with the loops would be:

$file_array = array();
foreach($array_name as $row =>$value){
    foreach ($value as $row1 =>$value1){
        if(in_array($value1, $file_array)){
        }
        else{
        $file_array[] =$value1;
        }
    }
}

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.