0

I want to do something with PHP arrays. I wrote a function to do something which I will say. My input array is:

Array
(
    [0] => stdClass Object
        (
            [message] => Automated XML Site Map Generator
http://pastebin.com/xjJe38dp
            [id] => 103114753133019_371405176303974
            [from] => stdClass Object
                (
                    [id] => 735588533186829
                    [name] => Mohammad Mostafa Shahreki
                )

            [created_time] => 2013-04-26
            [updated_time] => 2013-04-26
        )

    [1] => stdClass Object
        (
            [message] => Simple but powerful DB class
http://pastebin.com/1qgxUrwX
            [id] => 103114753133019_371404696304022
            [from] => stdClass Object
                (
                    [id] => 735588533186829
                    [name] => Mohammad Mostafa Shahreki
                )

            [created_time] => 2013-04-26
            [updated_time] => 2013-04-26
        )

    [2] => stdClass Object
        (
            [message] => Convert Existing DB to Unicode
http://pastebin.com/pHu08cPs
            [id] => 103114753133019_371404609637364
            [from] => stdClass Object
                (
                    [id] => 735588533186829
                    [name] => Mohammad Mostafa Shahreki
                )

            [created_time] => 2013-04-26
            [updated_time] => 2013-04-26
        )

    [3] => stdClass Object
        (
            [message] => thanks mosthafa ,,,, for adding me to this group
            [id] => 103114753133019_103333343111160
            [from] => stdClass Object
                (
                    [id] => 10155092057165556
                    [name] => HalF PixeL
                )

            [created_time] => 2011-10-11
            [updated_time] => 2011-11-21
        )
)

I want to save each data in my MySQL database. For example I want to save message,id,from->name,created_time by a function. I tried to write a foreach loop but I couldn't find a way to add from->name, it's a stdClass and can't use it with ['from']['name']. could you tell me how I can do this?

Michael
  • 8,446
  • 4
  • 26
  • 36
simba
  • 3
  • 3

2 Answers2

0

Assuming you have that entire array saved as a variable, $array, try:

$array[0]->from->name;

And your function can look something like so:

for ($i = 0; $i < $inputArray.count(); $i++) {
  $message = $inputArray[$i]->message;
  $id = $inputArray[$i]->id;
  $name = $inputArray[$i]->from->name;
  $created = $inputArray[$i]->created_time;

  // Store the record as you want.
}

// Alternatively, a foreach loop
// See: http://php.net/manual/en/control-structures.foreach.php
foreach ($inputArray as $entry) {
  $message = $entry->message;
  $id = $entry->id;
  $name = $entry->from->name;
  $created = $entry->created_time;

  // Store the record as you want.
}      
Meetarp
  • 2,331
  • 1
  • 23
  • 31
  • I told you i want to write a function that save all data. this works but i cant save each other in a function – simba Dec 17 '14 at 17:26
  • You can use a `for` loop to loop through the elements of the array. I've edited my answer to include an example. – Meetarp Dec 17 '14 at 17:30
  • hey dear. it's not Just ** message,id,form->name,created_time ** it's a dynamic function. :| is there any way for it:| – simba Dec 17 '14 at 17:43
  • I'm sorry, I'm not quite understanding what you mean. Are you saying that there can be a variable number of properties for each object in the array? – Meetarp Dec 17 '14 at 17:46
0

Objects' attributes are accessed with the '->' operator (unless you implement ArrayAccess interface). You should get your sender name like so:

$var[0]->from->name

Optionally you can convert it to array:

$from = (array) $var[0]->from;
echo $from['name'];
Waldson Patricio
  • 1,489
  • 13
  • 17
  • I know all of it. as you know, I want to write a dynamic function that gives an array and add it into mysql database so what should i do now ? – simba Dec 17 '14 at 17:36