function recarray($ar, $searchfor) {
static $result = array();
foreach($ar as $k => $v) {
if ($k === $searchfor) $result[] = $v;
if (is_array($ar[$k])) recarray($v, $searchfor);
}
return $result;
}
$result = recarray($xmlArr, '@attributes');
function recarray($ar, $searchfor, $parent = null)
{
$result = [];
$code_parent = null;
foreach ($ar as $k => $v) {
if ($k === $searchfor) {
if ($parent)
$result['code_parent'] = $parent;
if (isset($v["code"]))
$code_parent = $v["code"];
$result[$k] = is_array($v)? recarray($v, $searchfor, $code_parent): $v;
} else {
$result[$k] = is_array($v)? recarray($v, $searchfor, $parent): $v;
}
}
return $result;
}
function recarray($ar, $searchfor, $parent = null)
{
$result = [];
$code_parent = null;
foreach ($ar as $k => $v) {
if ($k === $searchfor) {
if ($parent)
$result['code_parent'] = $parent;
if (isset($v["code"]))
$code_parent = $v["code"];
$result[$k] = is_array($v)? recarray($v, $searchfor, $code_parent): $v;
} else {
$result[$k] = is_array($v)? recarray($v, $searchfor, $parent): $v;
}
}
return $result;
}
$categories = [
[
"@attributes" => [
"code" => 1251,
"name" => "Красота и здоровье",
"category" => [
[
"@attributes" => [
"code" => "BAC",
"name" => "Фен счтетки",
]
],
[
"@attributes" => [
"code" => "EPI",
"name" => "Эпиляторы"
],
],
[
"@attributes" => [
"code" => "HCL",
"name" => "Машинки для стрижки"
],
],
]
]
],
[
"@attributes" => [
"code" => 1253,
"name" => "Красота и здоровье",
"category" => [
[
"@attributes" => [
"code" => "BAC",
"name" => "Фен счтетки",
]
],
[
"@attributes" => [
"code" => "EPI",
"name" => "Эпиляторы"
],
],
[
"@attributes" => [
"code" => "HCL",
"name" => "Машинки для стрижки"
],
],
]
]
]
];
print_r(recarray($categories, "@attributes"));
Array
(
[0] => Array
(
[@attributes] => Array
(
[code] => 1251
[name] => Красота и здоровье
[category] => Array
(
[0] => Array
(
[code_parent] => 1251
[@attributes] => Array
(
[code] => BAC
[name] => Фен счтетки
)
)
[1] => Array
(
[code_parent] => 1251
[@attributes] => Array
(
[code] => EPI
[name] => Эпиляторы
)
)
[2] => Array
(
[code_parent] => 1251
[@attributes] => Array
(
[code] => HCL
[name] => Машинки для стрижки
)
)
)
)
)
[1] => Array
(
[@attributes] => Array
(
[code] => 1253
[name] => Красота и здоровье
[category] => Array
(
[0] => Array
(
[code_parent] => 1253
[@attributes] => Array
(
[code] => BAC
[name] => Фен счтетки
)
)
[1] => Array
(
[code_parent] => 1253
[@attributes] => Array
(
[code] => EPI
[name] => Эпиляторы
)
)
[2] => Array
(
[code_parent] => 1253
[@attributes] => Array
(
[code] => HCL
[name] => Машинки для стрижки
)
)
)
)
)
)
function recarray($ar, $searchfor, $parent = null)
{
$result = [];
$code_parent = null;
foreach ($ar as $k => $v) {
if ($k === $searchfor) {
if ($parent)
$v['code_parent'] = $parent;
if (isset($v["code"]))
$code_parent = $v["code"];
$result[$k] = is_array($v)? recarray($v, $searchfor, $code_parent): $v;
} else {
$result[$k] = is_array($v)? recarray($v, $searchfor, $parent): $v;
}
}
return $result;
}
Array
(
[0] => Array
(
[@attributes] => Array
(
[code] => 1251
[name] => Красота и здоровье
[category] => Array
(
[0] => Array
(
[@attributes] => Array
(
[code] => BAC
[name] => Фен счтетки
[code_parent] => 1251
)
)
[1] => Array
(
[@attributes] => Array
(
[code] => EPI
[name] => Эпиляторы
[code_parent] => 1251
)
)
[2] => Array
(
[@attributes] => Array
(
[code] => HCL
[name] => Машинки для стрижки
[code_parent] => 1251
)
)
)
)
)
[1] => Array
(
[@attributes] => Array
(
[code] => 1253
[name] => Красота и здоровье
[category] => Array
(
[0] => Array
(
[@attributes] => Array
(
[code] => BAC
[name] => Фен счтетки
[code_parent] => 1253
)
)
[1] => Array
(
[@attributes] => Array
(
[code] => EPI
[name] => Эпиляторы
[code_parent] => 1253
)
)
[2] => Array
(
[@attributes] => Array
(
[code] => HCL
[name] => Машинки для стрижки
[code_parent] => 1253
)
)
)
)
)
)
function parentPush($array, $key , $parent = null){
$result = [];
foreach ($array as $k => $v) {
if(isset($v[$key])){
if ($parent)
$v[$key]['code_parent'] = $parent;
$result[] = [$key => array_diff_key($v[$key], array_flip(['category']))];
}
if (isset($v["code"]))
$parent = $v["code"];
$m = is_array($v)? parentPush($v, $key, $parent) : null;
$result = is_array($v)? array_merge_recursive($m, $result) : $result;
}
return $result;
}
function parentPush($array, $key , $parent = null){
$result = [];
foreach ($array as $k => $v) {
if(isset($v[$key])){
if ($parent)
$v[$key]['code_parent'] = $parent;
$result[] = array_diff_key($v[$key], array_flip(['category']));
}
if (isset($v["code"]))
$parent = $v["code"];
$m = is_array($v)? parentPush($v, $key, $parent) : null;
$result = is_array($v)? array_merge_recursive($m, $result) : $result;
}
return $result;
}
[0] => Array
(
[code] => CAB
[name] => Кабели
[code_parent] => 1572
)
function parentPush($array, $key , $parent = null){
$result = [];
foreach ($array as $k => $v) {
if(isset($v[$key])){
if ($parent)
$v[$key]['code_parent'] = $parent;
$result[] = array_diff_key($v[$key], array_flip(['category']));
}
if (isset($v["code"]))
$parent = $v["code"];
$m = is_array($v)? parentPush($v, $key, $parent) : null;
$result = is_array($v)? array_merge_recursive($result, $m) : $result;
}
return $result;
}
function addParent($array, $parent = null) {
foreach ($array as $key => $value) {
if (\is_null($parent)) {
$array[$key]['category'] = addParent($value['category'], $value['@attributes']['code']);
$array[$key]['@attributes'] = $value['@attributes'];
} else {
$array[$key]['code_parent'] = $parent;
}
}
return $array;
}
function recarray($ar, $searchfor) {
static $result = array();
foreach($ar as $k => $v) {
if ($k === $searchfor) $result[] = $v;
if (is_array($ar[$k])) recarray($v, $searchfor);
}
return $result;
}
$result = recarray($xmlArr, '@attributes');
function addParent($array, $parent = null) {
$result = [];
foreach ($array as $key => $value) {
if (\is_null($parent)) {
$result[] = $value['@attributes'];
$result = \array_merge($result, addParent($value['category'], $value['@attributes']['code']));
} else {
$result[] = \array_merge($value['@attributes'], ['code_parent' => $parent]);
}
}
return $result;
}
function addParent($array, $parent = null) {
$result = [];
foreach ($array as $key => $value) {
if (isset($value['category'])) {
$result = \array_merge($result, addParent($value['category'], $value['@attributes']['code']));
} else {
if ($parent) {
$result[] = \array_merge($value['@attributes'], ['code_parent' => $parent]);
} else {
$result[] = $value['@attributes'];
}
}
}
return $result;
}
function parentPush($array, $key , $parent = null){
$result = [];
foreach ($array as $k => $v) {
if(isset($v[$key])){
if ($parent)
$v[$key]['code_parent'] = $parent;
$result[] = array_diff_key($v[$key], array_flip(['category']));
}
if (isset($v["code"]))
$parent = $v["code"];
$m = is_array($v)? parentPush($v, $key, $parent) : null;
$result = is_array($v)? array_merge_recursive($result, $m) : $result;
}
return $result;
}
$result = parentPush($getSection, "@attributes");
print_r($result);
function addParent($array, $parent = null, $result = []) {
foreach ($array as $key => $value) {
if ($parent) {
$result[] = $value['@attributes'] + ['code_parent' => $parent];
} else {
$result[] = $value['@attributes'];
}
if (isset($value['category'])) {
$result = addParent($value['category'], $value['@attributes']['code'], $result);
}
}
return $result;
}