problem traversing an Array and getting the correct data Php

Question:

Hi, I have this Array:

   $array  = array (  
    'id' => 457243060,
    'album_id' => -3,
    'owner_id' => 208428341,
    'sizes' => array (
      array (
        'type' => 's',
        'url' => 'https://sun9-67.userapi.com/c857616/v857616133/1a62f4/eS7hlSZYlHE.jpg',
        'width' => 75,
        'height' => 75
      ),
      array (
        'type' => 'm',
        'url' => 'https://sun9-29.userapi.com/c857616/v857616133/1a62f5/oYJdSRKPbfY.jpg',
        'width' => 130,
        'height' => 130
      ),
      array (
        'type' => 'x',
        'url' => 'https://sun9-16.userapi.com/c857616/v857616133/1a62f6/-NHOu25XkDk.jpg',
        'width' => 604,
        'height' => 604
      ),
      array (
        'type' => 'y',
        'url' => 'https://sun9-14.userapi.com/c857616/v857616133/1a62f7/34fwsN3EFEc.jpg',
        'width' => 807,
        'height' => 807
      ),
      array (
        'type' => 'z',
        'url' => 'https://sun9-64.userapi.com/c857616/v857616133/1a62f8/8jDB0yaqEk4.jpg',
        'width' => 1080,
        'height' => 1080
      ),
      array (
        'type' => 'w',
        'url' => 'https://sun9-40.userapi.com/c857616/v857616133/1a62f9/ToyJjVZXaUg.jpg',
        'width' => 1280,
        'height' => 1280
      ),
      array (
        'type' => 'o',
        'url' => 'https://sun9-32.userapi.com/c857616/v857616133/1a62fa/7z-veU2v_1o.jpg',
        'width' => 130,
        'height' => 130,
      ),
      array (
        'type' => 'p',
        'url' => 'https://sun9-56.userapi.com/c857616/v857616133/1a62fb/dD_sshumBxE.jpg',
        'width' => 200,
        'height' => 200
      ),
      array (
        'type' => 'q',
        'url' => 'https://sun9-62.userapi.com/c857616/v857616133/1a62fc/qkCjR4c_k_M.jpg',
        'width' => 320,
        'height' => 320
      ),
      array (
        'type' => 'r',
        'url' => 'https://sun9-48.userapi.com/c857616/v857616133/1a62fd/NuONkTQdEkE.jpg',
        'width' => 510,
        'height' => 510
      )
    ),
    'text' => '',
    'date' => 1584084929,
    'access_key' => 'b97d42f1637ed7d825'
  );

I need help to get the largest image of this array , I have tried several ways, such as:

$contenido="";
foreach($array["items"] as $row){ 
    $contenido .= $row["id"].PHP_EOL; 
}

But it doesn't work for me, I hope you have an idea.

Answer:

A simple way to do it would be:

  1. Get the largest width in the overall array by using max() combined with array_column()
  2. Search within the sizes sub-array for the entire sub-array that has the width found in (1)

Let's see:

#(1) Obtenemos el mayor width
$max=max(array_column($mData["sizes"], 'width'));
#Array que tendrá los datos del mayor
$maxSize=array();

foreach ($mData["sizes"] as $size) { 
    #(2) Verificamos si es el width que encontramos en (1)
    if ($size["width"] == $max) {         
        $maxSize=$size;
        #Salimos del bucle porque ya encontramos lo que queríamos
        break;
    }
}
#Test ... bórralo luego
var_dump($maxSize);

Exit:

array(4) {
  ["type"]=>
  string(1) "x"
  ["url"]=>
  string(18) "https://sun......."
  ["width"]=>
  int(1280)
  ["height"]=>
  int(1280)
}

DEMONSTRATION


PS : If you are interested in doing something similar with another key, it would be convenient to incorporate the code into a function that receives the name of the keys as a parameter, both from the parent ( sizes ) and from the child ( width ).

Scroll to Top