i was trying to access this php array with no luck, i want to access the [icon] => icon.png
Array ( [total] => 2
[total_grouped] => 2
[notifys] => Array ( [0] => Array ( [notifytype_id] => 12
[grouped] => 930
[icon] => icon.png
[n_url] => wall_action.php?id=930
[desc] => 690706096
[text] => Array ( [0] => Sarah O'conner ) [total] => 1 )))
$arr['notifys'][0]['icon']
ETA: I'm not sure what your comment means, the following code:
$arr = array('total'=>2, 'total_grouped' => 2, 'notifys' => array(array(
'notifytype_id' => 12, 'icon' => 'icon.png')));
echo '<pre>';
print_r($arr);
echo '</pre>';
var_dump($arr['notifys'][0]['icon']);
outputs:
Array
(
[total] => 2
[total_grouped] => 2
[notifys] => Array
(
[0] => Array
(
[notifytype_id] => 12
[icon] => icon.png
)
)
)
string(8) "icon.png"
Generally, code never outputs nothing. You should be developing with all errors and notifications on.
$arr['notifys'][0]['icon']
rg = Array ( [total] => 2 [total_grouped] => 2 [notifys] => Array ( [0] => Array ( [notifytype_id] => 12 [grouped] => 930 [icon] => icon.png [n_url] => wall_action.php?id=930 [desc] => 690706096 [text] => Array ( [0] => Sarah O'conner ) [total] => 1 )));
icon = rg["notifsys"][0]["icon"];
Everybody is posting right answer. Its just you have giving a wrong deceleration of array.
Try var_dump/print_r of array and then you can easily understand nodes.
$arr = array(total => 2,
total_grouped => 2,
notifys => array( 0 => array(notifytype_id => 12,
grouped => 930,
icon => 'icon.png',
n_url => 'wall_action.php?id=930',
desc => 690706096,
text =>array(0 => 'Sarah Oconner' ),
total => 1,
),
),
);
echo $arr['notifys']['0']['icon'];
Related
I have a question about this:
I have two array, one is static, and one can be updated by the user...
I would like to check for every id from the static array if exist the id to the other array, and if exsist, do something, if doesn't exist (when finish to check) pass to other ID etc...
now, the arrays are these:
user array (the user unlock 2 achievement):
Array (
[0] => Array (
[data] => Array (
[importance] => 0
[achievement] => Array (
[id] => 644081262362202
[title] => Achievement 2
[type] => game.achievement
[url] => http://www.***.com/achievements/achievement2.html
)
)
[id] => 104693166566570
)
[1] => Array (
[data] => Array (
[importance] => 0
[achievement] => Array (
[id] => 968802826528055
[title] => Achievement 1
[type] => game.achievement
[url] => http://www.***.com/achievements/achievement1.html
)
)
[id] => 104023386633548
)
)
the static Array (have 6 achievement saved):
Array (
[0] => Array (
[data] => Array (
[points] => 50
)
[description] => you unlock the achievement2
[title] => Achievement 2
[id] => 644081262362202
)
[1] => Array (
[data] => Array (
[points] => 50
)
[description] => you unlock the achievement3
[title] => Achievement 3
[id] => 912599152147444
)
[2] => Array (
[data] => Array (
[points] => 50
)
[description] => you unlock the achievement5
[title] => Achievement 5
[id] => 913757345379232
)
[3] => Array (
[data] => Array (
[points] => 50
)
[description] => you unlock the achievement6
[title] => Achievement 6
[id] => 921989084564878
)
[4] => Array (
[data] => Array (
[points] => 50
)
[description] => you unlock the achievement1
[title] => Achievement 1
[id] => 968802826528055
)
[5] => Array (
[data] => Array (
[points] => 50
)
[description] => you unlock the achievement4
[title] => Achievement 4
[id] => 1149671038394021
)
)
now, I use this script to echo the final output like the picture (results is the static array):
if (empty($results)) {
//echo 'noAchievement for the app';
} else {
foreach ($results as $result) {
$totalAchievementsApp .= ' [["' . "0" .'"],["'.$result[id] .'"],["'. $result[title] .'"],["'. $result[data][points]."]] ";
}
}
now, How I can do to check inside the this script? I know I have to add another if inside the else to check if the ID is = to other ID, but I don't know how, I'm a little bit confused... I would like to check if the id of the static array exist in the other array, and if exsist, do this:
**$totalAchievementsApp .= ' [["' . "1" .'"],["'.$result[id] .'"],["'. $result[title] .'"],["'. $result[data][points]."]] ";**
Thank you very much :)
If I understand correctly, you want to indicate for each entry in the static array whether its ID exists in the user array.
You can use array_column to generate an array of all IDs in the user array. Then use in_array to check if each static ID exists in that array. Set a value to 1 if its found and 0 if its not found.
For the sake of example, I've generated a new final output array. But you could just add the "found" value to each entry of the the static array.
<?php
$static=array(
array('point'=>50,'title'=>'TITLE 1','id'=>54632),
array('point'=>50,'title'=>'TITLE 2','id'=>54344),
array('point'=>50,'title'=>'TITLE 3','id'=>34225),
array('point'=>50,'title'=>'TITLE 4','id'=>2323245),
array('point'=>50,'title'=>'TITLE 5','id'=>23872445),
);
$user=array(
array('id'=>2323245,'title'=>'TITLE 1','point'=>50),
array('id'=>54344,'title'=>'TITLE 2','point'=>50),
array('id'=>34225,'title'=>'TITLE 3','point'=>50)
);
$final=array();
foreach ($static as $entry) {
$final[]=array(
'found'=>in_array($entry['id'],array_column($user,'id'))?1:0,
'id'=>$entry['id'],
'title'=>$entry['title'],
'point'=>$entry['point']
);
}
echo"<pre>".print_r($final,true)."</pre>";
With your data, the output is:
Array
(
[0] => Array
(
[found] => 0
[id] => 54632
[title] => TITLE 1
[point] => 50
)
[1] => Array
(
[found] => 1
[id] => 54344
[title] => TITLE 2
[point] => 50
)
[2] => Array
(
[found] => 1
[id] => 34225
[title] => TITLE 3
[point] => 50
)
[3] => Array
(
[found] => 1
[id] => 2323245
[title] => TITLE 4
[point] => 50
)
[4] => Array
(
[found] => 0
[id] => 23872445
[title] => TITLE 5
[point] => 50
)
)
EDIT
Given the more complex structure of your actual arrays, I nested several array_column functions to access the deeper "data > achievement > id" keys in your user array:
$user_achvmts=array_column(array_column(array_column($user,'data'),'achievement'),'id');
See the example below:
// initialize the "static" and "user" arrays
$static=array (
0 => array(
'data' => array(
'points' => 50
),
'description' => 'you unlock the achievement2',
'title' => 'Achievement 2',
'id' => 644081262362202
),
1 => array(
'data' => array(
'points' => 50
),
'description' => 'you unlock the achievement3',
'title' => 'Achievement 3',
'id' => 912599152147444
),
2 => array(
'data' => array(
'points' => 50
),
'description' => 'you unlock the achievement5',
'title' => 'Achievement 5',
'id' => 913757345379232
),
3 => array(
'data' => array(
'points' => 50
),
'description' => 'you unlock the achievement6',
'title' => 'Achievement 6',
'id' => 921989084564878
),
4 => array(
'data' => array(
'points' => 50
),
'description' => 'you unlock the achievement1',
'title' => 'Achievement 1',
'id' => 968802826528055
),
5 => array(
'data' => array(
'points' => 50
),
'description' => 'you unlock the achievement4',
'title' => 'Achievement 4',
'id' => 1149671038394021
)
);
$user=array(
0=>array(
'data' => array(
'importance' => 0,
'achievement' => array (
'id' => 644081262362202,
'title' => 'Achievement 2',
'type' => 'game.achievement',
'url' => 'http://www.***.com/achievements/achievement2.html'
)
),
'id' => 104693166566570
),
1 => array (
'data' => array (
'importance' => 0,
'achievement' => array (
'id' => 968802826528055,
'title' => 'Achievement 1',
'type' => 'game.achievement',
'url' => 'http://www.***.com/achievements/achievement1.html'
)
),
'id' => 104023386633548
)
);
// build array of user achievement IDs
$user_achvmts=array_column(array_column(array_column($user,'data'),'achievement'),'id');
// generate final array, with "found" values
$final=array();
foreach ($static as $entry) {
$final[]=array(
'found'=>in_array($entry['id'],$user_achvmts)?1:0,
'id'=>$entry['id'],
'title'=>$entry['title'],
'description'=>$entry['description'],
'points'=>$entry['data']['points']
);
}
echo"<pre>".print_r($final,true)."</pre>";
The result is:
Array
(
[0] => Array
(
[found] => 1
[id] => 644081262362202
[title] => Achievement 2
[description] => you unlock the achievement2
[points] => 50
)
[1] => Array
(
[found] => 0
[id] => 912599152147444
[title] => Achievement 3
[description] => you unlock the achievement3
[points] => 50
)
[2] => Array
(
[found] => 0
[id] => 913757345379232
[title] => Achievement 5
[description] => you unlock the achievement5
[points] => 50
)
[3] => Array
(
[found] => 0
[id] => 921989084564878
[title] => Achievement 6
[description] => you unlock the achievement6
[points] => 50
)
[4] => Array
(
[found] => 1
[id] => 968802826528055
[title] => Achievement 1
[description] => you unlock the achievement1
[points] => 50
)
[5] => Array
(
[found] => 0
[id] => 1149671038394021
[title] => Achievement 4
[description] => you unlock the achievement4
[points] => 50
)
)
Note that array_column is only available in PHP >= 5.5.0. For older versions, see the Recommended userland implementation for PHP lower than 5.5.
As an alternative to array_column, you could use array_map to build an array of the user IDs:
$user_achvmts = array_map( function($v) {return $v['data']['achievement']['id'];}, $user);
Or even just iterate through the user array:
$user_achvmts=[];
foreach ($user as $v) { $user_achvmts[]=$v['data']['achievement']['id']; }
I am fairly new to PHP and I am writing a PHP function that grabs an object from SOAP.
I found a code to convert it to an array but I can't manage to echo any data.
The array from print_r
Array
(
[Status] => Array
(
[Code] => 0
[Message] => OK
)
[Order] => Array
(
[OrderNumber] => 9334543
[ExternalOrderNumber] =>
[OrderTime] => 2014-07-15T15:20:31+02:00
[PaymentMethod] => invoice
[PaymentStatus] => Paid
[ShipmentMethod] => Mypack
[DeliveryStatus] => Delivered
[Language] => sv
[Customer] => Array
(
[CustomerId] => 13556
[CustomerNumber] =>
[Username] => admin
[Approved] => 1
[OrgNumber] => 9309138445
[Company] =>
[VatNumber] =>
[FirstName] => Jane
[LastName] => Doe
[Address] => Gatan
[Address2] =>
[Zip] => 1230
[City] => Staden
[Country] => Sweden
[CountryCode] => SE
[PhoneDay] => 84848474
[PhoneNight] =>
[PhoneMobile] =>
[Email] => mail#msn.com
[NewsLetter] =>
[OrgType] => person
[OtherDelivAddress] =>
[DelivName] =>
[DelivAddress] =>
[DelivAddress2] =>
[DelivZip] =>
[DelivCity] =>
[DelivCountry] =>
[DelivCountryCode] =>
)
[Comment] =>
[Notes] => 9063025471 UK/MA
[CurrencyCode] => SEK
[ExchangeRate] => 1
[LanguagePath] => se
[FreightWithoutVat] => 0
[FreightWithVat] => 0
[FreightVatPercentage] => 25
[PayoptionFeeWithoutVat] => 0
[PayoptionFeeWithVat] => 0
[PayoptionFeeVatPercentage] => 25
[CodWithoutVat] => 0
[CodWithVat] => 0
[CodVatPercentage] => 0
[DiscountWithoutVat] => 0
[DiscountWithVat] => 0
[DiscountVat] => 0
[TotalWithoutVat] => 4388
[TotalWithVat] => 5485
[TotalVat] => 1097
[PayWithoutVat] =>
[AffiliateCode] =>
[AffiliateName] =>
[OrderField] => Array
(
[0] => Array
(
[Name] => external_ref
[Value] => 43445
)
[1] => Array
(
[Name] => webshopid
[Value] => 423
)
[2] => Array
(
[Name] => webshopname
[Value] => Manuell
)
)
)
)
Non working code
echo $array[1][0]
I have tried different combos of indexes. I know how to return the values from the soap object but if I could do it this way it would be easier. It should work shouldn't it?
$array[1] is the second index of the array. the key of this array us "Status", this array contains a code and message
i assume you want to echo the message, you can do that with the following
echo $array[1]["Status"]["Message"];
You should use $array['Status']['Code'] , $array['Status']['Message'], $array['Order']['OrderNumber'], $array['Order']['Customer']['CustomerId'] and so on to display your data. It's an associative array so you need to use string keys and not numbers
try
$array['Order']['Customer']['LastName']
is my best guess without losing my sanity in that one line.
But for us to be sure please post the print_r($array) output
There are some way I always do this:
print_r($array);
And the other way is
$array[0]['Order']['LastName']
Try to access the arrays elements with the string keys, not the integer ones you are using:
echo $array['Order']['Customer']['Address'];
Another way you could see what is going on is by iterating through the array, and print out the keys and values:
foreach ($array as $key => $value)
echo "Key=$key value=$value<br>";
I have this:
print_r($response["member"]);
I need to retrieve name under levels, it's at the bottom, how should I write it:
I thought of $response["member"][0]["Sequential"]["levels"]...? Also this number under levels wont be always the same.
Thank you!
Array
(
[0] => Array
(
[ID] => 1
[UserInfo] => Array
(
[ID] => 1
[caps] => Array
(
[administrator] => 1
)
[cap_key] => wp_capabilities
[roles] => Array
(
[0] => administrator
)
[allcaps] => Array
(
[switch_themes] => 1
[edit_themes] => 1
[activate_plugins] => 1
[edit_plugins] => 1
[edit_users] => 1
[edit_files] => 1
[manage_options] => 1
[moderate_comments] => 1
[manage_categories] => 1
[manage_links] => 1
[upload_files] => 1
[import] => 1
[unfiltered_html] => 1
[edit_posts] => 1
[edit_others_posts] => 1
[edit_published_posts] => 1
[publish_posts] => 1
[edit_pages] => 1
[read] => 1
[level_10] => 1
[level_9] => 1
[level_8] => 1
[level_7] => 1
[level_6] => 1
[level_5] => 1
[level_4] => 1
[level_3] => 1
[level_2] => 1
[level_1] => 1
[level_0] => 1
[edit_others_pages] => 1
[edit_published_pages] => 1
[publish_pages] => 1
[delete_pages] => 1
[delete_others_pages] => 1
[delete_published_pages] => 1
[delete_posts] => 1
[delete_others_posts] => 1
[delete_published_posts] => 1
[delete_private_posts] => 1
[edit_private_posts] => 1
[read_private_posts] => 1
[delete_private_pages] => 1
[edit_private_pages] => 1
[read_private_pages] => 1
[delete_users] => 1
[create_users] => 1
[unfiltered_upload] => 1
[edit_dashboard] => 1
[update_plugins] => 1
[delete_plugins] => 1
[install_plugins] => 1
[update_themes] => 1
[install_themes] => 1
[update_core] => 1
[list_users] => 1
[remove_users] => 1
[add_users] => 1
[promote_users] => 1
[edit_theme_options] => 1
[delete_themes] => 1
[export] => 1
[administrator] => 1
)
[filter] =>
[user_login] => admin
[user_nicename] => admin
[user_email] => goranefbl#gmail.com
[user_url] =>
[user_registered] => 2014-01-29 10:57:09
[user_activation_key] =>
[user_status] => 0
[display_name] => admin
[wlm_feed_url] => http://pialarson.com/excel/feed/?wpmfeedkey=1;2e7e48ca65d94e5f0ec1baae46e4972c
[wpm_login_date] => 1392155735
[wpm_login_ip] => 62.68.119.252
)
[Sequential] =>
[Levels] => Array
(
[1391447566] => stdClass Object
(
[Level_ID] => 1391447566
[Name] => Team Membership
[Cancelled] =>
[CancelDate] =>
[Pending] =>
[UnConfirmed] =>
[Expired] =>
[ExpiryDate] => 1393866766
[SequentialCancelled] =>
[Active] => 1
[Status] => Array
(
[0] => Active
)
[Timestamp] => 1391447566
[TxnID] => WL-1-1391447566
)
)
[PayPerPosts] => Array
(
)
)
)
An answer might be to use array_walk_recursive by following the official documentation:
http://www.php.net/manual/en/function.array-walk-recursive.php
<?php
$properties = new stdClass();
$properties->names = [];
function extractNames($levels, $key, $properties) {
if (
is_object($levels) &&
array_key_exists('Name', get_object_vars($levels)) &&
array_key_exists('Level_ID', get_object_vars($levels))
) {
$properties->names[] = $levels->Name;
}
}
array_walk_recursive($response, 'extractNames', $properties);
echo print_r($properties, true);
<?php
$nameInFirstLevelsElement = current($response["member"][0]["levels"])->Name
?>
This should work to retrieve the Name from the first levels element.
That empty space next to "Sequential" means it doesn't have a value. So that's not the one you're looking for.
Furthermore, one of those levels indicates "stdClass object", which means you can access its members via the -> operator.
Let's strip away everything that doesn't matter for a minute. I think it'll help you understand the data structure:
Array
(
[0] => Array
(
[Levels] => Array
(
[1391447566] => stdClass Object
(
[Level_ID] => 1391447566
[Name] => Team Membership
)
)
)
)
So this will work:
$object = $response["member"][0]["Levels"][1391447566];
$name = $object->Name;
Edit
If the index of Levels changes every time, then pull it apart a little bit more...
$levels = $response["member"][0]["Levels"];
$firstLevel = array_shift(array_values($levels));
$name = $firstLevel->Name;
See here for a good answer on getting the first element out of the $levels array: https://stackoverflow.com/a/3771228/266374
If the number under the 'Levels' array won't be the same, you can use a foreach to pull out the 'Name' information.
foreach ($response[0]['Levels'] AS $level_key => $level_val) {
$level_name = $level_key->Name;
}
echo 'Name: '.$level_name;
If there is only going to be one element, then it will grab it. If there are multiple numbers under 'Levels', then it will loop through them and assign each one to '$level_name', overwriting any previous assignments. In other words, only the last one it finds will be captured.
EDIT:
In the example, I mistakenly tried to grab the Name from the $key instead of the $val. This is the correct method:
foreach ($response[0]['Levels'] AS $level_key => $level_val) {
$level_name = $level_val->Name;
}
echo 'Name: '.$level_name;
Here is a demo of the working code
I'm so close to this I could just scream.
Here's what I'm after. I have two arrays. The first one is a follows:
array("id", "txtLname", "txtFname");
The second is as follows:
Array
(
[0] => Array
(
[id] => 220
[RecordGUID] => 1233C9-1F7A15-E8A447-C56CB2-227C20-2829E0
[txtEmplid] => 5469857
[txtLname] => Jones
[txtFname] => Richard
[txtMname] =>
[txtEmail] => email address
[Reg_Pass] => umbra1234
[Reg_User] => user
[txtSecEmail] => user#gmail.com
[dtDOB] => 1979-02-28
[drpStatus] => STUDENT
[lstWaive] => 1
[ENTERED] => 2013-09-15 18:03:18
[Status] => 0
[Approvalcode] => 17dc8e7336f0e9fd3411a4d9617efe865c5744ac
[Approvaldate] => 2013-09-15 18:03:47
[Approved] => 1
)
[1] => Array
(
[id] => 221
[RecordGUID] => DD1E72-368879-68CFE2-E03010-ECE1B1-0974E9
[txtEmplid] => 4454688
[txtLname] => Mathews
[txtFname] => Richard
[txtMname] =>
[txtEmail] => user2#gmail.com
[Reg_Pass] => umbra1234
[Reg_User] => user
[txtSecEmail] => user3#gmail.com
[dtDOB] => 1979-02-28
[drpStatus] => STUDENT
[lstWaive] => 1
[ENTERED] => 2013-09-16 12:28:08
[Status] => 0
[Approvalcode] => 7182769e45dc38a3a747c4bdb128e2f0a8c658e4
[Approvaldate] =>
[Approved] => 0
)
[2] => Array
(
[id] => 222
[RecordGUID] => 40D8E7-04C600-30A829-8E26CC-498BBE-9D3DF6
[txtEmplid] =>
[txtLname] =>
[txtFname] =>
[txtMname] =>
[txtEmail] =>
[Reg_Pass] =>
[Reg_User] =>
[txtSecEmail] =>
[dtDOB] => 1979-02-28
[drpStatus] => STUDENT
[lstWaive] => 1
[ENTERED] => 2013-09-16 12:28:24
[Status] => 0
[Approvalcode] => 8d2c33f7b7d6ef620811dbc4358e8103346bfb59
[Approvaldate] =>
[Approved] => 0
)
)
All I need is to loop through the second array and remove the items that do not match the first. The result would be as follows:
Array
(
[0] => Array
(
[id] => 220
[txtLname] => Method
[txtFname] => Richard
)
[1] => Array
(
[id] => 221
[txtLname] => Method
[txtFname] => Richard
)
[2] => Array
(
[id] => 222
[txtLname] => Method
[txtFname] => Richard
)
)
Here's what I've done so far.
foreach ($r as $reg) {
foreach($reg as $k => $v) {
if ($k == !in_array($k, $f)) {
echo $reg[$k];
}
}
}
The echo response I get is a listing of all of the data from the first array, minus the fields that are in the first array. So, it is removing items that
I want removed, but I can't seem to get it into the proper format.
Thanks in advance for your help.
$arr1 = array("id"=>1, "txtLname"=>1, "txtFname"=>1)
$result = array();
// $arr2 = array 2 (check your code)
foreach ($arr2 as $el) {
$result[] = array_intersect_key($el, $arr1);
}
var_dump($result);
replace echo $reg[$k]; with unset($reg[$k]);
Hello people here is the code that i was using initially....
array_push($Parameter_IdArray, $Parameter_Id1, $Parameter_Id2, $Parameter_Id3, $OptParameter_Id);
array_push($Eqt_ParamArray, $eqt_param1, $eqt_param2, $eqt_param3, $Opt_eqt_param1);
i had no issues to push array values .... but now $eqt_param1, $eqt_param2, $eqt_param3 and $Opt_eqt_param1 are in one more array it is something like this
Array ( [Profile_Id] => 4 [eqt_param] => Array ( [0] => 4.00 [1] => 4.00 [2] => 4.00 ) [Parameter_Id1] => 8 [min_param] => Array ( [0] => 1.00 [1] => 1.00 [2] => 1.00 ) [max_param] => Array ( [0] => 5.00 [1] => 5.00 [2] => 5.00 ) [Wtg_param] => Array ( [0] => 25.00 [1] => 25.00 [2] => 50.00 ) [Parameter_Id2] => 5 [Parameter_Id3] => 1 [Opt_eqt_param] => Array ( [0] => 0.00 ) [OptParameter_Id] => 14 [Opt_wtg] => Array ( [0] => 1.05 ) [operator] => Array ( [0] => M ) [eqt_pay] => 1,574,235 [rec_sal] => 1,485,000 [#] => -6.01 % [Emp_Id] => 490699 [Emp_Process] => Confirm New Pay )
now i need tp push array values $eqt_param1, $eqt_param2, $eqt_param3 and $Opt_eqt_param1 to $Eqt_ParamArray how to do that?
If I understand correctly, you've now got an associative array in PHP. On that assumption, I went ahead and reformatted the nightmare for you:
$myarray = array(
"Profile_Id" => 4,
"eqt_param" => array(4.00, 4.00, 4.00),
"Parameter_Id1" => 8,
"min_param" => array (1.00, 1.00, 1.00 ),
"max_param" => array (5.00, 5.00, 5.00 ),
"Wtg_param" => array (25.00, 25.00, 50.00 ),
"Parameter_Id2" => 5,
"Parameter_Id3" => 1,
"Opt_eqt_param" => array (0.00),
"OptParameter_Id" => 14,
"Opt_wtg" => array (1.05),
"operator" => array ("M"),
"eqt_pay" => array(1,574,235),
"rec_sal" => array(1,485,000),
"#" => "-6.01 %",
"Emp_Id" => 490699,
"Emp_Process" => "Confirm New Pay"
);
What this good formatting now makes clear is that you can use array_push directly on the "eqt_param" index:
array_push($myArray["eqt_param"], $eqt_param1, $eqt_param2, $eqt_param3, $Opt_eqt_param1);
You may also mean that you want to replace it, which is easy too:
$myArray['eqt_param'] = array($eqt_param1, $eqt_param2, $eqt_param3, $Opt_eqt_param1);
The same principles apply in Javascript, which you have had tagged so maybe you mean that.