I am attempting to grab the uploaded audio file from WordPress but having some issues. I need to grab the patch excluding the domain name so something like wp-content/uploads/2014/09/file.mp3.
I tried to use get_attached_media() and I can see exactly what I need when I var_dump() it is stored in an array with the key of guid. I have tried several different ways but I cannot access it. This is the result.
object(WP_Post)#2059 (24) {
["ID"]=>
int(4312)
["post_author"]=>
string(1) "3"
["post_date"]=>
string(19) "2014-10-06 15:33:16"
["post_parent"]=>
int(4298)
["guid"]=>
string(73) "/wp-content/uploads/2014/09/file.mp3"
}
}
I removed most of the code to keep it brief. How can I access the guid key? I store the results in $a and tried $a->guid $a['guid']and $a->post->guid and others but no luck.
Any help would be greatly appreciated.
If you look at the documentation of the get_attached_media function you will see that it returns an array of WP_Post objects.
The array is indexed by attachment id so you can't access the first element simply by doing $a[0]. I would recommend that you reindex the array before using it, like this:
$a = get_attached_media(...);
$a = array_values($a);
echo 'GUID: ' . $a[0]->guid;
You could also iterate over all the attachments like this
foreach ($a as $attachment) {
echo "GUID: {$attachment->guid}\n";
}
Related
I query a database to obtain an array of results.
$usersArray = $db->getAllUsers(); // db-Query
If I print out the array's var_dump, its content is structured in form of other arrays:
array(9) { [0]=> **array**(1) { ["column"]=> string(20) "..." } [1]=> **array**(1) { ["column"] (remaining 8 are the same).
Now I need these values (which are correct, so far) to be casted as strings, so that:
array(9) { [0]=> **string**(1) { ["column"]=> string(20) "..." } [1]=> **string**(1) { ["column"] ....
There are several answers to this here and elsewhere, such as
-array_map: here I can actually cast the content as string, but- it prints "Array" instead the value. It tried then getting the content via
$users = array_map('strval',implode( $usersArray));
$users = array_map('strval', print_r($usersArray));
Neither of those worked.
Is there a method through which I could cast the content as string and get the content ? Or should I rewrite the query to format the result as strings ?
You have a wrong understanding of types or at least this:
array(9) { [0]=> **string**(1) { ["column"]=> string(20) "..." } [1]=> **string**(1) { ["column"] ....
doesn't make any sense. You believe you want elements to be of type string but yet contain array data which really doesn't work.
What you actually want is a different array structure but you are heading in the wrong direction for that.
You basically have two options:
Modify the getAllUsers() method in a way that returns your data in a structure you actually need.
Modify the data after you have received it. Obviously there's no builtin function convert_data_to_how_i_want_them() - so a basic understanding of arrays is required.
Basically you create a new array and copy those values you need to the position you need them at.
Something like this should do the trick in this case:
$out = array();
foreach($in as => $value) {
$out[] = $value['column'];
}
I get a returned result from an API in stdClass format. I don't know anything about this type of data. So here it looks like:
object(stdClass)#41 (1) {
["return"]=>
object(stdClass)#42 (7) {
["afterPayOrderReference"]=> string(32) "d4ab78df6ab2ef84194dd1c1d66240b8"
["checksum"]=> string(32) "4f8826a99e9c0a67e578d04b6a625117"
["resultId"]=> int(0)
["statusCode"]=> string(1) "A"
["timestampIn"]=> float(1408533108515)
["timestampOut"]=> float(1408533113616)
["transactionId"]=> int(129525)
}
}
What I need is retrieving the statusCode value. I tried doing like in a post I read:
$array = (array) $stringResult;
$array[0]->statusCode;
But it didn't work. Please, someone explain to me in the simplest way because it's really new to me. Thanks.
Object properties are accessed with the -> operator. Just do:
echo $stringResult->return->statusCode;
If you wanted an array you would access like this since the array contains an object:
$array = (array)$stringResult;
echo $array['return']->statusCode;
Its an object array, so just as you call the array element,
echo $stringResult["return"]->statusCode
How can i read this data i'm getting on my page via post?
This are the variables as they appear on firebug under POST.
list[0].firstName =test 1
list[0].name =test
list[1].name =test
I know that php replaces dots with underscores so i can access something like "address.box" by doing:
$_POST[address_box];
but i really can't figure out how to access all the above data.
Seems like that php doesn't want to read whatever comes after the square brackets, overwriting all the fields with the same index (list[0].firstName seems to get overwritten by list[0].name .
Any solution?
EIDT:
Here a var_dump of the data:
address.number 1
list[0].firstName firstname0
list[0].name name0
list[1].name name1
array(2) { ["list"]=> array(2) { [0]=> string(10) "firstname0" [1]=> string(5) "name1" } ["address_number"]=> string(1) "1" }
No. This is not possible within the PHP-language.
You must use either $object->field or $array['key'] notation.
I'm creating an array from a database and its being created correctly as seen below.
array(2) {
["members"]=> bool(true)
["maps"]=> bool(true)
}
The issue is when I output the array using key:value the key is outputting the element number for example 0 for 'members and 1 for 'maps'.
foreach($vPrivileges as $vKey => $vValue) {
echo "$vKey: $vValue";
}
I need to output the actual name of the element and the value, I've spent awhile looking online and trying out some code samples but haven't been able to find a solution, any help is greatly appreciated.
Also I noticed that if I take the array and use the sort() function on it, it sorts as expected with one exception that it renames all the array elements to 0, 1 etc.
array(2) {
[0]=> bool(true)
[1]=> bool(true)
}
Use asort instead of sort to preserve array keys. This will fix your problem (assuming you call sort before the foreach-loop).
update
how can I retrieve this value? I need to do that if I will write the value to my database.
array(3) {
[1]=> NULL
[2]=> array(2) {
[123]=>
int(123)
[122]=>
int(0)
}
[3]=> NULL
}
There is something missing in your output. I assume it looks something like:
// var_dump($array);
array(1) {
[0]=>
string(2) "39"
}
so you can access the value with $array[0]. Simple array access.
As arrays are the most important data structure in PHP, you should learn how to deal with them.
Read PHP: Arrays.
Update:
Regarding your update, which value do you want? You have a multidimensional array. This is what you will get:
$array[1] // gives null
$array[2] // gives an array
$array[2][123] // gives the integer 123
$array[2][122] // gives the integer 0
$array[3] // gives null
Maybe you also want (have) to loop over the inner array to get all values:
foreach($array[2] as $key => $value) {
// do something with $key and $value
}
As I said, read the documentation, it contains everything you need to know. Accessing arrays in PHP is not much different than in other programming languages.
The PHP manual contains a lot of examples, it is a pretty could documentation. Use it!
If your array is referenced as $myArray, you can get the string 39 via $myArray[0], i.e., this zeroth item.