How extract arrays from array depend on values

Discussion in 'PHP' started by earngate, May 12, 2020.

  1. #1
    My array is
    $arr = array(
        'global' => array(
            array('tabs', 'tabs', 'tabs', array(
                'Main' => 'main',
                'Gallery' => 'galleryID',
                'mera' => 'mera',
            )),
    
            array('main','div',  'div'),
            array('egice', 'Eice', 'text', '', ''),
            array('closeHere', 'closeHere', 'closeHere'),
    
    
            array('galleryID','div',  'div'),
            array('gallery', 'Gallery #1', 'image', '', ''),
            array('closeHere', 'closeHere', 'closeHere'),
    
    
            array('networking', 'div', 'div'),
            array('siLype', 'SiLype', 'text', '', ''),
            array('gpXCge', 'GpXCge',  'toggle_button', '1', 'Yes', 'No', ''),
            array('closeHere', 'closeHere', 'closeHere'),
    
    
            array('mera', 'div', 'div'),
            array('maimera', 'Maimera', 'repeatedText', 'resYI', 'GYL',''),
            array('closeHere', 'closeHere', 'closeHere'),
    
    
    
            array( 'moures','div', 'div'),
            array('meLmo', 'MeLmo', 'text', '', ''),
            array('cYSlot', 'CYSlot', 'text', '', ''),
            array('closeHere', 'closeHere', 'closeHere'),
    
            array('clT', 'clT', 'clT'),
    
    
    
        ),
    
    );
    
    PHP:
    How can I separate it to multiple array when value is "div" close the array and generate a new one .
    I had tried here to do it but I can't
    https://3v4l.org/ppj1O
    Could you help?
     
    earngate, May 12, 2020 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,897
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #2
    Can you explain what the first array represents and how the $split array will be used?

    I'm trying to get my head around what you're doing.
     
    sarahk, May 13, 2020 IP
  3. earngate

    earngate Well-Known Member

    Messages:
    102
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #3

    Thanks, simply I want to separate the array into 4 arrays
    when array has value == div begin new array to be into


    $arr1 =  array(array('main','div',  'div'),
        array('egice', 'Eice', 'text', '', ''),
        array('closeHere', 'closeHere', 'closeHere'));
    
    $arr2 = array( array('galleryID','div',  'div'),
        array('gallery', 'Gallery #1', 'image', '', ''),
        array('closeHere', 'closeHere', 'closeHere'),);
    
    $arr3= array(    array('mera', 'div', 'div'),
        array('maimera', 'Maimera', 'repeatedText', 'resYI', 'GYL',''),
        array('closeHere', 'closeHere', 'closeHere'));
    
    $arr4= array( array('networking', 'div', 'div'),
        array('siLype', 'SiLype', 'text', '', ''),
        array('gpXCge', 'GpXCge',  'toggle_button', '1', 'Yes', 'No', ''),
        array('closeHere', 'closeHere', 'closeHere'));
    PHP:
     
    earngate, May 13, 2020 IP
  4. sarahk

    sarahk iTamer Staff

    Messages:
    28,897
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #4
    Something like this will work if you're ok with not testing the nested array
    
    $arr = array(
        'global' => array(
            array('tabs', 'tabs', 'tabs', array('Main' => 'main', 'Gallery' => 'galleryID',  'mera' => 'mera' )),
            array('main','div',  'div'),
            array('egice', 'Eice', 'text', '', ''),
            array('closeHere', 'closeHere', 'closeHere'),
            array('galleryID','div',  'div'),
            array('gallery', 'Gallery #1', 'image', '', ''),
            array('closeHere', 'closeHere', 'closeHere'),
            array('networking', 'div', 'div'),
            array('siLype', 'SiLype', 'text', '', ''),
            array('gpXCge', 'GpXCge',  'toggle_button', '1', 'Yes', 'No', ''),
            array('closeHere', 'closeHere', 'closeHere'),
            array('mera', 'div', 'div'),
            array('maimera', 'Maimera', 'repeatedText', 'resYI', 'GYL',''),
            array('closeHere', 'closeHere', 'closeHere'),
            array( 'moures','div', 'div'),
            array('meLmo', 'MeLmo', 'text', '', ''),
            array('cYSlot', 'CYSlot', 'text', '', ''),
            array('closeHere', 'closeHere', 'closeHere'),
            array('clT', 'clT', 'clT'),
        ),
    );
    $set = array();
    $setK = 0;
    foreach($arr['global'] as $row){
      
        if (in_array('div', $row)){
            $setK++;
        }
        $set[$setK][] = $row;
    }
    var_export($set);
    Code (php):
     
    sarahk, May 13, 2020 IP
  5. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #5
    1) stop saying array so blasted much...

    2) Your code's $k => $v would be returning 'global' and the entire array, not 'div' -- EVER. You should be looking for $v[1] or $v[2] === 'div', though it's hard to say which.
     
    deathshadow, May 21, 2020 IP
  6. sarahk

    sarahk iTamer Staff

    Messages:
    28,897
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #6
    lol

    I hate to think what is really happening there. Looks like the CMS from hell!
     
    sarahk, May 21, 2020 IP