php 过滤掉不需要的元素后将1D数组转换为2D

um6iljoc  于 2023-01-16  发布在  PHP
关注(0)|答案(6)|浏览(150)

我有一个一维数组如下:

$arr = [
    'itemName0' => "first Item",
    'itemDesc0' => "first item's description",
    'itemName1' => "second item",
    'itemDesc1' => "third item's description",
];

并且我想将其转换为2D阵列,如下所示

$arr = [
    0 => [
        'itemName' => "first Item",
        'itemDesc' => "first item's description",
    ],
    1 => [
        'itemName' => "second item",
        'itemDesc' => "Second item's description",
    ],
];

实际上,我有来自API响应的数据,其中包含除项目之外的其他详细信息.....{index},例如

$arr = [
    'status' => 'success',
    'blahblah' => 'this is just a test',
    'itemName0' => "first Item",
    'itemDesc0' => "first item's description",
    'itemName1' => "second item",
    'itemDesc1' => "third item's description",
];

但是我只需要项目数据。
我已经尝试使用array_map获取项目,但无法..

array_map(function($value, $key){ strpos($key, 'item') === false ? false : $value;  }, $arr));

我还在努力,任何想法都会很有帮助

kb5ga3dv

kb5ga3dv1#

使用数组块方法是最简单的

$input_array = array('a', 'b', 'c', 'd', 'e','f');
print_r(array_chunk($input_array, 2));

输出:

Array
(
 [0] => Array
    (
        [0] => a
        [1] => b
    )

 [1] => Array
    (
        [0] => c
        [1] => d
    )

 [2] => Array
    (
        [0] => e
        [1] => f
    ) 
)
c9qzyr3d

c9qzyr3d2#

你可以试试这个-

$arr = [
    'itemName0' => "first Item",
    'itemDesc0' => "first item's description",
    'itemName1' => "second item",
    'itemName2' => "second item's description",
];

$new = [];
foreach($arr as $key => $value) {
   $index = sscanf($key, '%[A-Za-z]%d');
   if(strpos($key, 'item') === 0) {
      $new[$index[1]][$index[0]] = $value;
   }
}
    • 产出**
array(3) {
  [0]=>
  array(2) {
    ["itemName"]=>
    string(10) "first Item"
    ["itemDesc"]=>
    string(24) "first item's description"
  }
  [1]=>
  array(1) {
    ["itemName"]=>
    string(11) "second item"
  }
  [2]=>
  array(1) {
    ["itemName"]=>
    string(25) "second item's description"
  }
}
t40tm48m

t40tm48m3#

首先,创建一个空的多维数组:
$multi_arr = array();
然后遍历原始数组,

foreach($arr as $key => $val) { <br/>
    //get last character from key<br/>
    $k = substr($key, -1);<br/>
    //get key index value from original key by removing last character<br/>
    $index = rtrim($key, $k);<br/>
    //Push values to multi dimensional array<br/>
    $multi_arr[$k][$index] = $val;<br/>
}
ecr0jaav

ecr0jaav4#

$arr = [
    'itemName0' => "first Item",
    'itemDesc0' => "first item's description",
    'itemName1' => "second item",
    'itemName2' => "third item's description",
    'testKey1' => "Should be ignored",
    'itemData' => "No numeric, so should also be ignored"
];

$newArray = [];
foreach($arr as $key => $value) {
    list($keyName, $keyNumber) = sscanf($key, 'item%[A-Za-z]%d');
    if (!is_null($keyNumber)) {
        $newArray[$keyNumber]['item'.$keyName] = $value;
    }
}

var_dump($newArray);

Demo

xdnvmnnf

xdnvmnnf5#

我能说的最简单的就是

$process = array_filter($arr, function($key){
               return (strpos($key, 'itemName') !== false && ctype_digit(substr($key, 8)))
                      || 
                      (strpos($key, 'itemDesc') !== false && ctype_digit(substr($key, 8)));
           }, ARRAY_FILTER_USE_KEY);

$final = [];

foreach ($process as $key => $value)
{
    $final[substr($key, 8)][substr($key, 0, 8)] = $value;
}
8tntrjer

8tntrjer6#

现在回答这个问题可能为时已晚,但我已经创建了一个函数,可以根据您的需要将数组拆分为多个组。

function SplitInto($array, $groups = 2)
{
   $members = count($array) / $groups;
   if (count($array) % $groups) $members = (int)(count($array) / $groups) + 1;
   return array_chunk($array, $members);
}

相关问题