PHP, how to echo specific object data from array? - php

I use this in wordpress:
$arr=get_post_meta($post->ID, false);
I receive this array:
Array (
[_edit_last] => Array ( [0] => 2)
[year_completed] => Array ( [0] => 2010 )
[designers] => Array ( [0] => )
[developers] => Array ( [0] => )
[producers] => Array ( [0] => )
[_edit_lock] => Array ( [0] => 1298159324 )
[name_en] => Array ( [0] => game 1)
[name_et] => Array ( [0] => game 2 )
[friends_meta] => Array ( [0] => )
)
How do I echo (no for, foreach, etc please) name_en data? Even print_r ($arr->name_en); doesn't work... I suppose it has to be something like - echo $arr->name_en[0]; ???

It is an array or arrays, so:
print_r($arr['name_en']);
or if you only want to get the data:
echo $arr['name_en'][0];
The -> operator is for accessing properties of objects.

echo $arr['name_en'][0] should work.

this should work
echo $arr[0]['year_completed'];
echo $arr[0]['designers'];
etc

Related

How to Fetch data from Array in WordPress

Array
(
[0] => stdClass Object
(
[meta_id] => 23233
[post_id] => 4467
[meta_key] => first_name
[meta_value] => Daud
)
)
How can I echo post_id from this array for all posts using while or foreach statement?
Array
(
[classic-editor-remember] => Array
(
[0] => classic-editor
)
[_edit_lock] => Array
(
[0] => 1582905950:5
)
[_edit_last] => Array
(
[0] => 5
)
[_thumbnail_id] => Array
(
[0] => 4376
)
[slide_template] => Array
(
[0] => default
)
[_yoast_wpseo_content_score] => Array
(
[0] => 30
)
[_yoast_wpseo_primary_advisor_category] => Array
(
[0] =>
)
[title] => Array
(
[0] => Demo Daniel Wrenne, CFP, ChFC
)
[designation] => Array
(
[0] => Wrenne Financial Planing, LLC Lexington, KY
)
[client_specialities] => Array
(
[0] => Gen Y/Millennials, Medical Professionals
)
[address] => Array
(
[0] => 3223 S LEHI DR
)
[phone_number] => Array
(
[0] => 64646446486
)
[email_address] => Array
(
[0] => demo#demo.com
)
[website_url] => Array
(
[0] => a:3:{s:3:"url";s:23:"https://www.google.com/";s:4:"text";s:20:"View Advisor Profile";s:6:"target";s:4:"none";}
)
[first_name] => Array
(
[0] => Daud
)
[last_name] => Array
(
[0] => Yahya
)
)
And how can I get las_name, first_name, email, address, website url, specialities, designation and title from the above array using and loop like while or foreach loop.
This is less a WordPress question and a basic PHP foreach question.
The first example you have is an Object, so you need to access the properties, e.g. meta_id, post_id like:
// THIS IS JUST AN EXAMPLE. YOUR VARIABLE WILL CHANGE BASED ON HOW YOU GOT THE DATA. `$object_array` is how you got the data to begin with.
foreach( $object_array as $object ) {
$post_id = $object->post_id;
echo $post_id;
}
For your second example, since there is only one array key inside each array key, you would set it up like this:
// Example. you would use whatever you used to get the array to begin with as the `$array`.
foreach ($array as $item ) {
$last_name = $item['last_name'][0];
$first_name = $item['first_name'][0];
....
}

add elements inside elements in associative array

I have an array that looks like this:
array
(
[0] => personA
[1] => personB
)
and I want to add elements to each person like this:
array
(
[0] => personA
(
[0] => elemA
[1] => elemB
[2] => elemC
)
[1] => personB
)
I'm using this code:
foreach($proj as $key => $cat)
{
$proj[$key] = $this->ReturnFolders(WWW_ROOT . "img/proyectos/" . $cat);
}
That function returns an array that looks like this:
array
(
[0] => elemA
[1] => elemB
)
But obviously is not working, I get this result:
array
(
[0] => Array
(
[0] => elemA
[1] => elemB
[2] => elemC
)
[1] => Array
)
Your "like this" structure is not possible. You cannot have a single array key have two different values like that (personA and the sub-array).
You'd have to build a more complex structure:
[0] => array(
'name' => 'personA'
'values' => array('elemA', 'elemB', 'elemC')
)

Choose what values to output from an array

I have this to print values from an array.
<?php foreach ($product->data['attributes'] as $attribute => $option) {
echo '<li>'. t('#attribute: #options', array('#attribute' => $attribute, '#options' => implode(', ', (array)$option))) .'</li>';
} ?>
The code above prints everything from the attributes array:
[products] => Array
(
[0] => stdClass Object
(
[data] => Array
(
[attributes] => Array
(
[Duration] => Array
(
[4] => 2 Years
)
[Anchor Text] => Array
(
[0] => asdf
)
[URL] => Array
(
[0] => asdddddd
)
[Feed ID] => Array
(
[0] => 32845898
)
)
)
)
)
I only want to print the [URL] and [Feed ID]...
echo($product->data['attributes']['URL'])
echo($product->data['attributes']['Feed ID'])
Update:
It looks like the values of your individual "attributes" are, themselves, arrays.
Try the following instead (it combines all the values of the array, separated with commas).
echo(implode(',', (array)$product->data['attributes']['URL']));
echo(implode(',', (array)$product->data['attributes']['Feed ID']));

Strip a IPTC.php result into two variables

I have the following code:
<?php
require_once('IPTC.php');
$iptc = new Image_IPTC('001.jpg');
print_r($iptc);
?>
And it returns:
Image_IPTC Object ( [_sFilename] => 001.jpg [_aIPTC] => Array (
[1#090] => Array ( [0] => %G ) [2#000] => Array ( [0] => ) [2#005]
=> Array ( [0] => TITULO NO WINDOWS, TITLE NO BRIDGE ) [2#080] => Array ( [0] => pictureauthor ) [2#085] => Array ( [0] => photographer )
[2#090] => Array ( [0] => mycity ) [2#095] => Array ( [0] => ST )
[2#101] => Array ( [0] => mycountry ) [2#105] => Array ( [0] =>
IWANTTHIS1 ) [2#116] => Array ( [0] => copyrightinfo ) [2#120] => Array ( [0] => IWANTTHIS2 ) ) [_bIPTCParse] => 1
)
This is a dummy question, but how do I put texts "IWANTTHIS1" and "IWANTTHIS2" into 2 different variables to use like this:
echo "title: $variable1 <br />";
echo "descr: $variable2";
Resulting in:
title: IWANTTHIS1
descr: IWANTTHIS2
I'm pretty shure it's extremelly easy for you guys, but I'm still learning all this. I think it's an array inside an array? Can't figure it out.
Thanks.
$variable1 = $iptc->_aIPTC['2#105'][0];
$variable2 = $iptc->_aIPTC['2#120'][0];
Rodaine's answer was giving me "Fatal error: Cannot use object of type Image_IPTC as array". With some research, the final correct answer is:
$variable1 = $iptc->_aIPTC['2#105'][0];
$variable2 = $iptc->_aIPTC['2#120'][0];
I wouldn't be able to achieve it by myself at all, therefore I'm marking Rodaine's answer as correct.
Thank you very much.

Why is this output blank?

I know I must be doing something simple wrong. When I do this:
echo '<pre>';
print_r($event->when);
echo '</pre>';
I get this:
Array
(
[0] => Zend_Gdata_Extension_When Object
(
[_rootElement:protected] => when
[_reminders:protected] =>
[_startTime:protected] => 2011-06-16T10:00:00.000-05:00
[_valueString:protected] =>
[_endTime:protected] => 2011-06-17T11:00:00.000-05:00
[_rootNamespace:protected] => gd
[_rootNamespaceURI:protected] =>
[_extensionElements:protected] => Array
(
)
[_extensionAttributes:protected] => Array
(
)
[_text:protected] =>
[_namespaces:protected] => Array
(
[atom] => Array
(
[1] => Array
(
[0] => http://www.w3.org/2005/Atom
)
)
[app] => Array
(
[1] => Array
(
[0] => http://purl.org/atom/app#
)
[2] => Array
(
[0] => http://www.w3.org/2007/app
)
)
[gd] => Array
(
[1] => Array
(
[0] => http://schemas.google.com/g/2005
)
)
[openSearch] => Array
(
[1] => Array
(
[0] => http://a9.com/-/spec/opensearchrss/1.0/
)
[2] => Array
(
[0] => http://a9.com/-/spec/opensearch/1.1/
)
)
[rss] => Array
(
[1] => Array
(
[0] => http://blogs.law.harvard.edu/tech/rss
)
)
)
)
)
I'm then trying to get startTime by doing this:
$StartTime = $event->when->startTime;
But I'm not getting anything.
And yet, when I do this:
pr($event->published);
I get this:
Zend_Gdata_App_Extension_Published Object
(
[_rootElement:protected] => published
[_rootNamespace:protected] => atom
[_rootNamespaceURI:protected] =>
[_extensionElements:protected] => Array
(
)
[_extensionAttributes:protected] => Array
(
)
[_text:protected] => 2011-06-15T03:32:14.000Z
[_namespaces:protected] => Array
(
[atom] => Array
(
[1] => Array
(
[0] => http://www.w3.org/2005/Atom
)
)
[app] => Array
(
[1] => Array
(
[0] => http://purl.org/atom/app#
)
[2] => Array
(
[0] => http://www.w3.org/2007/app
)
)
)
)
and I can do this:
$dateAdded = $event->published->text;
echo $dateAdded;
and I see an output...
According to to the official Zend_Gdata_Extension_When documentation, there's a method called getStartTime() which will give you the time.
If you do $event->when[0]->getStartTime() or $event->when[0]->startTime, you'll get the start time.
startTime is marked protected. You can't reference it from outside like you did. There must be a getter function 'getStartTime()' function in that object that would allow you to reference it publicly.
EDIT: Also it is returning an object array - not an single object, so you would need to reference the it like: $event[0]->getterFunction() or loop through the array with a foreach accessing the individual objects in the loop

Categories