Skip to content

Instantly share code, notes, and snippets.

@mavitm
Last active March 10, 2017 10:43
Show Gist options
  • Save mavitm/fe875c86df03c84d0924b9899e46ce35 to your computer and use it in GitHub Desktop.
Save mavitm/fe875c86df03c84d0924b9899e46ce35 to your computer and use it in GitHub Desktop.
<?php
//array_keys
function thisArr($arr){
$a = array();
if(is_array($arr)){
//return array_keys($arr)
foreach ($arr as $key => $value) {
$a[] = $key;
if(is_array($value)){
$a = array_merge($a, thisArr($value));
}
}
$a = array_unique($a);
$a = array_diff($a, array(""," "));
//return implode(',', $a);
return $a;
}else{
return $arr;
}
}
$categories = '{"6":{"29":{"49":null,"50":null,"51":null,"52":null,"53":null,"54":null,"55":null,"56":null,"57":null,"58":null,"59":null},"30":{"60":null,"61":null,"62":null,"63":null,"64":null,"65":null,"66":null,"67":null,"68":null},"31":{"69":null,"70":null,"71":null,"72":null,"73":null,"74":null,"75":null}},"7":{"32":{"76":null,"77":null,"78":null,"79":null,"80":null,"81":null,"82":null,"83":null,"84":null,"85":null,"86":null,"87":null,"88":null,"89":null,"90":null,"91":null,"92":null,"93":null,"94":null,"95":null,"96":null},"33":{"97":null,"98":null,"99":null,"100":null,"101":null,"102":null,"103":null,"104":null,"105":null,"106":null,"107":null,"108":null,"109":null,"110":null,"111":null,"112":null,"113":null,"114":null,"115":null,"116":null,"117":null,"118":null,"119":null,"120":null,"121":null,"122":null,"123":null,"124":null,"125":null,"126":null,"127":null,"128":null,"129":null,"130":null,"131":null,"132":null,"133":null,"134":null},"34":{"135":null,"136":null,"137":null,"138":null,"139":null,"140":null,"141":null,"142":null,"143":null,"144":null,"145":null,"146":null,"147":null,"148":null,"149":null,"150":null,"151":null,"152":null,"153":null,"154":null,"155":null,"156":null,"157":null,"158":null,"159":null,"160":null,"161":null,"162":null,"163":null,"164":null,"165":null,"166":null,"167":null,"168":null,"169":null,"170":null,"171":null,"172":null,"173":null,"174":null,"175":null,"176":null,"177":null,"178":null,"179":null,"180":null,"181":null,"182":null,"183":null,"184":null,"185":null,"186":null,"187":null,"188":null,"189":null,"190":null,"191":null,"192":null,"193":null,"194":null,"195":null,"196":null,"197":null,"198":null,"199":null,"200":null,"201":null,"202":null,"203":null,"204":null,"205":null,"206":null,"207":null,"208":null,"209":null,"210":null,"211":null,"212":null,"213":null,"214":null,"215":null}},"8":{"35":null,"36":null},"9":null,"10":{"37":null,"38":null},"11":{"39":{"216":null,"217":null,"218":null,"219":null,"220":null,"221":null,"222":null,"223":null,"224":null},"40":{"225":null},"41":{"226":null,"227":null,"228":null,"229":null,"230":null,"231":null,"232":null,"233":null,"234":null,"235":null,"236":null,"237":null,"238":null,"239":null,"240":null,"241":null,"242":null,"243":null,"244":null,"245":null,"246":null,"247":null,"248":null,"249":null,"250":null,"251":null,"252":null,"253":null,"254":null,"255":null,"256":null,"257":null,"258":null,"259":null,"260":null,"261":null,"262":null,"263":null,"264":null,"265":null,"266":null,"267":null,"268":null,"269":null,"270":null,"271":null,"272":null,"273":null,"274":null,"275":null,"276":null,"277":null,"278":null},"42":{"279":null,"280":null,"281":null,"282":null,"283":null,"284":null,"285":null,"286":null,"287":null,"288":null,"289":null,"290":null,"291":null},"43":{"292":null,"293":null},"44":{"294":null,"295":null,"296":null,"297":null,"298":null,"299":null,"300":null,"301":null,"302":null,"303":null,"304":null,"305":null,"306":null,"307":null,"308":null,"309":null,"310":null,"311":null,"312":null,"313":null,"314":null,"315":null,"316":null},"45":{"317":null,"318":null},"46":null},"12":{"47":{"319":null,"320":null,"321":null,"322":null,"323":null,"324":null,"325":null},"48":{"326":null,"327":null,"328":null,"329":null,"330":null,"331":null,"332":null,"333":null}}}';
$json = json_decode($categories, true);
$istenen = array();
foreach ($json as $key => $value) {
$istenen[$key] = thisArr($value);
$istenen[$key][] = $key;
$istenen[$key] = implode(',', $istenen[$key]);
}
?>
<pre>
<?php print_r($istenen);?>
</pre>
Array
(
[6] => 29,49,50,51,52,53,54,55,56,57,58,59,30,60,61,62,63,64,65,66,67,68,31,69,70,71,72,73,74,75,6
[7] => 32,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,33,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,34,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,7
[8] => 35,36,8
[9] => 9
[10] => 37,38,10
[11] => 39,216,217,218,219,220,221,222,223,224,40,225,41,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255,256,257,258,259,260,261,262,263,264,265,266,267,268,269,270,271,272,273,274,275,276,277,278,42,279,280,281,282,283,284,285,286,287,288,289,290,291,43,292,293,44,294,295,296,297,298,299,300,301,302,303,304,305,306,307,308,309,310,311,312,313,314,315,316,45,317,318,46,11
[12] => 47,319,320,321,322,323,324,325,48,326,327,328,329,330,331,332,333,12
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment