Really simply wordpress array issue - php

I cannot figure this out, when I use get_post_meta, like so
$meta = get_post_meta( get_the_ID() );
Its returns an array but printing a key such as weight like this
echo $meta['weight'];
returns another array, how am I able to use this as I simply want to print the value rather than this if I use print_r
Array ( [0] => 12313 )
I really would like to get all the values in this was as I have over 60 custom fields and do not want to have 60 lines to get each custom field individually :(
Many thanks

Try this:
echo $meta['weight'][0];

Related

Get results from array by not using the key

I have the following code to get an array of looped posts that the user has a status of 'active'.
In that array I have multiple entries for "slug" like this.
["slug"]=>string(9) "the_bbc""
I can't pull in the data based on string number - "string(9)" as they're all different.
How can I echo "the_bbc" by searching for ["slug"] and return all results.
<?php $user_id = get_current_user_id();
$active_memberships = wc_memberships_get_user_memberships( $user_id, 'active' ); // userID and active user ?>
<pre><?php var_dump ($active_memberships, $product_id); ?></pre>
Yes this looks quite novice and have been told yesterday by another person but I'm in my first year in college studying php.
Thanks
If you have multiple "slug" in an array you can loop through it with a foreach(), ex:
foreach ( $active_memberships['slug'] as $slug) {
//do whatever with this variable
}
I hope that I understood you correctly. Please provide a better description next time.

Wordpress output to array in query from custom field

I have created a custom field named "sec1array" in my categories so that i can add an array, for example 1,2,3,4
I want to retrieve that array and output it in the loop, so I created this code.
$seconearray = array($cat_data['sec1array']);
$args = array(
'post__in' => $seconearray
);
However, it only seems to be outputting the first post in the array. Is it something to do with the way the comma is outputting?
If I print $seconearray it outputs correctly, example 1,2,3,4
What you are doing is storing a string value of "1,2,3,4" in your database which when you are trying to construct an array from it like array("1,2,3,4") you end up just assigning a single value to that new array. This is why it only contains a single value.
You need to store your value in a serializable format so it can be converted back to an array after you save it to the database. There are many ways to do this, I'm sure others will give more examples:
JSON encode it
json_encode(array(1,2,3,4)); // store this in your db
json_decode($cat_data['sec1array']); // outputs an array
Or, you can use PHP serialize
serialize(array(1,2,3,4)); // store this in your db
unserialize($cat_data['sec1array']); // outputs an array
If you want to keep your string, you can explode it:
explode(',', $cat_data['sec1array']); // outputs your array of 1,2,3,4.
Using any of these methods will work. Finally you'll end up with an example like:
$seconearray = explode(',', $cat_data['sec1array']);
$args = array(
'post__in' => $seconearray
);

Array item call

I have problem with calling Array which are form redux framework to wordpress
when i execute this:
print_r ($ka_opt['theme-order']);
i have this result:
Array ( [nr2] => 1 [nr3] => 1 [nr1] => 1 )
I need to call specific item from this array for example first item, i tryed this to call first possition but dont work:
echo $ka_opt['theme-order'][0];
whats wrong? i dont know how to call variable
That is an associative array, not a numerically keyed array. You can't use numerical keys with associative arrays. You must use their proper keys:
echo $ka_opt['theme-order']['nr2'];
If you want the first item you can us array_shift():
echo array_shift($ka_opt['theme-order']);
If you want a deeper array element you can use array_slice():
// get second element, assuming PHP5.4+
echo array_slice(array_values($ka_opt['theme-order']), 1, 1)[0];
And, of course, you can always loop through it to get the values you seek.

In PHP, how can I access a child object when I don't know it's name?

Using json_decode, I've ended up with an object that looks like this:
$data->foo->bar->1234567->id
I want to access id. There are two problems, both with the number 1234567:
It's an illegal property name.
The number will differ each time, and I can't predict what the number will be. I need a way of accessing id, even when I don't know the number.
I know I can overcome problem (1) with curly braces, but I don't know how to overcome (2). I don't want to use get_object_vars, because the object is likely to be very large, and that function is very slow.
My current solution is simply
foreach ($data->foo->bar as $id); but that feels rather hacky. Is there a better way?
From my comment above, using json_decode(,true) and then resetting.
The example json array looks like:
Array (
[foo] => Array (
[bar] => Array (
[1234567] => Array (
[id] => 1234
)
)
)
)
The code:
<?php
$data = json_decode('{"foo":{"bar":{"1234567":{"id":1234}}}}', true);
reset($data['foo']['bar']);
$number = key($data['foo']['bar']);
echo $data['foo']['bar'][$number]['id'];
Output: 1234
In case you don't need the whole array anymore and only want to get the id you can get it like this:
<?php
$data = json_decode('{"foo":{"bar":{"1234567":{"id":1234}}}}', true);
echo array_shift($data['foo']['bar'])['id'];
Only works if the unknown key is the first element of bar. array_shift removes the element from $data.

How to make an array with only 1 index to top level?

I have an array like so:
Array
(
[0] => stdClass Object
(
[id] => 12
[name] => Business
)
)
But since it only has 1 index element, how can I make it top level removing the zero index? Because I want to reference this array like so $category->name instead of $category[0]->name
I know I can re-build this with a foreach loop but I was hoping there might be a PHP builtin function that does this?
Do you mean something like this?
$category = $category[0];
echo $category->name;
Instead of the first line, you could also do
$category = reset($category);
reset() will reset the array pointer to the first element and return that element. So this will also work with an associative array (which might have elements but not one with the index 0).
You can use current:
$category = current($category);
echo $category->name;
or
echo current($category)->name;
Using current solves the problem of the index not actually being 0. Another advantage to current is that it doesn't affect the array's internal pointer in any way.
$category = $category[0];

Categories