get the last two-dimensional associative arrangement

0

Hello, I need to obtain the last two-dimensional associative arrangement but I do not know how to go through each element of the first dimension, and make it "end () to obtain the last record of each one.

It is according to "time" I must extract the last one and leave them in a new array.

What I want to do is get the last message of each number ("author"). get the last one with "time"

$url = 'http://gnex.cl/ajax/json.json';

$result = file_get_contents($url); // Send a request
$data = json_decode($result, 1); // Parse JSON

$agrupados = array();

foreach($data['messages'] as $currmsg)
{
    if (!isset($agrupados[$currmsg['author']]))
        {
            $agrupados[$currmsg['author']] = array();
        }

    array_push($agrupados[$currmsg['author']], $currmsg);
}

ACA I HAVE THE PROBLEM OF HOW TO IMPLEMENT END AND ADD TO $ hello = array ()

$hola = array();

foreach ($agrupados as $key => $value) {

    print $key."<p>";

    foreach ($value as $key => $value_1) {

        foreach ($value_1 as $key_1 ) {

            //if($key_1['author']){

                array_push(end($hola[$value_1['time']]), $value);
            //}
            # code...
        }



    }
    
asked by gonzalo ulloa 07.05.2018 в 16:11
source

1 answer

1

What you should do is only in the first foreach a end() but of the messages, since they are ordered.

$url = 'http://gnex.cl/ajax/json.json';
$result = file_get_contents($url); // Send a request
$data = json_decode($result, true); // Parse JSON

$agrupados = array();

foreach($data['messages'] as $currmsg) {
    if (!isset($agrupados[$currmsg['author']])) {
        $agrupados[$currmsg['author']] = array();
    }

    array_push($agrupados[$currmsg['author']], $currmsg);
}

$hola = array();

foreach ($agrupados as $author => $messages) {
    array_push($hola, end($messages));
}

And doing a var_dump($hola) will generate the following output :

array(2) {
  [0]=>
  array(9) {
    ["id"]=>
    string(42) "[email protected]_3EB0022131448222E23A"
    ["body"]=>
    string(5) "okale"
    ["fromMe"]=>
    bool(true)
    ["author"]=>
    string(16) "[email protected]"
    ["time"]=>
    int(1525275551)
    ["chatId"]=>
    string(16) "[email protected]"
    ["messageNumber"]=>
    int(185)
    ["type"]=>
    string(4) "chat"
    ["senderName"]=>
    string(4) "Alex"
  }
  [1]=>
  array(9) {
    ["id"]=>
    string(43) "[email protected]_3EB08C084679FE808DDC"
    ["body"]=>
    string(7) "de todo"
    ["fromMe"]=>
    bool(false)
    ["author"]=>
    string(16) "[email protected]"
    ["time"]=>
    int(1525270853)
    ["chatId"]=>
    string(16) "[email protected]"
    ["messageNumber"]=>
    int(199)
    ["type"]=>
    string(4) "chat"
    ["senderName"]=>
    string(4) "Alex"
  }
}
    
answered by 07.05.2018 / 17:34
source