0

Does anyone know how to extract arrays that of a certain type from a larger array? For example, consider these results from a var_dump():

     [0]=>
      array(5) {
        ["post"]=>
        string(60) "Paris"
        ["capacity"]=>
        string(5) "58515"
        ["region"]=>
        string(60) "WHA"
        ["seats"]=>
        string(2) "55"
        ["growth"]=>
        string(4) "3880"
      }
      [1]=>
      array(5) {
        ["post"]=>
        string(60) "Tel Aviv"
        ["capacity"]=>
        string(6) "530910"
        ["region"]=>
        string(60) "NEA"
        ["seats"]=>
        string(2) "65"
        ["growth"]=>
        string(4) "3267"
      }
      [2]=>
      array(5) {
        ["post"]=>
        string(60) "Paris"
        ["capacity"]=>
        string(6) "962115"
        ["region"]=>
        string(60) "EUR"
        ["seats"]=>
        string(2) "11"
        ["growth"]=>
        string(4) "2660"
      }

I am sure this may seem simple to some that live in the 5th dimension, but I am having a devil of a time extracting arrays where region = "WHA" within my foreach() to try and build a separate array with just the WHA data.

foreach($data as $keys => $datums){
    //Planning to build JSON string
    if($datums['region'] == "WHA"){
        //Some processing
    }
}

The result should look like this:

  [0]=>
  array(5) {
    ["post"]=>
    string(60) "Paris"
    ["capacity"]=>
    string(5) "58515"
    ["region"]=>
    string(60) "WHA"
    ["seats"]=>
    string(2) "55"
    ["growth"]=>
    string(4) "3880"
  }

I have tried all kinds of different things like looking at array_filter that are just not working for me, and I am just concerned about what the right way looks like. Any ideas? Much appreciated.

1
  • I don't really see where you are stuck? You can do it of course like you already do it with a foreach loop and add the array: if($datums['region'] == "WHA"){ $result[] = $datums; }. Or also with array_filter(), e.g. $result = array_filter($yourArray, function($v){return $v["region"] == "WHA";}); (btw: I live in the 4th dimension :) Commented Jan 7, 2016 at 23:17

3 Answers 3

1

If I understand correctly, I believe you want to build a new array that only contains the elements from the first array that pass a test. The following example does that and produces a JSON encoded string of that data:

$new = Array();
foreach($data as $key => $datum){
    //Planning to build JSON string
    if ( $datum['region']=='WHA') {
        $new[] = $datum;
    }
}
$out = json_encode($new);
Sign up to request clarification or add additional context in comments.

2 Comments

I used array_push($new, $datums); and got what I wanted as there are many arrays with WHA as the region. I know I was close. Must have been tired :). Thanks for the tip @Octopus.
array_push($haystack,$needle) does exactly the same as $haystack[] = $needle
1

You could also encapsulate this in a function

<?php
function filterArray($arr, $index, $filter) {
    foreach( $arr as $key=>$elem ) {
        if ( $elem[$index] != $filter ) {
            unset($arr[$key]);
            }
        }
    return $arr;
    }
?>

If $YourArray is the OP's array, you could use this to filter out any item in the array which does not have an indexed element for "region" which equals "WHA"

$filtered = filterArray($yourArray, "region", "WHA");

An alternative version of this would use an ampersand (&) to pass a pointer to the original array. For example, if you used this as your function declaration:

function filterArray(&$arr, $index, $filter) {

then you no longer need to set a destination for the returned array, because the original array actually gets modified directly by the function.

filterArray($yourArray, "region", "WHA");

Comments

1

Think this should fix your code.

foreach($data as $keys => $datums){
    //Planning to build JSON string
    if(isset($datums["region"]) && strcmp($datums["region"], "WHA") === 0){
        //Some processing
     }
 }

3 Comments

Why should OP use double quotes for the array index? Makes no difference if ' or "
I know it should not matter, but I have had problems with it :)
You can use the faster === if you don't need to know which string is greater. See this link

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.