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
               )
)
al'ein
  • 1,711
  • 1
  • 14
  • 21
Rumi
  • 229
  • 1
  • 13
  • can't be done without loops – Abdo Adel Oct 19 '15 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](http://stackoverflow.com/questions/13920659/php-remove-parent-level-array-from-set-of-arrays-and-merge-nodes), which is why I'm not answering. It's duplicated. – al'ein Oct 19 '15 at 12:31
  • @AlanMachado `iterator_to_array` uses loops inside – Abdo Adel Oct 19 '15 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... – al'ein Oct 19 '15 at 12:33
  • I see, the subject of the question should be sth like **Is it possible to do without `for` , `while` or `foreach` constructs** – Abdo Adel Oct 19 '15 at 12:37
  • @AlanMachado, there are many similar questions but the expected result for each of them is different. I want to maintain the inner indexes as stated in my question under expected result. – Rumi Oct 19 '15 at 12:41
  • I see. `array_walk_recursive` or `array_filter` are probably the answer you're looking for. Both of them are capable of maintain index association. – al'ein Oct 19 '15 at 12:44
  • 1
    Why do you need to "fix" the array? can't you build the array the proper way from the beginning? – dognose Oct 19 '15 at 15:41

3 Answers3

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);
Chris Lear
  • 6,592
  • 1
  • 18
  • 26
  • 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. – al'ein Oct 19 '15 at 17:26
  • 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. – Chris Lear Oct 20 '15 at 08:13
  • 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. – al'ein Oct 20 '15 at 09:40
  • 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. – Chris Lear Oct 20 '15 at 09:47
  • Ok then, that's good sense. Sorry if I misunderstood your point. :) – al'ein Oct 20 '15 at 09:51
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
        )

)
al'ein
  • 1,711
  • 1
  • 14
  • 21
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;
        }
    }
}