php - array conversion to the custom format

461

I have trying to solve this from past 5 hours result not came as i espected.. below the sample data which i have receive from a function.. please scroll down

Array
(
    [35] => Array
        (
            [36] => 
            [37] => Array
                (
                    [44] => 
                    [45] => 
                    [43] => 
                    [42] => 
                    [41] => 
                    [40] => 
                )

        )

    [1] => Array
        (
            [3] => 
            [2] => Array
                (
                    [25] => 
                    [146] => 
                    [23] => 
                    [22] => 
                    [8] => 
                    [9] => 
                    [12] => Array
                        (
                            [20] => 
                            [19] => 
                        )

                )

        )

What i wanted is the ex: "key value 35" having a reclusive array and i want to list all keys of its array

Example:

    $output_required = array(
                35 => array(
                    36, 37, 44, 45, 43, 42, 41,40
                ),
                36 => array(

                ),
                37 => array(
                    44,45,43,42,41,40
                ),
                1 => array(
                    3,2,25,146,23,22,8,9,12,20,19
                )
//Continue 3,2,25 etc
            );

Full DATA If you are working on solving my problem. :)

 $data_array = unserialize('a:9:{i:35;a:2:{i:36;s:0:"";i:37;a:6:{i:44;s:0:"";i:45;s:0:"";i:43;s:0:"";i:42;s:0:"";i:41;s:0:"";i:40;s:0:"";}}i:1;a:2:{i:3;s:0:"";i:2;a:7:{i:25;s:0:"";i:146;s:0:"";i:23;s:0:"";i:22;s:0:"";i:8;s:0:"";i:9;s:0:"";i:12;a:2:{i:20;s:0:"";i:19;s:0:"";}}}i:33;a:2:{i:34;s:0:"";i:51;a:2:{i:135;s:0:"";i:52;s:0:"";}}i:117;a:2:{i:118;a:4:{i:130;s:0:"";i:129;s:0:"";i:128;s:0:"";i:127;s:0:"";}i:119;a:9:{i:126;s:0:"";i:131;s:0:"";i:132;s:0:"";i:125;s:0:"";i:124;s:0:"";i:121;s:0:"";i:122;s:0:"";i:123;s:0:"";i:120;s:0:"";}}i:59;a:3:{i:64;a:2:{i:134;s:0:"";i:133;s:0:"";}i:60;a:2:{i:63;s:0:"";i:62;s:0:"";}i:61;a:5:{i:145;s:0:"";i:144;s:0:"";i:143;s:0:"";i:142;s:0:"";i:141;s:0:"";}}i:97;a:4:{i:101;s:0:"";i:100;s:0:"";i:99;s:0:"";i:98;s:0:"";}i:82;a:4:{i:86;s:0:"";i:83;a:2:{i:85;s:0:"";i:84;s:0:"";}i:87;a:2:{i:89;s:0:"";i:88;s:0:"";}i:136;a:4:{i:140;s:0:"";i:139;s:0:"";i:138;s:0:"";i:137;s:0:"";}}i:92;a:4:{i:96;s:0:"";i:95;s:0:"";i:94;s:0:"";i:93;s:0:"";}i:47;a:6:{i:57;s:0:"";i:58;s:0:"";i:54;s:0:"";i:53;s:0:"";i:49;s:0:"";i:48;s:0:"";}}');

This is the code what i have tried

public  function clean_cat_path($datas, $parent_ids = array())
    {
        static $array,$parent_ids;

        foreach ($datas as $k => $data) {
            if (!empty($data)) {
                $parent_ids[] = $k;
                $this->clean_cat_path($data, $parent_ids);
            }
            if (empty($parent_ids)) {
                $array[$k][$k] = $k;
            } else {
                foreach ($parent_ids as $id) {
                    $array[$k][$id] = $id;
                }
            }
            $parent_ids = array();
        }
        return $array;
    }

ITS SOLVED MYSELF sorry if you didn't understand correctly

<?php

class test_class
{
    static $cat_list;

    public function execute()
    {
        $data_array = unserialize('a:9:{i:35;a:2:{i:36;s:0:"";i:37;a:6:{i:44;s:0:"";i:45;s:0:"";i:43;s:0:"";i:42;s:0:"";i:41;s:0:"";i:40;s:0:"";}}i:1;a:2:{i:3;s:0:"";i:2;a:7:{i:25;s:0:"";i:146;s:0:"";i:23;s:0:"";i:22;s:0:"";i:8;s:0:"";i:9;s:0:"";i:12;a:2:{i:20;s:0:"";i:19;s:0:"";}}}i:33;a:2:{i:34;s:0:"";i:51;a:2:{i:135;s:0:"";i:52;s:0:"";}}i:117;a:2:{i:118;a:4:{i:130;s:0:"";i:129;s:0:"";i:128;s:0:"";i:127;s:0:"";}i:119;a:9:{i:126;s:0:"";i:131;s:0:"";i:132;s:0:"";i:125;s:0:"";i:124;s:0:"";i:121;s:0:"";i:122;s:0:"";i:123;s:0:"";i:120;s:0:"";}}i:59;a:3:{i:64;a:2:{i:134;s:0:"";i:133;s:0:"";}i:60;a:2:{i:63;s:0:"";i:62;s:0:"";}i:61;a:5:{i:145;s:0:"";i:144;s:0:"";i:143;s:0:"";i:142;s:0:"";i:141;s:0:"";}}i:97;a:4:{i:101;s:0:"";i:100;s:0:"";i:99;s:0:"";i:98;s:0:"";}i:82;a:4:{i:86;s:0:"";i:83;a:2:{i:85;s:0:"";i:84;s:0:"";}i:87;a:2:{i:89;s:0:"";i:88;s:0:"";}i:136;a:4:{i:140;s:0:"";i:139;s:0:"";i:138;s:0:"";i:137;s:0:"";}}i:92;a:4:{i:96;s:0:"";i:95;s:0:"";i:94;s:0:"";i:93;s:0:"";}i:47;a:6:{i:57;s:0:"";i:58;s:0:"";i:54;s:0:"";i:53;s:0:"";i:49;s:0:"";i:48;s:0:"";}}');
        return $this->clean_cat_path($data_array);
    }

    public function clean_cat_path($datas)
    {
        foreach ($datas as $k => $data) {
            $this->cat_list[$k][$k] = $k;
            if (!empty($data)) {
                $this->list_keys_of($data, $k);
                $this->clean_cat_path($data);
            }
        }
        return $this->cat_list;
    }

    private function list_keys_of($data, $key_id)
    {
        foreach ($data as $k => $d) {
            $this->cat_list[$key_id][] = $k;
            if (!empty($d)) {
                $this->list_keys_of($d, $key_id);
            }
        }
    }

}

$test = new test_class();
$output = $test->execute();

echo '<pre>';
print_r($output);
echo '</pre>';
311

Answer

Solution:

Here's a first idea :

$array = unserialize('a:9:{i:35;a:2:{i:36;s:0:"";i:37;a:6:{i:44;s:0:"";i:45;s:0:"";i:43;s:0:"";i:42;s:0:"";i:41;s:0:"";i:40;s:0:"";}}i:1;a:2:{i:3;s:0:"";i:2;a:7:{i:25;s:0:"";i:146;s:0:"";i:23;s:0:"";i:22;s:0:"";i:8;s:0:"";i:9;s:0:"";i:12;a:2:{i:20;s:0:"";i:19;s:0:"";}}}i:33;a:2:{i:34;s:0:"";i:51;a:2:{i:135;s:0:"";i:52;s:0:"";}}i:117;a:2:{i:118;a:4:{i:130;s:0:"";i:129;s:0:"";i:128;s:0:"";i:127;s:0:"";}i:119;a:9:{i:126;s:0:"";i:131;s:0:"";i:132;s:0:"";i:125;s:0:"";i:124;s:0:"";i:121;s:0:"";i:122;s:0:"";i:123;s:0:"";i:120;s:0:"";}}i:59;a:3:{i:64;a:2:{i:134;s:0:"";i:133;s:0:"";}i:60;a:2:{i:63;s:0:"";i:62;s:0:"";}i:61;a:5:{i:145;s:0:"";i:144;s:0:"";i:143;s:0:"";i:142;s:0:"";i:141;s:0:"";}}i:97;a:4:{i:101;s:0:"";i:100;s:0:"";i:99;s:0:"";i:98;s:0:"";}i:82;a:4:{i:86;s:0:"";i:83;a:2:{i:85;s:0:"";i:84;s:0:"";}i:87;a:2:{i:89;s:0:"";i:88;s:0:"";}i:136;a:4:{i:140;s:0:"";i:139;s:0:"";i:138;s:0:"";i:137;s:0:"";}}i:92;a:4:{i:96;s:0:"";i:95;s:0:"";i:94;s:0:"";i:93;s:0:"";}i:47;a:6:{i:57;s:0:"";i:58;s:0:"";i:54;s:0:"";i:53;s:0:"";i:49;s:0:"";i:48;s:0:"";}}');
$output = array();

function get_every_key($arr)
{    
    global $output; 
    foreach( $arr as $key => $value )
    {
        if (is_array($value)) 
        {
            $output[$key]=array_keys($value);
            get_every_key($value);
        }
    }

}

get_every_key($array);
print_r($output);

This will find every sub array in your initial array, and extract the keys of this sub-array. The issue is that it only gets the first-level keys of the sub-array.

55

Answer

Solution:

I think you could try this.

It's pretty short and it does what you want.

$arr_initial_data = unserialize('a:9:{i:35;a:2:{i:36;s:0:"";i:37;a:6:{i:44;s:0:"";i:45;s:0:"";i:43;s:0:"";i:42;s:0:"";i:41;s:0:"";i:40;s:0:"";}}i:1;a:2:{i:3;s:0:"";i:2;a:7:{i:25;s:0:"";i:146;s:0:"";i:23;s:0:"";i:22;s:0:"";i:8;s:0:"";i:9;s:0:"";i:12;a:2:{i:20;s:0:"";i:19;s:0:"";}}}i:33;a:2:{i:34;s:0:"";i:51;a:2:{i:135;s:0:"";i:52;s:0:"";}}i:117;a:2:{i:118;a:4:{i:130;s:0:"";i:129;s:0:"";i:128;s:0:"";i:127;s:0:"";}i:119;a:9:{i:126;s:0:"";i:131;s:0:"";i:132;s:0:"";i:125;s:0:"";i:124;s:0:"";i:121;s:0:"";i:122;s:0:"";i:123;s:0:"";i:120;s:0:"";}}i:59;a:3:{i:64;a:2:{i:134;s:0:"";i:133;s:0:"";}i:60;a:2:{i:63;s:0:"";i:62;s:0:"";}i:61;a:5:{i:145;s:0:"";i:144;s:0:"";i:143;s:0:"";i:142;s:0:"";i:141;s:0:"";}}i:97;a:4:{i:101;s:0:"";i:100;s:0:"";i:99;s:0:"";i:98;s:0:"";}i:82;a:4:{i:86;s:0:"";i:83;a:2:{i:85;s:0:"";i:84;s:0:"";}i:87;a:2:{i:89;s:0:"";i:88;s:0:"";}i:136;a:4:{i:140;s:0:"";i:139;s:0:"";i:138;s:0:"";i:137;s:0:"";}}i:92;a:4:{i:96;s:0:"";i:95;s:0:"";i:94;s:0:"";i:93;s:0:"";}i:47;a:6:{i:57;s:0:"";i:58;s:0:"";i:54;s:0:"";i:53;s:0:"";i:49;s:0:"";i:48;s:0:"";}}');

function clean_cat_path($arr_data, $int_parent_id) {
   global $arr_final_data;
   if (count($arr_data) > 0 && is_array($arr_data)) {
      foreach ($arr_data as $k=>$v) {
         if ($int_parent_id) {
            $arr_final_data[$int_parent_id][$k] = $k;
         }
         if (is_array($v)) {
            if ($int_parent_id) {
               clean_cat_path($v, $int_parent_id);
            } else {
               clean_cat_path($v, $k);
            }
         }   
      }      
   }         
}            
clean_cat_path($arr_initial_data, NULL);
echo "<pre>";
var_dump($arr_final_data);
echo "</pre>";

It the final array should be as this at the end:

array(9) {
  [35]=>
  array(8) {
    [36]=>
    int(36)
    [37]=>
    int(37)
    [44]=>
    int(44)
    [45]=>
    int(45)
    [43]=>
    int(43)
    [42]=>
    int(42)
    [41]=>
    int(41)
    [40]=>
    int(40)
  }
  [1]=>
  array(11) {
    [3]=>
    int(3)
    [2]=>
    int(2)
    [25]=>
    int(25)
    [146]=>
    int(146)
    [23]=>
    int(23)
    [22]=>
    int(22)
    [8]=>
    int(8)
    [9]=>
    int(9)
    [12]=>
    int(12)
    [20]=>
    int(20)
    [19]=>
    int(19)
  }
  [33]=>
  array(4) {
    [34]=>
    int(34)
    [51]=>
    int(51)
    [135]=>
    int(135)
    [52]=>
    int(52)
  }
  [117]=>
  array(15) {
    [118]=>
    int(118)
    [130]=>
    int(130)
    [129]=>
    int(129)
    [128]=>
    int(128)
    [127]=>
    int(127)
    [119]=>
    int(119)
    [126]=>
    int(126)
    [131]=>
    int(131)
    [132]=>
    int(132)
    [125]=>
    int(125)
    [124]=>
    int(124)
    [121]=>
    int(121)
    [122]=>
    int(122)
    [123]=>
    int(123)
    [120]=>
    int(120)
  }
  [59]=>
  array(12) {
    [64]=>
    int(64)
    [134]=>
    int(134)
    [133]=>
    int(133)
    [60]=>
    int(60)
    [63]=>
    int(63)
    [62]=>
    int(62)
    [61]=>
    int(61)
    [145]=>
    int(145)
    [144]=>
    int(144)
    [143]=>
    int(143)
    [142]=>
    int(142)
    [141]=>
    int(141)
  }
  [97]=>
  array(4) {
    [101]=>
    int(101)
    [100]=>
    int(100)
    [99]=>
    int(99)
    [98]=>
    int(98)
  }
  [82]=>
  array(12) {
    [86]=>
    int(86)
    [83]=>
    int(83)
    [85]=>
    int(85)
    [84]=>
    int(84)
    [87]=>
    int(87)
    [89]=>
    int(89)
    [88]=>
    int(88)
    [136]=>
    int(136)
    [140]=>
    int(140)
    [139]=>
    int(139)
    [138]=>
    int(138)
    [137]=>
    int(137)
  }
  [92]=>
  array(4) {
    [96]=>
    int(96)
    [95]=>
    int(95)
    [94]=>
    int(94)
    [93]=>
    int(93)
  }
  [47]=>
  array(6) {
    [57]=>
    int(57)
    [58]=>
    int(58)
    [54]=>
    int(54)
    [53]=>
    int(53)
    [49]=>
    int(49)
    [48]=>
    int(48)
  }
}

People are also looking for solutions to the problem: php - Laravel required_if not working

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.