2011-01-22 11 views
5

Mi gama viene como esto¿Cómo convertir objeto a matriz para obtener los datos?

Array ([0] => stdClass Object ([ID] => 578 [post_author] => 1 [post_date] => 2011-01-18 07:23:17 [post_date_gmt] => 2011-01-18 07:23:17 [post_content] => Home WordPress is web software you can use to create a beautiful website or blog. We like to say that WordPress is both free and priceless at the same time. The core software is built by hundreds of community volunteers, and when you’re ready for more there are thousands of plugins and themes available to transform your site into almost anything you can imagine. Over 25 million people have chosen WordPress to power the place on the web they call “home” — we’d love you to join the family [post_title] => second post [post_excerpt] => [post_status] => publish [comment_status] => open 

cuando escribo como esto

$myposts = get_posts($args); 
$arrDt = (array) $myposts; 
print_r($arrDt); 

pero mi problema es cómo puedo obtener los valores dentro de esa matriz de objetos.

ayuda. Thnx print_r ($ arrDt);

Respuesta

4

Es simplemente normal acceso a objetos:

$obj = $arrDt[0]; 
echo $obj->ID; 
echo $obj->post_author; 
// etc. 

Pero depende de lo que quiere hacer. Sugiero echar un vistazo a los ejemplos get_posts. Usan setup_postdata para cargar el contenido del mensaje en el contexto actual. Si desea mostrar la publicación, esta es probablemente la solución más limpia.

+0

ohh mi dios gracias Felix u resuelto mi problema thnx thnx – rajeshrt

+0

@user si una respuesta resuelve tu pregunta, se le anima a aceptarlo, marcando el contorno marca verde junto a él. Si hay varias respuestas, marque la que fue más útil para usted y considere avatar otras respuestas. – Gordon

3

Es muy simple:

tiene una matriz Array ([0] => stdClass Object ([ID]

Esta matriz tiene una clave, que puede ser identificado por el "[0]" (pero pueden existir más teclas)) Acceso a la tecla :

foreach ($arrDt as $value): //Look, whe are inside the first key. (currently is '0'). 
    echo $value->ID; 
    echo $value->post_author; 
endforeach; 

O, si desea convertir objeto a la matriz (como valor $ [ 'ID'], por ejemplo), sólo necesita esto:

function objectToArray($obj) 
    { 
     if (is_object($obj)): 
      $object = get_object_vars($obj); 
     endif; 

     return array_map('objectToArray', $object); // return the object, converted in array. 
    } 

$objArray = objectToArray($arrDt); 
print_r($objArray); 
0

puede usar wp_get_recent_posts() en lugar de get_posts(). La función wp_get_recent_posts() devuelve una matriz normal en lugar de una matriz de objetos, y luego mediante el bucle foreach puede acceder a cualquier valor de una matriz.

1

En mi caso fue:

foreach ($returnedObject as $row) { 
    $sub_array = ''; 
    $sub_array['ID'] = $row->data->ID; 
    $sub_array['user_login'] = $row->data->user_login; 
    $sub_array['display_name'] = $row->data->display_name; 
    $sub_array['user_email'] = $row->data->user_email; 
    $sub_array['user_registered'] = $row->data->user_registered; 
    $main_array[] = $sub_array; 
} 
Cuestiones relacionadas