简体   繁体   中英

Get the key value from multivalued array in php

I have an array

 Array ( 
[0] => Array ( [0] => Array ( [videoId] => FysV6XnDlQk [title] => Kannaana Kanney Song with Lyrics | Viswasam Songs | Ajith Kumar,Nayanthara | D.Imman|Siva|Sid Sriram [likeInfo] => Array ( [likes] => 1 [dislikes] => 0 [liked] => 1 [disliked] => 0 ) ) ) 
[1] => Array ( [0] => Array ( [videoId] => hXNSAb3s1XY [title] => Best of IRON MAN | Best of TONY STARK [2008-2018] [likeInfo] => Array ( [likes] => 0 [dislikes] => 0 [liked] => 0 [disliked] => 0 ) ) ) ) 

how can I get each videoId,title one by one

I have tried

$s=array();
for ($i=0; $i < sizeof($myArray) ; $i++) { 
    $s[] = array($myArray[$i]);
    echo "<br/>";
}
print_r($s);
$ids=array();
foreach($s as $user) {
    $ids[] = $user['videoId'];
}
print_r($ids);

I'm expecting each videoId and title should be print one by one..I totally confused..If you give me small hint I'll work on that

Use array_column to get particular key's value in multidimensional array

  <?php
  $data = array_column($yourArr, 0);
  $videoIdArr =  array_column($data, 'title', 'videoId');

  print_r($videoIdArr );

 ?>

Your output will be

Array
(
  [FysV6XnDlQk] => Kannaana Kanney Song with Lyrics | Viswasam Songs | Ajith Kumar,Nayanthara | D.Imman|Siva|Sid Sriram
  [hXNSAb3s1XY] => Best of IRON MAN | Best of TONY STARK
)

Try this .You have two sub array inside the array .So you need to iterate via 2 forEach loop.

 forEach($myArray as $value){
    forEach($value as $subvalue){
     echo $subvalue['videoId'].':'.$subvalue['title'];
   }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM