The output for print_r($array) is as follow;
Array (
[0] => xmlrpcval Object (
[me] => Array ( [int] => 50 )
[mytype] => 1
[_php_class] =>
)
[1] => xmlrpcval Object (
[me] => Array ( [string] => Angel Cook (Chamber Works) )
[mytype] => 1 [_php_class] => )
)
I only want to print 'Angel Cook (Chamber Works)', Any help appreciated?
Hardcoded this would be something like the following:
print $array[1]->me['string']
Judged from the following:
You have an array... which contains entries... which are objects... which contain properties... which are associative arrays... which contain your values
Edit: not certain whether [string] is actually a key. If not... use me[0] instead.
You have an array of objects which you want the second object (array's are zero based), where the me key point to an array where you want the first item so:
$array[1]->me[0]
Related
I have an array like this:
(
[data] => Array
(
[account_id] => 1
[description] => my asset
[value] => Estimate
[value_amount] => 85000
[type] => Vehicle
[owner] => Array
(
[app_id] => 123
[percent] => 100
)
)
)
Clearly I can loop through the array and pull out the nested owner array that way, but is there something similar to array_column that will get the entire owner nested array without having to loop ?
Use the indexes, no function necessary.
$owner = $array['data']['owner']
or..
$percent = $array['data']['owner']['percent']
In php it's call associative array which mean index will not numeric it can be anythink.
So you can retrieve data like
<?php echo $array['data']['owner']['app_id']; ?>
Using PHP I am running a query on my database and as a result I get a data-set with optional values. An example:
Result:
Array
(
[0] => Array
(
[attribute_1] => Red,
[attribute_2] => Car,
[name] => Sportscar
)
[1] => Array
(
[attribute_1] => Red,
[attribute_2] => ,
[name] => Rose
)
)
I want to format the resulting Data as such:
Array
(
[Red] => Array
(
[Car] => Array
(
[0] => Sportscar
)
)
[0] => Rose
)
So that I could access the Sportscar by
$array['Red']['Car'][0];
And the Rose by
$array['Red'][0]
This will make it easier to output the data with a nice little foreach loop.
of course the actual case is a bit more complex with a lot more values etc but this example demonstrates the principle.
The problem is that I can't think of a proper way to format the data efficiently. Not necessarily recursive but at least not one million if and elses would be nice.
Any ideas?
Below is a array generated by a query builder.
$random_array = Array ( [0] => Array ( [text] => A great time was had by all! )
[1] => Array ( [text] => KILL SHOT )
[2] => Array ( [text] => How is it possible)
[3] => Array ( [text] => http://www.youtube.com/watch?v=KwGOZpbxU9g )
[4] => Array ( [text] => http://www.youtube.com/watch?v=KwGOZpbxU9g )
)
Currently i am doing like this to print the random value
print_r(array_rand($random_array,1));
This is printing the array key as 3 or 1 etc(random from above array).I want to print the value of the key and not the key.
e.g I want to print the random value like this "http://www.youtube.com/watch?v=KwGOZpbxU9g" or "A great time was had by all!" instead of 3 or 1 which is printing now.
Is it possible to do this.
You will have one more line of code as shown below:
$array_key = array_rand($random_array,1); //get the key
print_r( $random_array[$array_key] ); //use the key to print value
What about simply calling
$randNumber = rand(0,count($random_array))-1; //index in array starts with 0
print (string) $random_array[$randNumber];
I have an array of values returned from Facebook - let's call it $array.
If I do print_r($array) - it looks like this:
Array
(
[code] => 200
[headers] => Array
(
[0] => Array
(
[name] => Some value
[value] => *
)
[1] => Array
(
[name] => Some value
[value] => Some value
)
[2] => Array
(
[name] => Some value
[value] => Some value
)
)
[body] => {"about":"Some more values.","can_post":true}
)
I need to extract the body part from this array.
I cannot refer to it by it's position, I'm looking for something like $array->body and receive the {....} string.
$array->body would work if the variable $array was an object
For arrays, just use:
$body = $array['body'];
(see: http://be2.php.net/manual/en/language.types.array.php)
If you want to access to your array via -> just do 1 more step:
$array = (object) $array;
And now, you can access to your body via:
$array->body;
Else without this step there is just one way:
$array['body'];
If you are more interested about converting arrays into objects, you can visit this question: How to convert an array to object in PHP?
Access array elements by using their name.
$array['body'];
I have three arrays each having SimpleXML Objects in them. They are structured like so:
Array
(
[0] => SimpleXMLElement Object
(
[post_id] => 1476
[name] => Johnson Fisheries Ltd.
[owner] => Mr. John Johnson
)
)
I want to be able to compare all 3 arrays and filter out the differences so that the results have only the elements that are the same in all 3 arrays.
For example:
Array1
(
[0] => 1476
[1] => 1560
[2] => 1342
)
Array2
(
[0] => 2454
[1] => 1476
)
Array3
(
[0] => 3412
[1] => 7512
[2] => 2454
[4] => 1476
)
The resulting array would only contain [0] => 1476
What's the best way to do this? I've looked for a function that will compare arrays in this way but I have had no luck. Any ideas?
Any help is very appreciated!
Best option will be using php's built-in array-intersect function.
$answer = array_intersect($Array1,$Array2,$Array3);
Loop over your first array, checking it against the 2nd and 3rd using in_array, like this:
foreach($array1 as $post_id) {
if(in_array($post_id, $array2) && in_array($post_id, $array3)) {
// We have a winner!
}
}
create a new array.
add all elements of the first array into the new array.
iterate through all the other elements, find out which elements in your new array are in the array to be compared. remove all the elements that don't appear.
at the end of your iterations, you have your desired array.