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']; ?>
Related
I have an array of image data like this:
[other-image] => Array
(
[img] => Array
(
[0] => 1526973657.jpg
[1] => 1526973661.jpg
[2] => 1526973665.jpg
)
[path] => Array
(
[0] => ../post-upload/1/
[1] => ../post-upload/1/
[2] => ../post-upload/1/
)
[type] => Array
(
[0] => 1
[1] => 1
[2] => 1
)
[thumb] => Array
(
[0] => thumb_1526973661.jpg
[1] => thumb_1526973665.jpg
[2] => thumb_1526973668.jpg
)
)
Now I want to delete an image and it's all related data from sub arrays. (path, type, thumb data)
This is how I tried it in php:
$delkey = '1526973657.jpg';
if(in_array($delkey, $_SESSION['other-image']['img'])){
$imgkey = array_search($delkey, $_SESSION['other-image']['img']);
if($imgkey) unset($_SESSION['other-image']['img'][$imgkey]);
}
But problem is I can't delete related data from other arrays.
Can anybody tell me how to do this?
Thank you.
You should use !==false after array_search() because it may return first index i.e. 0 in some cases, so your condition will not executed. And regarding delete related data from other arrays, you have to unset other data related to that key.
if($imgkey!==false){
unset($_SESSION['other-image']['img'][$imgkey]);
unset($_SESSION['other-image']['path'][$imgkey]);
unset($_SESSION['other-image']['type'][$imgkey]);
unset($_SESSION['other-image']['thumb'][$imgkey]);
}
Is the related data has same key with img?
If they are same, I think you only need to add some codes to delete other data like the way was used to delete img.
if($imgkey) unset($_SESSION['other-image']['path'][$imgkey]);
if($imgkey) unset($_SESSION['other-image']['type'][$imgkey]);
if($imgkey) unset($_SESSION['other-image']['thumb'][$imgkey]);
If the keys in img sub-array are related with the same key(index) in sub-arrays(path, type and thumb, you can also unset those keys. e.g.
$delkey = '1526973657.jpg';
if(in_array($delkey, $_SESSION['other-image']['img'])){
$imgkey = array_search($delkey, $_SESSION['other-image']['img']);
if($imgkey){
unset($_SESSION['other-image']['img'][$imgkey]);
unset($_SESSION['other-image']['path'][$imgkey]);
unset($_SESSION['other-image']['type'][$imgkey]);
unset($_SESSION['other-image']['thumb'][$imgkey]);
}
}
So I want to delete an array element from a JSON array based on an id in a sub-array. I know it sounds weird. Here's an example of the array. I want to delete the entire array [0] based on the [dealer][id] array where the [id] = 20220 in this example.
Array
(
[results] => Array
(
[offset] => 1
[length] => 15
[data] => Array
(
[0] => Array
(
[dealer] => Array
(
[id] => 20220
[name] => apple
)
)
)
)
}
In reality there are a lot more elements in the [results] array. I'm not sure how to go about it.
Any help is greatly appreciated!
Loop thru data key first then check if dealer id matches the searched id
$id = 20220;
foreach ($array['results']['data'] as $key => $value) {
if ($value['dealer']['id'] == $id) {
unset($array['results']['data'][$key]);
}
}
use array_filter,
$array['results']['data'] = array_filter($array['results']['data'], function($v){return $v['dealer']['id'] != 20220;});
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'];
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]
I've seen similar questions on here but I can't seem to apply the solutions to my problem. I have a variable called $results which I got from an API. I'll change the proper nouns so as to protect my work's customers:
stdClass Object
(
[out] => stdClass Object
(
[count] => 2
[transactions] => stdClass Object
(
[RealTimeCommissionDataV2] => Array
(
[0] => stdClass Object
(
[adId] => 12345678
[advertiserId] => 123456789
[advertiserName] => Chuck E. Cheese, inc.
[commissionAmount] => 50
[country] => US
[details] => stdClass Object
(
)
[eventDate] => 2009-11-16T09:44:25-08:00
[orderId] => X-XXXXXXXXXX
[saleAmount] => 0
[sid] => 123456789
[websiteId] => 2211944
)
[1] => stdClass Object
(
[adId] => 987654321
[advertiserId] => 12345
[advertiserName] => Chorizon Wireless.
[commissionAmount] => 50
[country] => US
[details] => stdClass Object
(
)
[eventDate] => 2009-11-16T09:58:40-08:00
[orderId] => X-CXXXXXX
[saleAmount] => 0
[sid] => 61-122112
[websiteId] => 1111922
)
)
)
)
)
I shortened it to two entries here but the number of entries will vary, it's the result of a check for transactions in the past hour, there may sometimes be only one and sometimes as many as a dozen.
I want to assign these entries to variables like websiteId1 websiteId2 etc. I know I need to do a foreach loop but can't seem to figure it out. How can I write it so that I get the "[details]" as well?
foreach ($results->out->transactions->RealTimeCommissionDataV2 AS $commissionData) {
// you can access the commissionData objects now, i.e.:
$commissionData->adId;
$commissionData->details;
}
<?
foreach ($result->out->transactions->RealTimeCommissionDataV2 as $item)
{
// do somthing with each item.
print_r($item);
// or the details array
$num_details = sizeof($item->details)
}
I think this is what you want.
EDIT
Updated based on some notes in the documentation. Specifically, these two
a numerically indexed array will not
produce results unless you use
EXTR_PREFIX_ALL or
EXTR_PREFIX_INVALID.
Prefixes are automatically separated
from the array key by an underscore
character.
echo extract( $results->out->transactions->RealTimeCommissionDataV2, EXTR_PREFIX_ALL, 'websiteId' );
// test the extract
print_r( $websiteId_0 );