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.

Mathlight
  • 6,436
  • 17
  • 62
  • 107
Yusuf Devranlı
  • 159
  • 1
  • 2
  • 8

2 Answers2

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;
      }
    }
  }
}
Mathlight
  • 6,436
  • 17
  • 62
  • 107
nospor
  • 4,190
  • 1
  • 16
  • 25
  • 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 – Yusuf Devranlı Jun 07 '16 at 17:16
  • You're 1 array to deep. The second foreach isn't needed – Mathlight Jun 07 '16 at 17:18
  • 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 Jun 07 '16 at 17:21
  • @nospor Warning: Invalid argument supplied for foreach() #in 2nd foreach line – Yusuf Devranlı Jun 07 '16 at 17:26
  • I'm sorry, you're correct. Was looking at the second code example. in that case a simple `isset` should indeed do the trick :D – Mathlight Jun 07 '16 at 17:27
  • 1
    @YusufDevranlı I've updated my answer. You really have strange your data – nospor Jun 07 '16 at 17:29
  • @Mathlight im getting Warning: Invalid argument supplied for foreach() in second foreach line help. – Yusuf Devranlı Jun 07 '16 at 17:29
  • @nospor oh i found out why that wasnt working. in original code it was more deep in arrays. so i added more foreachs and it worked. your solution was right for the array i wrote there. i didnt wrote the actual array because it is very huge. thank you very much – Yusuf Devranlı Jun 07 '16 at 17:50
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

Daniel Duarte
  • 103
  • 2
  • 8