2011-01-22 9 views
5

मेरे सरणी इसडेटा प्राप्त करने के लिए ऑब्जेक्ट को सरणी में कैसे परिवर्तित करें?

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 

की तरह आ रहा है जब मैं इस

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

की तरह लिखने लेकिन मेरी समस्या यह है कि कैसे मैं उस वस्तु सरणी के अंदर मूल्यों को प्राप्त कर सकते हैं।

कृपया मदद करें। थेंक्स print_r ($ arrDt);

उत्तर

4

यह सिर्फ सामान्य वस्तु का उपयोग कर सकते है:

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

लेकिन यह है कि आप क्या करना चाहते हैं पर निर्भर करता है। मैं get_posts उदाहरणों पर एक नज़र डालने का सुझाव देता हूं। वर्तमान संदर्भ में पोस्ट सामग्री लोड करने के लिए वे setup_postdata का उपयोग करते हैं। यदि आप पोस्ट प्रदर्शित करना चाहते हैं, तो शायद यह क्लीनर समाधान है।

+0

ओह मेरे भगवान आप फेलिक्स यू मेरी समस्या हल धन्यवाद thnx thnx – rajeshrt

+0

@user आपके प्रश्न का उत्तर को हल करती है, तो आप हरी सही का निशान रूपरेखा टिक टिक द्वारा इसे स्वीकार करने के लिए प्रोत्साहित किया जाता इसके पास वाला। यदि कई उत्तर हैं, तो उस व्यक्ति पर टिकटें जो आपके लिए सबसे उपयोगी था और किसी अन्य उत्तर को ऊपर उठाने पर विचार करें। – Gordon

3

यह बहुत सरल है:

आप किसी सरणी Array ([0] => stdClass Object ([ID]

इस सरणी एक कुंजी है, कि कुंजी "[0]" (लेकिन अधिक कुंजी मौजूद हो सकता है)) तक पहुँचना से पहचाना जा सकता है :

:

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

या, यदि आप (, $ मूल्य [ 'आईडी'] की तरह उदाहरण के लिए) सरणी वस्तु परिवर्तित करना चाहते हैं, तो आप सिर्फ यह की जरूरत है

0

आप wp_get_recent_posts()के बजाय get_posts() उपयोग कर सकते हैं। wp_get_recent_posts() फ़ंक्शन ऑब्जेक्ट सरणी के बजाय सामान्य सरणी देता है, फिर फ़ोरैच लूप का उपयोग करके आप किसी सरणी के किसी भी मान तक पहुंच सकते हैं।

1

मेरे मामले में यह था:

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; 
} 
संबंधित मुद्दे

 संबंधित मुद्दे