Как распарсить массив c частичным совпадением ключей?

Имеется массив с ключами типа
text_1, text_2...
title_1, title_2...
и так далее
нужно из всего этого хозяйства выделить группы типа
[text_1, title_1, ....]
[text_2, title_2, ....]
ума не приложу как это можно сделать

array (size=15)
  'text_1' => 
    array (size=1)
      0 => string 'I am so clever that sometimes I do not underst' (length=106)
  'text_2' => 
    array (size=1)
      0 => string 'It is better to be hated for what you are than f.' (length=110)
  'text_3' => 
    array (size=1)
      0 => string 'Whenever you find yourself on the .' (length=106)
  'text_4' => 
    array (size=1)
      0 => string 'Whenever you find yourself on the side of the majority.' (length=106)
  'title_1' => 
    array (size=1)
      0 => string 'Responsive design' (length=17)
  'title_2' => 
    array (size=1)
      0 => string 'Web development' (length=15)
  'title_3' => 
    array (size=1)
      0 => string 'Customer support' (length=16)
  'title_4' => 
    array (size=1)
      0 => string 'images included' (length=15)
  '_edit_lock' => 
    array (size=1)
      0 => string '1570606503:1' (length=12)
  '_thumbnail_id' => 
    array (size=1)
      0 => string '59' (length=2)
  '_edit_last' => 
    array (size=1)
      0 => string '1' (length=1)
  'single_image_1' => 
    array (size=1)
      0 => string '94' (length=2)
  'single_image_2' => 
    array (size=1)
      0 => string '91' (length=2)
  'single_image_3' => 
    array (size=1)
      0 => string '92' (length=2)
  'single_image_4' => 
    array (size=1)
      0 => string '93' (length=2)
  • Вопрос задан
  • 150 просмотров
Решения вопроса 1
Eridani
@Eridani
Мимо проходил
$arr = array(
  'title_1' => '1111111',
  'title_2' => '2222222',
  'title_3' => '33333333',
  'title_4' => '4444444444',
  'text_1' => 'aaa111',
  'text_2' => 'aaa222',
  'text_3' => 'aaa333',
  'text_4' => 'aaaa444', 
  'olol' => 'wssss',
  'sssss' => 'xxxxxxx', 
  'zzzzzzzzzzzzzz' => 'w222222sss',  
  'vfdsvsvds' => 'vdszxvsdsdv', 
);
$group = array();
foreach($arr as $key => $item) {
  if(strpos($key, 'text') !== false || strpos($key, 'title') !== false) {
    $group[substr($key, -1)][$key] = $item;
  }
}
$group = array_values($group);
print_r($group);

Array
(
    [0] => Array
        (
            [title_1] => 1111111
            [text_1] => aaa111
        )

    [1] => Array
        (
            [title_2] => 2222222
            [text_2] => aaa222
        )

    [2] => Array
        (
            [title_3] => 33333333
            [text_3] => aaa333
        )

    [3] => Array
        (
            [title_4] => 4444444444
            [text_4] => aaaa444
        )

)
Ответ написан
Пригласить эксперта
Ответы на вопрос 1
Ninazu
@Ninazu
Если неизвестны возможные ключи и количество индексов, а вывод нужно нормализировать

<?php
$array = [
	'text_1' => ['I am so clever that sometimes I do not underst'],
	//'text_2' => ['It is better to be hated for what you are than f.'],
	'text_3' => ['Whenever you find yourself on the .'],
	'text_4' => ['Whenever you find yourself on the side of the majority.'],
	'title_1' => ['Responsive design'],
	'title_2' => ['Web development'],
	'title_3' => ['Customer support'],
	'title_4' => ['images included'],
	'title_text_1' => ['Unexpected'], //Может и такое быть
	'_edit_lock' => ['1570606503:1'],
	'_thumbnail_id' => ['59'],
	'_edit_last' => ['1'],
	'single_image_1' => ['94'],
	'single_image_2' => ['91'],
	'single_image_3' => ['92'],
	'single_image_4' => ['93'],
];

$prefixes = [];
$maxIndex = 0;

foreach (array_keys($array) as $key) {
	$parts = explode('_', $key);
	$index = array_pop($parts);

	if (!is_numeric($index)) {
		continue;
	}

	$prefixes[implode('_', $parts)] = null;

	if ($index > $maxIndex) {
		$maxIndex = $index;
	}
}

$output = [];
$prefixes = array_keys($prefixes);

for ($index = 1; $index <= $maxIndex; $index++) {
	$row = [];

	foreach ($prefixes as $prefix) {
		$key = "{$prefix}_{$index}";
		$row[$key] = @$array[$key];
	}

	$output[] = $row;
}

var_dump($output);


sandbox.onlinephpfunctions.com/code/1fd759f48a4653...
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы