简体   繁体   English

获取数组中的数组值

[英]Get a value of an array within an array

Not sure on this one and was hoping you would be able to help me out on this one ... 不确定这一点,希望您能在这一方面帮助我...

Here's my code 这是我的代码

Loader::library('file/types');
    $ih = Loader::helper('image');
    $names = explode("||",$this->tName);
    $urls = explode("||",$this->tUrl);
    $fIDs = explode("||",$this->fID);

    Loader::model('file');
    $i = Loader::helper('image');

    $v = array();
    $cc = 0;

    foreach ($names as $k=>$n){
      if (intval($fIDs[$k]) > 0 ) :
        $img = $test = File::getByID($fIDs[$k]);
        $fv = $img->getExtension();
        $ft = FileTypeList::getType($fv);
        $img = $ft->type == 1 ?  $img : false;  
      else :
        $img = false;
      endif;
    $v[$cc]['name']         = $n;
    $v[$cc]['url']          = $urls;
    $v[$cc]['src']          = $img ? $ih->getThumbnail($img,100,100)->src : false;
    $cc ++;
    }
    return $v;

And what i'm having issues with is getting the array values from $urls within this code. 我遇到的问题是从这段代码中的$ urls获取数组值。 (5th last one) (最后5个)

foreach ($names as $k=>$n){
      if (intval($fIDs[$k]) > 0 ) :
        $img = $test = File::getByID($fIDs[$k]);
        $fv = $img->getExtension();
        $ft = FileTypeList::getType($fv);
        $img = $ft->type == 1 ?  $img : false;  
      else :
        $img = false;
      endif;
    $v[$cc]['name']         = $n;
    $v[$cc]['url']          = $urls;
    $v[$cc]['src']          = $img ? $ih->getThumbnail($img,100,100)->src : false;
    $cc ++;
    }
    return $v;

Thanks for all your help. 感谢你的帮助。 Appreciate it. 欣赏它。

I think you have to do something like this: 我认为您必须执行以下操作:

$i = 0;

foreach ($names as $k=>$n){
  if (intval($fIDs[$k]) > 0 ) :
    $img = $test = File::getByID($fIDs[$k]);
    $fv = $img->getExtension();
    $ft = FileTypeList::getType($fv);
    $img = $ft->type == 1 ?  $img : false;  
  else :
    $img = false;
  endif;
$v[$cc]['name']         = $n;
$v[$cc]['url']          = $urls[$i]; //changed
$v[$cc]['src']          = $img ? $ih->getThumbnail($img,100,100)->src : false;
$cc ++;
$i++; //changed
}
return $v;

Since you are looping through the names array using $k as the index, you can access the matching url with the same index. 由于您要遍历使用$ k作为索引的名称数组,因此可以使用相同的索引访问匹配的url。 So you could change the line to: 因此,您可以将行更改为:

$v[$k]['url'] = $urls[$k];

There's no need to keep track of the index using the $cc variable since $k already represents the current index in the names array. 无需使用$ cc变量来跟踪索引,因为$ k已经表示了names数组中的当前索引。 You are already accessing the fIDs array the same way (using $k). 您已经以相同的方式(使用$ k)访问fIDs数组。

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

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