Jump to content

get specific values in multidimensional array


Crusader

Recommended Posts

[code]Array
(
    [0] => Array
        (
            [sector] => 18
            [owner] => 1
            [ship] => 4
            [mothership] => carrier
            [name] => scout 1
        )

    [1] => Array
        (
            [sector] => 39
            [owner] => 1
            [ship] => 3
            [mothership] => carrier
            [name] => fighter 2
        )

    [2] => Array
        (
            [sector] => 39
            [owner] => 1
            [ship] => 3
            [mothership] => carrier
            [name] => fighter 1
        )

    [3] => Array
        (
            [sector] => 40
            [owner] => 1
            [ship] => 3
            [mothership] => carrier
            [name] => fighter 3
        )

    [4] => Array
        (
            [sector] => 40
            [owner] => 2
            [ship] => 3
            [mothership] => carrier 2
            [name] => fighter 2
        )

    [5] => Array
        (
            [sector] => 40
            [owner] => 2
            [ship] => 3
            [mothership] => carrier 2
            [name] => fighter 3
        )

)[/code]

How would I filter through this and get only the arrays with sector equalling zero? I've been trying array_filter filter but I'm having a hard time figuring it out since you have to do something like $variable[0]['sector'] to get a value.
Try something like this (untested):

[code]<?php
$new_array = array();
for ($i=0;$i<count($orig);$i++)
    if ($orig[$i]['sector'] == 0) $new_array[] = $orig[$i];
echo '<pre>' . print_r($new_array,true) . '</pre>';
?>[/code]

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.