How to Fetch data from Array in WordPress - php

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];
....
}

Related

Dynamically put condition with the array as per the array value

I have the below array. I want to put condition dynamically as per the sections are coming.
stdClass Object
(
[content_form] => Array
(
[0] => genre
[1] => cast
[2] => category
)
[content_type] => stdClass Object
(
[genre] => Array
(
[0] => History
[1] => ACTION
[2] => ROMANTIC
)
[cast] => Array
(
[0] => 13128
[1] => 13127
)
[category] => Array
(
[0] => 4119
[1] => 4118
[2] => 4081
)
)
[conditions] => Array
(
[0] => OR
[1] => AND
)
)
In the above array for content_form there are 3 array values and in 0th index genre, 1st index cast and 2nd index category present.
Similarly for content_type there are 3 array values present and in the last array conditions present.
My requirement is that as per the section coming I want to put condition with them.
Example- ((genre OR cast) AND category)
similarly if my output is like below then the condition will be
Example - (genre OR cast)
[content_form] => Array
(
[0] => genre
[1] => cast
)
[content_type] => stdClass Object
(
[genre] => Array
(
[0] => History
[1] => ACTION
[2] => ROMANTIC
)
[cast] => Array
(
[0] => 13128
[1] => 13127
)
)
[conditions] => Array
(
[0] => OR
)
)
And if my output is like the below then there will no condition with any other key and it will return the simple result.
stdClass Object
(
[content_form] => Array
(
[0] => cast
)
[content_type] => stdClass Object
(
[cast] => Array
(
[0] => 13128
[1] => 13127
)
)
[conditions] => Array
(
)
)
How I can do this dynamically as per the sections are coming as per sequence with the array of content_type and conditions?
We have done this. Please check:
$jsonData = '{"content_form":["genre","cast","category", "prakash"],"content_type":{"genre":["History","ACTION","ROMANTIC"],"cast":["13128","13127"],"category":["4119","4118","4081"]},"conditions":["OR","AND","OR"]}';
$contentIds = array(
'genre'=>array(1,2,3),
'cast'=>array(1,2,4),
'category'=>array(3,2,7),
'prakash'=>array(1,2,3,8,9)
);
$arrayDecode = json_decode($jsonData,true);
$result = array();
foreach($arrayDecode['conditions'] as $key=>$val){
if($result){
$secondArr = $contentIds[$arrayDecode['content_form'][$key+1]];
$result = array_merge($result, $secondArr);
} else {
$firstArr = $contentIds[$arrayDecode['content_form'][$key]];
$secondArr = $contentIds[$arrayDecode['content_form'][$key+1]];
$result = array_merge($firstArr, $secondArr);
}
if($val=='OR'){
$result = array_unique($result);
} else {
$result = array_diff_assoc($result, array_unique($result));
}
}
$result = array_values($result);
print_r($result);
Let me know this is your requirement or not?

Read data from a single record OR more records

I have a query which gives data from one person or from more persons.
The data for one person is presented with JSON as:
Array ( [0] => Array (
[a2a_Person] => Array (
[pid] => Person1
[a2a_PersonName] => Array (
[a2a_PersonNameFirstName] => Array ( [a2a_PersonNameFirstName] => Aagje )
[a2a_PersonNameLastName] => Array ( [a2a_PersonNameLastName] => Baltus ) ) ....
If there are more persons then the data is presented as
Array ( [0] => Array (
[a2a_Person] => Array (
[0] => Array (
[pid] => Person1 [a2a_PersonName] => Array (
[a2a_PersonNameFirstName] => Array ( [a2a_PersonNameFirstName] => Walig )
[a2a_PersonNamePatronym] => Array ( )
[a2a_PersonNamePrefixLastName] => Array ( )
[a2a_PersonNameLastName] => Array ( [a2a_PersonNameLastName] => Verdugt ) ) [a2a_Gender] => Array ( [a2a_Gender] => Man )
[1] => Array (
[pid] => Person2
[a2a_PersonName] => Array (
[a2a_PersonNameFirstName] => Array ( [a2a_PersonNameFirstName] => Huibert ) [a2a_PersonNamePatronym.....
My question is now : how can i determine if I have more Person-records so i can acces the data correct way.
And what is the best way to acces the data. Must I first check the definition and write different code.
Thanks,
Fred

How can I sort through multidimensional arrays within multidimensional arrays to eliminate repetitions?

Here is an example output from when I print out its contents:
Array
(
[0] => Array
(
[CountryA] => Array
(
[ProvinceA] => Array
(
[CityA] => Array
(
[SuburbA] =>
)
)
)
)
[1] => Array
(
[CountryA] => Array
(
[ProvinceA] => Array
(
[CityA] => Array
(
[SuburbB] =>
)
)
)
)
[2] => Array
(
[CountryA] => Array
(
[ProvinceB] => Array
(
[CityB] => Array
(
[SuburbC] =>
)
)
)
)
[3] => Array
(
[CountryB] => Array
(
[ProvinceD] => Array
(
[CityE] => Array
(
[SuburbE] =>
)
)
)
)
What I would like to do is create a function that parses it in some way (and perhaps creates a new array) so that the result will look something like:
Array
(
[0] => Array
(
[CountryA] => Array
(
[ProvinceA] => Array
(
[CityA] => Array
(
[SuburbA] =>
[SuburbB} =>
)
)
[ProvinceB] =>
(
[CityB] => Array
(
[SuburbC] =>
)
)
)
)
[1] => Array
(
[CountryB] => Array
(
[ProvinceD] => Array
(
[CityE] => Array
(
[SuburbE] =>
)
)
)
)
Thanks in advance!!
Change you structure, your array should not look like this :
Array(
[0] => Array([Country A] => data),
[1] => Array([Country B] => data)
)
But more like this :
Array(
[Country A] => data,
[Country B] => data
)
Once you've done this, it will be trivial to add a city in your array :
If the country exists in the array, add to the country, else add it to the array and stop
[Add to country :] If the province exists in the country, add to the province, else add the province to the array and stop
Same for city, suburb... you get the idea

How to store _attr key values in an array using php?

I have parsed my xml into array using xml2array.php.
It gave array like
Array(
[HotelImage] => Array
(
[0] => Array
(
)
[1] => Array
(
)
[0_attr] => Array
(
[Type] => Bar/Lounge
[URL] => http://images.gta-travel.com/HH/Images/SA/ELS/ELS-HOL1-11.jpg
)
[1_attr] => Array
(
[Type] => Lobby
[URL] => http://images.gta-travel.com/HH/Images/SA/ELS/ELS-HOL1-8.jpg
)
[2] => Array
(
)
[2_attr] => Array
(
[Type] => Exterior
[URL] => http://images.gta-travel.com/HH/Images/SA/ELS/ELS-HOL1-1.jpg
)
[3] => Array
(
)
[3_attr] => Array
(
[Type] => Recreational Facilities
[URL] => http://images.gta-travel.com/HH/Images/SA/ELS/ELS-HOL1-12.jpg
)
)
)
how can i store all _attr key values in a separate array.
// this may cause a dereference error.. if it does just run array_keys
// and stro it in a sperate var that is then passed to preg_grep
$attrKeys = preg_grep('#\d+_attr#', array_keys($xmlArray));
$attrKeys = array_flip($attrKeys);
$attributes = array_intersect_key($xmlArray, $attrKeys);
$tags = array_diff_key($xmlArray, $attributes);

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