0

I have array like this:

     array(4) {
  [0]=>
  array(2) {
    [0]=>
    string(2) "L2"
    [1]=>
    string(5) "Apple"
  }
  [1]=>
  array(2) {
    [0]=>
    string(2) "L3"
    [1]=>
    string(3) "Cat"
  }
  [2]=>
  array(2) {
    [0]=>
    string(2) "L2"
    [1]=>
    string(6) "Orange"
  }
  [3]=>
  array(2) {
    [0]=>
    string(2) "L3"
    [1]=>
    string(3) "Dog"
  }
}

I want make a conclusion:
a) what are content of L2?
b) what are content of L3?

So I make array $L2 and $L3.
But, I have no idea how to push some array in one array.
I already try this one:

for($j=0; $j<count($arrL); $j++){
    if($arrL[$j][0] == "L2"){
            $L2[] = $arrL[$j][1];
    }else if($arrL[$j][0] == "L3"){
            $L3[] = $arrL[$j][1];
    }
}

But, the result is:

array(0) {
}  

you have any idea?

2
  • Your code runs just fine for me using an array constructed just like the one shown for $arrL. Is it possible there's something else not working correctly? are you correctly testing $L2 and $L3? Is it possible $arrL is not constructed as shown? Commented Feb 5, 2015 at 2:53
  • Yeah don't see any errors. Commented Feb 5, 2015 at 3:51

1 Answer 1

1

Your code is just fine. If I understand what do you want correctly then perhaps this might help you out. This is the code that I am using:

$arrL = array(
        0 => array(
                    0 => "L2", 1 => "Apple"
                ),
        1 => array(
                    0 => "L3", 1 => "Cat"
                ),
        2 => array(
                    0 => "L2", 1 => "Orange"
                ),
        3 => array(
                    0 => "L3", 1 => "Dog"
                )
    );

for($j=0; $j<count($arrL); $j++){
    if($arrL[$j][0] == "L2"){
        $L2[] = $arrL[$j][1];
    }else if($arrL[$j][0] == "L3"){
        $L3[] = $arrL[$j][1];
    }
}

echo 'My L2 Array: ';
print_r($L2);

echo 'My L3 Array: ';
print_r($L3);

And here is the output:

My L2 Array: Array ( [0] => Apple [1] => Orange ) My L3 Array: Array ( [0] => Cat [1] => Dog )
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.