Return the values of an array after a given key

Asked

Viewed 3,352 times

2

I have an array with about 100 positions this way:

[
  {
      "1":{
         "id":1262,
         "nome":"Fulano",
         "sobrenome":"de Tal"
      }
  },
  {
      "2":{
         "id":1263,
         "nome":"Beltrano",
         "sobrenome":"da Silva"
      }
  },
...
]

How do I return all values after key 2 ?

Thanks for your help!

  • When you say after the second key you mean after the second position?

  • That’s right, after the second position!

  • Friend just to confirm, you want in js or php?

  • Considering the @Silvioandorinha question, so no matter what the key is in the second position, you want to generate another array with the contents from the third. That’s right?

  • @Silvioandorinha It is in php, sorry for the lack of information!

  • @bfavaretto I don’t want to do another array, I want to print the values from the third position.

Show 1 more comment

2 answers

2


Print from the key 3 being greater than key 2

<?php
$array = [
         ["1" => ["id"=>1262,"nome"=>"Fulano","sobrenome"=>"de Tal"]],
         ["2" => ["id"=>1263,"nome"=>"Beltrano","sobrenome"=>"da Silva"]],
         ["3" => ["id"=>1264,"nome"=>"Fulano 1","sobrenome"=>"de Tal1"]],
         ["4" => ["id"=>1267,"nome"=>"Fulano 2","sobrenome"=>"de Tal2"]]
         ];


foreach($array as $index => $value){
    foreach($value as $index1 => $value1){
        if ((int)$index1 > 2){
            echo "N&uacute;mero:".$index1.' '. $value1["id"].' '.$value1["nome"]. ' '. $value1["sobrenome"];
            echo '<br>';
        }
    }   
}
  • 1

    That’s right, perfect! Thank you so much @Harrypotter

1

What if I told you that you can filter this matrix, without injecting it, at least not in the conventional way?

Nothing against the solution presented but should be avoided at all costs nested loops. This is extremely damaging to the performance of the Application.

Translating your problem into smaller parts, you want to locate a particular index and return everything after it.

By doing this, you are removing some items from the original matrix, so you are filtering it. What if you are filtering an array, array_filter() is the solution.

You’re gonna need one callback customized since there is no native function to do what you need. The... "essence" of this callback will be based on function key():

$filtered = array_filter(

    $array,

    function( $current ) {

        return ( key( $current ) > 2 );
    }
);

This produces:

Array
(
    [2] => Array
        (
            [3] => Array
                (
                    [id] => 1264
                    [nome] => Fulano 1
                    [sobrenome] => de Tal1
                )

        )

    [3] => Array
        (
            [4] => Array
                (
                    [id] => 1267
                    [nome] => Fulano 2
                    [sobrenome] => de Tal2
                )

        )

)

Note: The focus of the answer is filter the matrix and not unidimensionalize it, secondary and subsequent task of extreme importance to keep the routine away from nested loops.

  • I agree. In addition to being a better solution for performance, it is of utmost importance legibility of the code.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.