3

我有以下数组:

$array = array(
   'item-img-list' => array(
      array(
         'image-type' => 1,
         'image-url' => 'http://img07.allegroimg.pl/...'
      ),
      array(
         'image-type' => 2,
         'image-url' => 'http://img07.allegroimg.pl/...'
      ),
      array(
         'image-type' => 3,
         'image-url' => 'http://img07.allegroimg.pl/...'
      )
   )
)

如何在“image-type”=“2”的情况下获得第一个“image-url”值?我正在尝试通过这段代码做到这一点,但没有:

$zdjecia = $item['item-img-list'];
foreach($zdjecia as $zdjecie) {
   foreach($zdjecie as $key=>$value) {
      if($key == "image-type" && $value == "2") {
         $zdjecie_aukcji = $key['image-url'];
      }
   }
}

感谢您提供任何帮助!

作品!

$searchKey = 2;
foreach($zdjecia as $zdjecie) {
    if (**$zdjecie->{'image-type'}** == $searchKey){
        $zdjecie_aukcji = **$zdjecie->{'image-url'}**;
        break;
    }
}
4

6 回答 6

2

修改为:

$zdjecia = $array['item-img-list'];
foreach($zdjecia as $zdjecie) {
  if($zdjecie['image-type'] == '2') {
     $zdjecie_aukcji = $zdjecie['image-url'];
  }
}
于 2013-10-31T10:36:01.467 回答
2
$zdjecia = $item['item-img-list'];
$searchKey = 2;
foreach($zdjecia as $zdjecie) {
    if ($zdjecie['image-type'] == $searchKey)
        $zdjecie_aukcji = $zdjecie['image-url'];
        break;
    }
}

或 (PHP >=5.5)

$zdjecia = $item['item-img-list'];
$searchKey = 2;
$results = array_column(
    $zdjecia,
    'image-url',
    'image-type'
);

$zdjecie_aukcji = $results[$searchKey];
于 2013-10-31T10:36:37.080 回答
2

我用于我的项目的自定义函数的建议,用于在多维数组中通过键查找值:

function array_search_multi($array, $key, $value)
{
    $results = array();
    if (is_array($array))
    {
        if (isset($array[$key]) && $array[$key] == $value)
            $results[] = $array; 
        foreach ($array as $subarray)
            $results = array_merge($results, array_search_multi($subarray, $key, $value));
    }
    return $results;
}

用法:

$results = array_search_multi($array, 'image-type', '2');
echo $results[0]['image-url'];

输出:

http://img07.allegroimg.pl/...

工作示例

于 2013-10-31T10:36:58.943 回答
2

break;之后 立即添加

$zdjecie_aukcji = $key['image-url'];
于 2013-10-31T10:37:11.327 回答
2

为什么不简单地这样做:-

    $array = array(
       'item-img-list' => array(
          array(
             'image-type' => 1,
             'image-url' => 'http://img07.allegroimg.pl/...'
          ),
          array(
             'image-type' => 2,
             'image-url' => 'http://img07.allegroimg.pl/...'
          ),
          array(
             'image-type' => 3,
             'image-url' => 'http://img07.allegroimg.pl/...'
          )
       )
    );
    $newArray = array();

    foreach($array['item-img-list'] as $k=>$v){
      $newArray[$v['image-type']] = $v['image-url'];
    }

输出 :-

     Array
    (
        [1] => http://img07.allegroimg.pl/...
        [2] => http://img07.allegroimg.pl/...
        [3] => http://img07.allegroimg.pl/...
    )

或者

    echo $newArray[2];

您还可以像这样检查密钥:

 if (array_key_exists(2, $newArray)) {
   // Do whatever you want
 }

Working Demo

于 2013-10-31T10:38:38.670 回答
2
foreach($zdjecia as $zdjecie) {
   foreach($zdjecie as $key=>$value) {
      if($key == "image-type" && $value == "2") {
         $zdjecie_aukcji = $zdjecie['image-url'];
      }
   }
}
于 2013-10-31T10:38:45.287 回答