获取特定键值的下一个索引

时间:2017-08-21 10:37:59

标签: php arrays loops

我是这样的阵列

[1] => Array(
            [description] => signer
            [boundingPoly] => Array (
                          [vertices] => Array (
                                    [0] => Array
                                        (
                                            [x] => 399
                                            [y] => 408
                                        )

                                    [1] => Array
                                        (
                                            [x] => 557
                                            [y] => 408
                                        )

                                    [2] => Array
                                        (
                                            [x] => 557
                                            [y] => 457
                                        )

                                    [3] => Array
                                        (
                                            [x] => 399
                                            [y] => 457
                                        )

                          )
              )
)
[2] => Array (
            [description] => -
            [boundingPoly] => Array (
                          [vertices] => Array (
                                    [0] => Array
                                        (
                                            [x] => 399
                                            [y] => 408
                                        )

                                    [1] => Array
                                        (
                                            [x] => 557
                                            [y] => 408
                                        )

                                    [2] => Array
                                        (
                                            [x] => 557
                                            [y] => 457
                                        )

                                    [3] => Array
                                        (
                                            [x] => 399
                                            [y] => 457
                                        )

                                )

                        )
)

[3] => Array (
            [description] => 1
            [boundingPoly] => Array (
                          [vertices] => Array (
                                    [0] => Array
                                        (
                                            [x] => 399
                                            [y] => 408
                                        )

                                    [1] => Array
                                        (
                                            [x] => 557
                                            [y] => 408
                                        )

                                    [2] => Array
                                        (
                                            [x] => 557
                                            [y] => 457
                                        )

                                    [3] => Array
                                        (
                                            [x] => 399
                                            [y] => 457
                                        )

                                )

                        )
)

现在我首先使用此代码

搜索了signer个关键字
if(stripos($eachItem['description'], "signer") !== false){}

然后我搜索description键中的值。

foreach($items as $index => $eachItem) {
   if(stripos($eachItem['description'], "signer") !== false) {
      if($eachItem['description'] == "signer") {
          $current = $eachItem['description'];
                      $keys = array_keys($eachItem);
                      $ordinal = (array_search($current,$keys)+1)%count($keys);
                      $next = $keys[$ordinal];
      }
   }
}

现在我想要的是获取每个索引的description键,例如在索引1 descriptionsigner而索引2 description是{{1} }并且在索引3 -description现在我想要1每个索引的值,所以当我连接description键时,值将是这样的{{1}在我的代码中,它获得了相同索引的description的下一个键,但我只想要每个索引的signer-1个键。

2 个答案:

答案 0 :(得分:0)

你可以简单地这样做。输出如下:array('singer-1', 'singer-2', ...)

$visited = -1;
$output = [];

foreach($items as $index => $item) {

  if($item['description'] == 'singer')
  {
      $output[$visited+1] = 'singer';
      $visited++;
  }
  else
  {
      $output[$visited] .= $item['description'];
  }

}

答案 1 :(得分:0)

仍然不确定您的预期输出。但是,如果你只想要键

,请试试这个
$a = [
    ["description" => "signer"],
    ["description" => "-"],
    ["description" => "1"]
];

//To get all values of description key
$desKeys = array_column($a, 'description'); //output ["singer", "-", "1"]
echo implode('', desKeys ); //output singer-1