2014-12-17 1 views
0

PHP 배열을 사용하여 무언가를하고 싶습니다. 나는 내가 말할 무언가를 할 수있는 기능을 썼다. 내 입력 배열은 다음과 같습니다.배열을 사용한 특수 작업

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 
     ) 
) 

각 데이터를 MySQL 데이터베이스에 저장하고 싶습니다. 예를 들어, 메시지, id, from-> name, created_time을 함수로 저장하고 싶습니다. foreach 루프를 작성하려고했지만 from-> name을 추가하는 방법을 찾을 수 없으며 stdClass이고 [ 'from'] [ 'name']과 함께 사용할 수 없습니다. 내가 어떻게이 일을 할 수 있는지 말해 줄 수 있니?

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

을 그리고 당신의 기능과 같이 보일 수 있습니다 :

+0

-> 이름은 이름은 속성 – Flavio

+0

가능한 중복 - HTTP : //stackoverflow.com/questions/19495068/convert-stdclass-object-to-array-in-php – Harshit

+0

안녕하세요. 나는 그런 배열을 가지고있다 !!!! 내가 그 근처에서 서로를 사용할 수 없다는 걸 알다시피. 어떻게해야합니까? – simba

답변

0

는 전체 배열 변수, $array로 저장하는 것이이 가정하면, 시도

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. 
}  
+0

나는 모든 데이터를 저장하는 함수를 작성하고 싶다고 말했습니다. 이 작동하지만 함수에서 서로를 저장할 수 없습니다. – simba

+0

'for' 루프를 사용하여 배열 요소를 반복 할 수 있습니다. 예를 들어 답을 편집했습니다. – Mephoros

+0

안녕하세요. ** message, id, form-> name, created_time **이 아니라 동적 인 함수입니다. : | 그것을위한 어떤 방법이 있습니까 : | – simba

0

객체의 속성에 액세스하는 ' -> 연산자 (ArrayAccess 인터페이스를 구현하지 않는 한). 당신은 너무처럼 보낸 사람 이름을 얻어야한다 :

$var[0]->from->name 

가 선택적으로 당신은 배열로 변환 할 수 있습니다 : [ '에서']

$from = (array) $var[0]->from; 
echo $from['name']; 
+0

나는 그것 모두를 안다. 아시다시피 배열을 제공하고 mysql 데이터베이스에 추가하는 동적 함수를 작성하여 지금해야 할 일은 무엇입니까? – simba