F
F
FANTASANTA2020-04-26 21:01:38
PHP
FANTASANTA, 2020-04-26 21:01:38

How to select certain arrays by keys in an array?

How to select only arrays with certain formats in this array: "mp4, video, 720p", "mp4, video, 480p" ?

array (
    array(
      "url" => "tut url",
      "format" => "mp4, video, 720p"
    ),
    array(
      "url" => "tut url",
      "format" => "mp4, video, 480p"
    ),
    array(
      "url" => "tut url",
      "format" => "mp4, video, 360p"
    )
  )

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Edward, 2020-04-26
@FANTASANTA

FANTASANTA

$arr = [
    [
        "url" => "tut url",
        "format" => "mp4, video, 720p"
    ],

    [
        "url" => "tut url",
        "format" => "mp4, video, 480p"
    ],

    [
        "url" => "tut url",
        "format" => "mp4, video, 360p"
    ]
];

$arr = array_filter($arr, function($a){
    return $a['format'] == "mp4, video, 720p" || $a['format'] == "mp4, video, 480p";
});

var_dump($arr);
?

R
Rsa97, 2020-04-26
@Rsa97

$result = array_filter(
    $array,
    function ($el) { 
        return in_array($el.format, ['mp4, video, 720p', 'mp4, video, 480p']);
    }
);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question