PHP Accessing Values quickly an Arrray holding objects which one element of the object has an array

StackOverflow https://stackoverflow.com/questions/17898566

Вопрос

I have an object which stores an associative array

The objects consist of

class mainObj {
public $name = 0;
public $address = '';
public $someArray = array();
    }

These objects are stored in an array.

objArray[] = obj1;
objArray[] = obj2

My question is: How can I best and easily access the KEY and VALUE of that associative array inside of the object which is itself stored in yet another array?

I can get the keys and values back through iteration...

for ($i=0; $i<count($objArray); $i++)
{
$tempArray = $objArray[$i]->someArray;
    foreach ($tempArray as $key => $value) 
    {
        echo "Key: $key; Value: $value\n";
    }
   }

Re-writing the objects to be an arrays is at this stage possibly too problematic...

In PHP, is there a quicker way to access the associative array keys and values from this array of objects storing the associative array?

Thank you kindly for your answers in advance... Marv

Это было полезно?

Решение

In this case I would create a getter method in your mainObj class, something like this:-

class mainObj
{
    public $name = 0;
    public $address = '';
    public $someArray = array(1 => 'one' ,2 => 'two', 3 => 'three', 4 => 'four');

    public function getSomeArray()
    {
        return $this->someArray;
    }
}

Then you can access $someArray quite easily:-

$obj1 = new mainObj();
$obj1->name = 1;

$obj2 = new mainObj();
$obj2->name = 2;

$obj3 = new mainObj();
$obj3->name = 3;

$objArray = array($obj1, $obj2, $obj3);

foreach($objArray as $obj){
    echo "Object: {$obj->name}<br/>\n";
    foreach($obj->getSomeArray() as $key => $value){
        echo "Key: $key , Value: $value<br/>\n";
    }
}

Output:-

Object: 1
Key: 1 , Value: one
Key: 2 , Value: two
Key: 3 , Value: three
Key: 4 , Value: four
Object: 2
Key: 1 , Value: one
Key: 2 , Value: two
Key: 3 , Value: three
Key: 4 , Value: four
Object: 3
Key: 1 , Value: one
Key: 2 , Value: two
Key: 3 , Value: three
Key: 4 , Value: four

Другие советы

The following does what you want:

foreach ($objArray as $key => $value){
    foreach ($value->someArray as $key => $value){
        echo "Key: $key; Value: $value\n";
    }
}

However, that's assuming a structure like the following. If your structure differs, you'll need to try a slightly different approach:

Array
(
    [0] => mainObj Object
        (
            [someArray] => Array
                (
                    [0] => 3
                    [1] => 4
                    [2] => 5
                )
        )
    [1] => mainObj Object
        (
            [someArray] => Array
                (
                    [0] => 6
                    [1] => 7
                    [2] => 8
                )
        )
    [2] => mainObj Object
        (
            [someArray] => Array
                (
                    [0] => 9
                    [1] => 20
                    [2] => 11
                )
        )
)
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top