0

The array:

Array(   
    [0] => Array(         
        [0] => Array(
            [type] => CHAMPION_KILL
            [timestamp] => 888477
            [position] => Array(
                    [x] => 3001
                    [y] => 13152
                )
            [killerId] => 5
            [victimId] => 6
            [assistingParticipantIds] => Array(
                [0] => 1
                [1] => 4
                )
             )

        [1] => Array(
            [type] => ITEM_PURCHASED
            [timestamp] => 2357
            [participantId] => 10
            [itemId] => 3303
        )

    )
    [1] => Array(         
        [0] => Array(
            [type] => CHAMPION_KILL
            [timestamp] => 889522
            [position] => Array(
                [x] => 13123
                [y] => 4564534
            )

            [killerId] => 1
            [victimId] => 4
            [assistingParticipantIds] => Array(
                [0] => 8
                [1] => 2
            )
        )

        [1] => Array(
            [type] => ITEM_PURCHASED
            [timestamp] => 2507
            [participantId] => 8
            [itemId] => 4750
        )
    )
)

What i want to do:

Array(   
    [0] => Array(
        [type] => CHAMPION_KILL
        [timestamp] => 888477
        [position] => Array(
            [x] => 3001
            [y] => 13152
        )
        [killerId] => 5
        [victimId] => 6
        [assistingParticipantIds] => Array(
        [0] => 1
        [1] => 4
        )
    )
    [1] => Array(
        [type] => CHAMPION_KILL
        [timestamp] => 889522
        [position] => Array(
            [x] => 13123
            [y] => 4564534
        )

        [killerId] => 1
        [victimId] => 4
        [assistingParticipantIds] => Array(
            [0] => 8
            [1] => 2
        )
    )
)

I want to make an array including all arrays that has [type] => CHAMPION_KILL. But it must be dynamic because the main array will not always be same. So $resultarray= array_merge($array[0][0],$array[1][0]); wont work.

5
  • 1
    so loop on the main array, test that type field on each iteration, and save the ones you want. Commented Jun 7, 2016 at 17:11
  • @MarcB can you give me a example code or something? how can i test "type" field on each iteration? Commented Jun 7, 2016 at 17:13
  • foreach(...) { if ($element['type'] == 'champion']) { $data[] = $element } Commented Jun 7, 2016 at 17:14
  • Undefined index: type in if ($element['type'] == 'champion']) line Commented Jun 7, 2016 at 17:21
  • Possible duplicate of PHP multi dimensional array search Commented Jun 7, 2016 at 17:48

2 Answers 2

2

Loop through your array and search what you want:

$resultArray = array();
foreach($yourMainArray as $arrays) {
  if (is_array($arrays)) {
    foreach($arrays as $array) {
      if (isset($array['type']) && $array['type'] == 'CHAMPION_KILL') {
        $resultArray[] = $array;
      }
    }
  }
}
Sign up to request clarification or add additional context in comments.

8 Comments

I get Notice: Undefined index: type and Warning: Invalid argument supplied for foreach(). notice: undefined index is in if ($array['type'] == 'CHAMPION_KILL') { line. warning is in foreach ($arrays as $array) { line
You're 1 array to deep. The second foreach isn't needed
Nope, there should be 2 foreach. Look at data. maybe not always there is type index? If not always change it into: if (isset($array['type']) && $array['type'] == 'CHAMPION_KILL')
@nospor Warning: Invalid argument supplied for foreach() #in 2nd foreach line
@YusufDevranlı I've updated my answer. You really have strange your data
|
2

I suggest you iterate through the array and check if the childs meet the criteria.

Example:

$resultArray = [];

// Iterates through the main array
foreach ($mainArray as $secondaryArray) {

    // Verifies if it has the right structure
    if (is_array($secondaryArray)) {

        // Iterates through the secondary arrays
        foreach ($secondaryArray as $finalArray) {

            // Checks if the 'type' key exists and if the value is the one you want
            if (array_key_exists('type', $finalArray) && 'CHAMPION_KILL' === $finalArray['type']) {

                // Add the array you need to the array result
                array_push($resultArray, $finalArray);
            }
        }
    }
}

You just have to replace $mainArray with the one you want. And $resultArray will be what you expect.

You can check a code snippet here: PHPFiddle Code

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.