0

OK so I've got an array which looks like this

Array
(
[0] => stdClass Object
    (
        [ipID] => 1
        [countryID] => 13
        [beginIP] => 1.0.0.0
        [endIP] => 1.0.0.255
        [netMask] => 24
        [beginIPNum] => 16777216
        [endIPNum] => 16777471
    )

[1] => stdClass Object
    (
        [ipID] => 2
        [countryID] => 44
        [beginIP] => 1.0.1.0
        [endIP] => 1.0.1.255
        [netMask] => 24
        [beginIPNum] => 16777472
        [endIPNum] => 16777727
    )

[2] => stdClass Object
    (
        [ipID] => 3
        [countryID] => 44
        [beginIP] => 1.0.2.0
        [endIP] => 1.0.3.255
        [netMask] => 23
        [beginIPNum] => 16777728
        [endIPNum] => 16778239
    )
)

Now I want to get each set of data (ipID, countryID etc) and write each one to a database, but I'm stuck

How do I get each set of data out ?

    foreach($data as $ipRecord ) {

        $a = array();
        $a['ipID'] = $ipRecord['ipID'];
        $a['countryID'] = $ipRecord['countryID'];

        echo $a;

    }

Using the above throws a Fatal error: Cannot use object of type stdClass as array

1
  • Your error is the result of you trying to access an object as an array. You need to use the $ipRecord-> ipID format instead. Commented Dec 17, 2013 at 16:28

3 Answers 3

2

You should get in the way because it is stdClass.

foreach($Array as $key => $value)
{
    $ipID = $value->ipID;
    $etc = $value->etc;
    $countryID = $value->countryID;

    // bla bla
}
Sign up to request clarification or add additional context in comments.

Comments

1

Assuming you're using PHP here, you could use a forEach:

forEach($arr as $item) {
    // Run your database insertion here
}

See: http://www.php.net/manual/en/control-structures.foreach.php

Basically, just access the items in your array individually and run the appropriate database operations on them.

However, honestly, it's going to depend on the language and database you are using.

4 Comments

yes I'm using php, I get this error when I try the above Fatal error: Cannot use object of type stdClass as array
@Rich yea you can't use your object as an array. You need to pass in the actual array, and then interact with your object in $item (or whatever you called it). Alternatively, you could just use a for-loop to access the data.
bare in mind that this is a multi-dimentional array and the above will return 3 seperate arrays
@LiamSorsby it's an array with 3 objects, you will be accessing the objects not another array.
1

You probably don't understand the difference between arrays and objects.

The way you access Arrays

$foo["key"]

The way you access properties of a class (ie stdclass)

$foo->key

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.