4

I have array with 45 elements. I need to make from it multidimensiona array. This is how it looks like:

Array45: [
         0 => {
              +'MODEL': "AAA"
              +'PRICE': 12.00
              +'SUBMODEL_NAME': "abc"
              }
         1 => {
              +'MODEL': "AAA"
              +'PRICE': 12.00
              +'SUBMODEL_NAME': ""
              }
         2 => {
              +'MODEL': "BBB"
              +'PRICE': 12.00
              +'SUBMODEL_NAME': "bcd"
              }
         3 => {
              +'MODEL': "AAA"
              +'PRICE': 12.00
              +'SUBMODEL_NAME': ""
              }
]

And now: If record doesn't have 'SUBMODEL_NAME' I need to put it in one array with previous record which has 'SUBMODEL_NAME'. And in this case it should look like:

Array: [
         0 => [ 
              0 => {
                    +'MODEL': "AAA"
                    +'PRICE': 12.00
                    +'SUBMODEL_NAME': "abc"
                   }
              1 => {
                    +'MODEL': "AAA"
                    +'PRICE': 12.00
                    +'SUBMODEL_NAME': ""
                   }

         1 => [ 
              0 => {
                    +'MODEL': "BBB"
                    +'PRICE': 12.00
                    +'SUBMODEL_NAME': "bcd"
                   }
              1 => {
                    +'MODEL': "AAA"
                    +'PRICE': 12.00
                    +'SUBMODEL_NAME': ""
                   }
]

etc.

I'm in foreach loop where I try to put every next record without SUBMODEL_NAME to previous but I get stuck. It's something like this:

$j = -1;
$newArray = [];

foreach($items as $item){

        if ($index->SUBMODEL_NAME) {
            $j++;
            $newArray [$j][] = $index ;
        }
  }

EDIT Thank you all for help! I've implement soulution proposed by @user1309690 and it looks like it works perfectly. Thank for you'r help and time

2
  • You might want to correct the spelling in your title Jakmen. Commented Jul 5, 2019 at 7:55
  • @jakmen Grouping doesn't make much sense to me here. You need those elements which have SUBMODEL_NAME to come before the ones which don't have. Okay. But, why are they inside an individual subgroup of size 2? Also, how did one qualify for any individual subgroup in the output? Commented Jul 5, 2019 at 8:20

2 Answers 2

3

Try this.

$j = -1;
$newArray = [];
foreach($items as $item){
    if ($item['SUBMODEL_NAME']) {
        $j++;
    }
    $newArray [$j][] = $item ;
}
Sign up to request clarification or add additional context in comments.

Comments

1

Another way to do it-

$result = [];
foreach($array as $k=>$v){
    $i=0;
    if($v['SUBMODEL_NAME']==''){
        $i++;
        $k = $k-1;
    }
    $result[$k][$i]= $v;
}
$result = array_values($result);
print_r($result);

DEMO: https://3v4l.org/3aTgR

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.