PHP working with arrays - php

I have this line of code:
print_r(get_the_terms( $_product->id, 'product_cat'));
which returns:
Array ( [0] => stdClass Object ( [term_id] => 67 [name] => Paintings [slug] => paintings [term_group] => 0 [term_taxonomy_id] => 67 [taxonomy] => product_cat [description] => [parent] => 0 [count] => 44 ) [1] => stdClass Object ( [term_id] => 13 [name] => Small [slug] => small [term_group] => 0 [term_taxonomy_id] => 13 [taxonomy] => product_cat [description] => [parent] => 0 [count] => 15 ) )
what I am trying to get is [name] => Paintings so I can create another array like so:
$array[get_the_terms( $_product->id, 'product_cat')->name] = $_product->get_title()
$_product->get_title() is "A Quiet Day"
expected output Array ( [Paintings] => A Quiet Day )
If I do this:
$array[] = $_product->get_title();
the output is
Array ( [0] => A Quiet Day )
I am just trying to replace the 0 with Paintings

$array['Paintings'] = $_product->get_title();
If you want the array to have specific keys, specify them...
The problem may be that it looks like the class you're storing that name in is not properly defined. Are you storing it in the session?
Your get_the_terms() function also appears to return more than one object, so you will not be able to chain ->name off it. You'll need to select the right one.
You need to make your code and question more readable.

Related

Display a single element of an array

I think it is a very simple question but for a long time I am trying to figure this out:
I need to store the number 18 ([term_id]) from the following array in a variable, how can I do this?
Array (
[0] => WP_Term Object (
[term_id] => 18
[name] => Im Rebgarten
[slug] => im-rebgarten
[term_group] => 0
[term_taxonomy_id] => 18
[taxonomy] => give_forms_category
[description] =>
[parent] => 0
[count] => 1
[filter] => raw
[object_id] => 900
)
)
Kind regards
You can simply access the variable from object by -> notation like
$array = array();//Your current array
$term_variable = $array[0]->term_id; //store the term_id into the variable
print_r($term_variable); //prints the variable

fastest way to get array block based on value contained within the block (not working)

https://gist.github.com/jcranny/9465715
I have this array (example)...
Array
(
[0] => Array
(
[series] => stdClass Object
(
[term_id] => 5
[name] => Moto2 2013
[slug] => moto2-2013
[term_group] => 0
[term_taxonomy_id] => 3
)
[race_number] => 77
[team] => Technomah carXpert
[constructor] => Suter
[machine] => Honda CBR600RR
)
[1] => Array
(
[series] => stdClass Object
(
[term_id] => 6
[name] => Moto2 2014
[slug] => moto2-2014
[term_group] => 0
[term_taxonomy_id] => 3
)
[race_number] => 15
[team] => La Fonte Tascaracing
[constructor] => Suter
[machine] => Honda CBR600RR
)
[2] => Array
(
[series] => stdClass Object
(
[term_id] => 7
[name] => Moto2 2015
[slug] => moto2-2015
[term_group] => 0
[term_taxonomy_id] => 3
)
[race_number] => 15
[team] => Mapfre Aspar Team Moto2
[constructor] => Suter
[machine] => Honda CBR600RR
)
)
Now I would like to be able to get information from each block.
For example I would like to echo this data:
[race_number]
[team]
[constructor]
[machine]
But I want only to echo this data that is relevant to a specific [series]
I have the [series] term_id so this is my key to get the relevant data, but i'm struggling to get my node function work.
This is the function to do this:
function node_modify_riders_array($rider_id)
{
$fields = get_field("rider_series_data", $rider_id);
foreach($fields as $field_key => $field_val)
{
$new_fields[$field_val["series"]] = $field_val;
}
return $new_fields;
}
Then this is how I am get the series specific data based on the series term id.
$rider_series_data = node_modify_riders_array($rider_id);
$series_id = $series[0]->term_id;
$team = $rider_series_data[$series_id]["team"];
$contstructor = $rider_series_data[$series_id]["constructor"];
$machine = $rider_series_data[$series_id]["machine"];
$race_number = $rider_series_data[$series_id]["race_number"];
But some thing is wrong and I can work it out. Can anyone see where I'm going wrong or help me fix it.
Massive thanks in advance, would really appreciate some help.
What the problem is:
My function node_modify_riders_array is returning null, which is causing my $team, $contstructor, $machine, and $race_number too not output anything.
If I echo $series_id on my example, I get 6
Which should pass though my node_modify_riders_array function and display the relevant array values. But it's not outputting anything and I got no errors.
This is my full code so you can see what I am trying to do...
https://gist.github.com/jcranny/9465715
You are using an object as your array key and not the term_id of your series object.
function node_modify_riders_array($rider_id)
{
$fields = get_field("rider_series_data", $rider_id);
foreach($fields as $field_key => $field_val)
{
$new_fields[$field_val["series"]->term_id] = $field_val;
//^^^^^^^^^ <--- add this
}
return $new_fields;
}

Display the value from a returned object

I have a variable object which stores the value below.
Array
(
[0] => stdClass Object
(
[term_id] => 1
[name] => Uncategorized
[slug] => uncategorized
[term_group] => 0
[term_taxonomy_id] => 1
[taxonomy] => category
[description] =>
[parent] => 0
[count] => 4
[object_id] => 39
[cat_ID] => 1
[category_count] => 4
[category_description] =>
[cat_name] => Uncategorized
[category_nicename] => uncategorized
[category_parent] => 0
)
)
I now want to display the slug from the list of values. How do i do so?
Assuming that object is stored in an array called $array:
echo $array[0]->slug
You can use print_r($yourObject) to print out the entire object for debugging or if you want to print just that value this would work: echo($yourObject[0]->slug); .
If there are multiple array indices then this:
foreach($yourObject as $object)
{
echo $object->slug;
}
Jonh gave you an exact answer...let me explain it a bit
$obj = your_array;//from where you are using var_dump() to see these values..
and then
echo $obj->slug
you can use the same technique for other terms in your dump, like
echo $obj->name
HTH

Comparing two stdClass Objects

I'm currently working on a modified search within a Wordpress theme that queries a custom taxonomy location and displays it's terms as search results. I couldn't find a built in Wordpress function to handle this, so I used a $wpdb query
$keywords = $_GET['s'];
$results = $wpdb->get_results( "SELECT * FROM $wpdb->terms WHERE name LIKE '%%$keywords%%'");
<ul>
<?php foreach ( $results as $result ) :?>
<li><?php echo $result->name;?></li>
<?php endforeach;?>
</ul>
The issue with this is the table wp_terms not only stores custom taxonomy terms, but other default terms as well. So in order to display search results just for the the custom taxonomy, not other default terms, I thought of using get_terms to get all of the terms belonging to the custom taxonomy location and display terms from the table wp_terms based off of get_terms result with in_array
$keywords = $_GET['s'];
$results = $wpdb->get_results( "SELECT * FROM $wpdb->terms WHERE name LIKE '%%$keywords%%'");
$terms = get_terms("location");
<ul>
<?php foreach ( $results as $result ) :?>
if(in_array($result->name, $terms)) :?>
<li><?php echo $result->name;?></li>
<?php endif;?>
<?php endforeach;?>
</ul>
However, $results and $terms are both stdClass Objects, so in_array doesn't work.
Is there either a function, method or possibly a MySQL query that will allow me to display results from the object $results based off the contents of the object $terms?
Thanks in advance.
EDIT:
Contents of $terms
Array (
[0] => stdClass Object ( [term_id] => 32 [name] => US [slug] => us [term_group] => 0 [term_taxonomy_id] => 32 [taxonomy] => signs [description] => [parent] => 25 [count] => 1 )
[1] => stdClass Object ( [term_id] => 22 [name] => EU [slug] => eu [term_group] => 0 [term_taxonomy_id] => 22 [taxonomy] => signs [description] => [parent] => 0 [count] => 3 )
[2] => stdClass Object ( [term_id] => 26 [name] => AU [slug] => au [term_group] => 0 [term_taxonomy_id] => 26 [taxonomy] => signs [description] => [parent] => 22 [count] => 1 )
[3] => stdClass Object ( [term_id] => 27 [name] => IE [slug] => ie [term_group] => 0 [term_taxonomy_id] => 27 [taxonomy] => signs [description] => [parent] => 22 [count] => 2 )
[4] => stdClass Object ( [term_id] => 23 [name] => PK [slug] => pk [term_group] => 0 [term_taxonomy_id] => 23 [taxonomy] => signs [description] => [parent] => 0 [count] => 2 )
)
You can convert them into arrays with
(array) $variable;
Or, if your stdClass contains nested stdClasses, you can do the following:
function obj_to_array_recursive(stdClass $obj) {
foreach ($obj as &$element) {
if ($element instanceof stdClass) {
obj_to_array_recursive($element);
$element = (array)$element;
}
}
$obj = (array)$obj;
return $obj;
}

reducing/filtering an array

I have an array which I want to filter out certain keys. Let's say $subcats equals this array:
Array
(
[0] => stdClass Object
(
[term_id] => 4
[term_group] => 0
[term_taxonomy_id] => 4
[taxonomy] => category
)
[1] => stdClass Object
(
[term_id] => 5
[term_group] => 0
[term_taxonomy_id] => 5
[taxonomy] => category
)
)
All I want is the term_ids in it's own array.
I've tried foreach and array_values, but I can't seem to wrap my head around it at the moment. Should I be using array_filter?
So the result should be $term_ids = array( 4, 5 );
$termIds = array_map(function($i) { return $i->term_id; }, $subcats);
This syntax requires PHP 5.3+.

Categories