Choose what values to output from an array - php

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']));

Related

Combine/Merge arrays that have the same keys PHP

I have an array that has some keys that are repeating multiple times. Would like to combine them so that I can have them in one array, within the same array.
I have an array of this type;
Array
(
[Shoes] => Array
(
[POLO] => Array
(
[0] => Size5
)
)
)
Array
(
[Shoes] => Array
(
[FUBU] => Array
(
[0] => size6
)
)
)
Array
(
[Bag] => Array
(
[HPBAG] => Array
(
[0] => Black
)
)
)
Array
(
[Bag] => Array
(
[HPBAG] => Array
(
[0] => White
)
)
)
I would like an output of the following kind;
Array
(
[Shoes] => Array
(
[POLO] => Array
(
[0] => size5
)
[FUBU] => Array
(
[0] => size6
)
)
[Bag] => Array
(
[HPBAG] => Array
(
[0] => Black
[1] => White
)
)
)
I have tried array_merge, array_merge_recursive but all are not working.
foreach ($county as $value) { print_r(array_merge($value)); } //$value contains the array
foreach ($county as $value) { print_r(array_merge_recursive($value)); } //$value contains the array
Kindly assist if you have an idea on how to solve this in PHP.
You could do it with a few nested array_walk() calls:
$arr=array(array("Shoes" => array("POLO" => array("Size5"))),
array("Shoes" => array("FUBU" => array("size6"))),
array("Bag" => array("HPBAG" => array("Black"))),
array("Bag" => array("HPBAG" => array("White"))));
$res=[];
array_walk($arr,function($a) use (&$res){
array_walk($a, function($ar,$type) use (&$res){
array_walk($ar, function ($arr,$brand) use (&$res,$type){
$res[$type][$brand]=array_merge($res[$type][$brand]??[],$arr);
});
});
});
print_r($res);
See the demo here: https://rextester.com/RFLQ18142
It produces this result:
Array
(
[Shoes] => Array
(
[POLO] => Array
(
[0] => Size5
)
[FUBU] => Array
(
[0] => size6
)
)
[Bag] => Array
(
[HPBAG] => Array
(
[0] => Black
[1] => White
)
)
)

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

Need to echo only first element of an associative array in foreach php

I have a long associative array but I am showing a small part of it here:
Array
(
[0] => Array
(
[0] => Array
(
[RoomType] => Array
(
[#roomTypeId] => 1927848
[RoomImages] => Array
(
[RoomImage] => Array
(
[0] => Array
(
[url] => http://media.expedia.com/hotels/2000000/1620000/1611500/1611477/1611477_106_s.jpg
)
)
)
)
)
)
[1] => Array
(
[RoomType] => Array
(
[#roomTypeId] => 1927848
[RoomImages] => Array
(
[RoomImage] => Array
(
[0] => Array
(
[url] => http://media.expedia.com/hotels/2000000/1620000/1611500/1611477/1611477_106_s.jpg
)
)
)
)
)
[1] => Array
(
[0] => Array
(
[RoomType] => Array
(
[#roomTypeId] => 1927848
[RoomImages] => Array
(
[RoomImage] => Array
(
[0] => Array
(
[url] => http://media.expedia.com/hotels/2000000/1620000/1611500/1611477/1611477_106_s.jpg
)
)
)
)
)
)
[1] => Array
(
[RoomType] => Array
(
[#roomTypeId] => 1927848
[RoomImages] => Array
(
[RoomImage] => Array
(
[0] => Array
(
[url] => http://media.expedia.com/hotels/2000000/1620000/1611500/1611477/1611477_106_s.jpg
)
)
)
)
)
)
What i need here is that to print only [url] once for every element .
Like we have two element in [0] array and two elements in [1] array but i need to print the url for them only once for each array .
I am not sure if it is written in good form. please correct it if there are mistakes .
attached an image for example .
Thanks for help. But i don't need the array of url . But i want to print them by skipping the others .You can see below :
foreach($group as $k => $v){
foreach($v as $key => $hotelRoom){
<tbody class="<?php echo $iscollapse; ?> searchpage<?php echo $page; ?>">
<tr>
<td>
<div>
<?php if(array_key_exists('RoomImages',$hotelRoom)) { ?>
<img src="<?php echo setHotelImage($hotelRoom['RoomImages']['RoomImage']['0']['url'],'_b','_s'); ?>" class="img-responsive">
<?php } ?>
</div>
</td>
<td>
</tr>
</tbody>
}
}
I am printing the array like this already where $group is the array a writtenabove. In this example, foreach you can see that it is printing that image for every element. so i want to skip that for other elements and print only for first.
Try like this..
echo $array[0][0]['RoomImages']['RoomImage'][0]['url'];//image url from first array indexed at 0
echo $array[1][0]['RoomImages']['RoomImage'][0]['url'];//image from second array indexed at 1
OR make array of url's as below:
foreach($array as $key=>$value){
$urls[]=$arr[$key][0]['RoomImages']['RoomImage'][0]['url']
}
print_r($urls);//outputs url from each array only once.. in your case two urls

How to access a specific key from an associative array in PHP?

I'm a newbie to this associative array concept in PHP. Now I'm having an array named $sample as follows:
Array
(
[name] => definitions
[text] =>
[attributes] => Array
(
[name] => Mediation_Soap_Server_Reporting
[targetnamespace] => https://mediation.moceanmobile.net/soap/reporting
)
[children] => Array
(
[types] => Array
(
[0] => Array
(
[name] => types
[text] =>
[attributes] => Array
(
)
[children] => Array
(
[xsd:schema] => Array
(
[0] => Array
(
[name] => schema
[text] =>
[attributes] => Array
(
[targetnamespace] => https://mediation.moceanmobile.net/soap/reporting
)
[children] => Array
(
[xsd:complextype] => Array
(
[0] => Array
(
[name] => complextype
[text] =>
[attributes] => Array
(
[name] => Mediation_Soap_FaultMessage
)
[children] => Array
(
[xsd:sequence] => Array
(
[0] => Array
(
[name] => sequence
[text] =>
[attributes] => Array
(
)
)
)
)
)
)
)
)
)
)
)
)
)
)
From the above array I want to refer(or access) to the key xsd:schema. But I'm not able to do it. Can you please tell me how should I access or refer this key from the associative array names $sample? Thanks in advance.
To access this value you would use:-
$sample['children']['types'][0]['children']['xsd:schema'];
If you have multiple of these elements in your types array you will need to loop through them:-
foreach($sample['children']['types'] as $type) {
if(isset($type['children']) && isset($type['children']['xsd:schema'])) {
// Perform action on element
$type['children']['xsd:schema'];
}
}
If you do not know your structure (as in xsd:schema can occur outside of types) then you will need to write a recursive function or loop for finding it.
I guess your goal is to seek for the key/value pair where the key is "xsd" ?
If so, in PHP, you can do so by using the follwing logic:
while (list($key, $value) = each($arr)) {
echo "Key: $key; Value: $value<br />\n";
}
// OR
foreach ($arr as $key => $value) {
echo "Key: $key; Value: $value<br />\n";
}
Just add a set of recursing or nested loops to traverse the structure until you find the proper key.

PHP, how to echo specific object data from array?

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

Categories