简体   繁体   English

从PHP中的多值数组获取键值

[英]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 如何获得每个videoId,一个标题一个标题

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 我希望每个videoId和标题都应该一张一张地打印。.我完全感到困惑。.如果您给我一些小提示,我会做的

Use array_column to get particular key's value in multidimensional array 使用array_column获取多维数组中特定键的值

  <?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. 试试这个。您在数组中有两个子数组。因此您需要通过2 forEach循环进行迭代。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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