mohsen barati
mohsen barati

Reputation: 73

php put two array indexs in one subarray if have a same value

we have an array like this :

Array
(
    [0] => Array
        (
            [value] => aaa
            [parent_id] => 5
        )

    [1] => Array
        (
            [value] => bbb 
            [parent_id] => 3
        )

    [2] => Array
        (
            [value] => ccc
            [parent_id] => 3
        )

)

as you can see 2 index have same [parent_id] , we need convert this array to this

Array
(
    [0] => Array
        (
            [parent_id] => 5
            [sub] => Array(
                             [0] =>(
                                      [value] => aaa
                                   )
                         )
        )

    [1] => Array
        (
            [parent_id] => 3
            [sub] => Array(
                             [0] =>(
                                      [value] => bbb
                                   )
                             [1] =>(
                                     [value] => ccc
                                  )
                         )
        )

)

in php we used this functions :

foreach ($Array as $item) {
         $item['subs'] = array();
         $indexedItems[$item['parent_id']] = (object) $item;
}
for($i=0; $i<count($Array); $i++){
     if($Array[$i]['parent_id'] == $Array[$i-1]['parent_id']){
            $indexedItems[$item['parent_id']]->subs[]=$Array[$i]['value'];
      }
}

but it doesnot works , can you help us to do that , please ?

Upvotes: 1

Views: 101

Answers (1)

DarkBee
DarkBee

Reputation: 15625

If u realy want that complicated array you gave as example you have to track the parent_id. This is how :

<?php

$foo = [
        [
            'value' => 'aaa',
            'parent_id' => 900,
        ],
        [
            'value' => 'aaa',
            'parent_id' => 813,
        ],
        [
            'value' => 'aaa',
            'parent_id' => 900,
        ],
        [
            'value' => 'aaa',
            'parent_id' => 813,
        ],      
    ];
$indexed = [];
foreach($foo as $f) {
    $index = getParentIndex($indexed, $f['parent_id']);
    if ($index === null) {
        $indexed[] = [
                        'parent_id' => $f['parent_id'],
                        'subs'      => [
                                        ['value' => $f['value'] ],
                                       ],
                     ];
    }else{
        $indexed[$index]['subs'][] = [ 'value' => $f['value'], ];
    }
}

function getParentIndex($array, $parent_id) {
    for($i=0;$i<count($array);$i++) {
        if ($array[$i]['parent_id'] == $parent_id) return $i;
    }
    return null;
}

var_dump($indexed);

However, this looks a very complicated array to work with imho. I would suggest the following snippet :

$indexed = [];

foreach($foo as $f) {
    if (!isset($indexed[$f['parent_id']])) $indexed[$f['parent_id']] = [];
    $indexed[$f['parent_id']][] = $f['value'];
}
var_dump($indexed);

This will output an array similar to :

array (size=2)
  900 => 
    array (size=2)
      0 => string 'aaa' (length=3)
      1 => string 'aaa' (length=3)
  813 => 
    array (size=2)
      0 => string 'aaa' (length=3)
      1 => string 'aaa' (length=3)

Upvotes: 1

Related Questions