Modifying an array inside an array - php

So I need to modify the array in a memcached key-value pair. I need to remove one of the arrays inside the array. An example of what it looks like:
array(2) { [0]=> array(3) { ["username"]=> string(3) "Bob" ["id"]=> string(5) "14537" ["comment"]=> string(4) "cool"} [1]=> array(3) { ["username"]=> string(3) "Tom" ["id"]=> string(5) "14538" ["comment"]=> string(3) "yes"}}
If I know the values of username, id, and comment, how can I delete it? The generic queston: How can I delete array 0?
Considering the answer of doing a foreach loop, I tried
foreach($memcachedarray as $f){
if ($f['id'] == '14537'){
echo key($f);
}
}
But it spits out username
Edit- Ok
I searched some more and found I need to do this:
foreach($memcachedarray as $key => $f){
if ($f['id'] == '14537'){
echo $key;
}
}
That works!

If the Id's are unique across the system then you could use an associative array to store you data then unset the key, otherwise you would want to use a foreach loop to get the array key, then unset that key and recommit your new array back into memcache.

Related

PHP Multidimensional Array Extract Specific Values Based Upon Key

I'm trying to create an anchor link by extracting specific array values based upon based upon the key. I've tried using a foreach loop inside of a for loop, however that doesn't seem to work.
Based upon the below multidimensional array how can I loop through each subarray to create individual anchor links, such as:
Example:
/* Array Example */
array(3) {
[0]=>
array(2) {
["#attributes"]=>
array(1) {
["id"] => string(1) "2"
}
["name"]=> string(10) "Mark"
}
[1]=>
array(2) {
["#attributes"]=>
array(1) {
["id"]=> string(1) "4"
}
["name"]=> string(8) "John"
}
[2]=>
array(2) {
["#attributes"]=>
array(1) {
["id"]=> string(1) "5"
}
["name"]=> string(10) "Suzy"
}
/* Desired Output */
Mark
John
Suzy
Let's assume the array you posted is the content of a variable called $users. You can walk through it by doing
foreach ($users as $usr)
{
$usr['#attributes']['id'];
$usr['name'];
}
This way, you can go through every node without worrying about the indexes.
You may output the link on each foreach iteration in several ways. A complete example (which allows to use HTML without escaping every special character) could be:
<?php
foreach ($users as $usr)
{ ?>
<?php echo $usr['name']; ?>
<?php } ?>
While it looks complex, with a lot of PHP opening and closing tags, it makes it easier on the markup with almost no performance penalty
You can use either ways: ($array is your array)
$names = array_column($array, 'name');
$ids = array_map(function($ele){return $ele['#attributes']['id'];}, $array);

php array loop to get array

I have a $date array like this:
[1]=> array(11) {
["meetingname"]=> string(33) "win2008connectcurrent0423131"
[0]=> array(3) {
["scoid"]=> string(7) "3557012"
["datecreated"]=> string(19) "2013-05-23 10:02:39"
["numparticipants"]=> string(1) "3"
}
[1]=> array(3) {
["scoid"]=> string(7) "3557012"
["datecreated"]=> string(19) "2013-05-23 14:40:06"
["numparticipants"]=> string(1) "3"
}
}
foreach($date[0] as $key => $meetings){
print "$key = $meetings\n";////yields scoid = 3557012
}
And, as you can see above, I am looping over individual elements. The first element (not indexed) is always meetingname; the rest of the elements are indexed and themselves contain arrays with three elements in each array--in the above code they are [0] and [1].
What I need to do is make the $meetings as an array containing [0] and then [1] etc, depending on the number of elements. So essentially, the output for print should be an array (I can also use var_dump) with key/values of [0] but right not it only outputs individual keys and their values, for example, as you can see above, scoid=3557012. I will need, something all keys/values in the $meetings variable, something like:
{
["scoid"]=> string(7) "3557012"
["datecreated"]=> string(19) "2013-05-23 10:02:39"
["numparticipants"]=> string(1) "3"
}
How can I fix the foreach loop for that?
please Try this. hope it help.
foreach($date as $key => $meetings){
if($key == "meetingname")
continue;
else
print "$meetings\n";
}
You can just create a new array and add the meetings to that one
<?php
$meetings = array();
foreach($date[1] as $key=>$meeting) {
if (!is_int($key))
continue; //only handle numeric keys, incase you ever change the name of the first key 'meetingname'
$meetings[] = $meeting
}
var_dump($meetings);
?>

Modify data in php codeigniter active record result without changing structure

I've got a codeigniter active record query that uses get('table_name')->result();
The output is below, what i'd like to do is unserialize(base_64_decode) the "venue_opening_hours" string and replace that string in the data structure with the result of the unserialized & base64_decoded data. I know i can array_walk to do this normally, but i don't see how to access that particular bit of data as an object within an array...
Thanks!
array(2) {
[0]=>
object(stdClass)#142 (4) {
["entry_id"]=>
string(2) "15"
["google-id"]=>
string(40) "552e7c08d3b86c14d130ebe43a0ba421d03a60ae"
["venue_opening_hours"]=>
string(148) "YToxOntzOjEzOiJvcGVuaW5nX2hvdXJzIjthOjE6e3M6NzoicGVyaW9kcyI7YToxOntzOjQ6Im9wZW4iO2E6Mjp7czozOiJkYXkiO3M6MToiNSI7czo0OiJ0aW1lIjtzOjQ6IjIzMTUiO319fX0="
["title"]=>
string(18) "Place Name"
}
[1]=>
object(stdClass)#143 (4) {
["entry_id"]=>
string(2) "18"
["google-id"]=>
string(40) "71d9c8e1f64f330637c96d30a0ae15533836a85e"
["venue_opening_hours"]=>
string(972) "YToxOntzOjEzOiJvcGVuaW5nX2hvdXJzIjthOjE6e3M6NzoicGVyaW9kcyI7YToxMDp7aTowO2E6MTp7czo1OiJjbG9zZSI7YToyOntzOjM6ImRheSI7czoxOiIxIjtzOjQ6InRpbWUiO3M6NDoiMjMzMCI7fX1pOjE7YToxOntzOjQ6Im9wZW4iO2E6Mjp7czozOiJkYXkiO3M6MToiMSI7czo0OiJ0aW1lIjtzOjQ6IjIzMTUiO319aToyO2E6MTp7czo1OiJjbG9zZSI7YToyOntzOjM6ImRheSI7czoxOiIxIjtzOjQ6InRpbWUiO3M6NDoiMjMxNSI7fX1pOjM7YToxOntzOjQ6Im9wZW4iO2E6Mjp7czozOiJkYXkiO3M6MToiMSI7czo0OiJ0aW1lIjtzOjQ6IjIzMzAiO319aTo0O2E6MTp7czo1OiJjbG9zZSI7YToyOntzOjM6ImRheSI7czoxOiIyIjtzOjQ6InRpbWUiO3M6NDoiMjMxNSI7fX1pOjU7YToxOntzOjQ6Im9wZW4iO2E6Mjp7czozOiJkYXkiO3M6MToiMiI7czo0OiJ0aW1lIjtzOjQ6IjIzMzAiO319aTo2O2E6MTp7czo1OiJjbG9zZSI7YToyOntzOjM6ImRheSI7czoxOiI0IjtzOjQ6InRpbWUiO3M6NDoiMjMzMCI7fX1pOjc7YToxOntzOjQ6Im9wZW4iO2E6Mjp7czozOiJkYXkiO3M6MToiNCI7czo0OiJ0aW1lIjtzOjQ6IjIzMzAiO319aTo4O2E6MTp7czo1OiJjbG9zZSI7YToyOntzOjM6ImRheSI7czoxOiI1IjtzOjQ6InRpbWUiO3M6NDoiMjM0NSI7fX1pOjk7YToxOntzOjQ6Im9wZW4iO2E6Mjp7czozOiJkYXkiO3M6MToiNSI7czo0OiJ0aW1lIjtzOjQ6IjIzNDUiO319fX19"
["title"]=>
string(24) "Other place name"
}
}
You'd access it using:
$array[0]->venue_opening_hours
in a for loop...
//foreach ($array as &$arrayItem)
//{
foreach ($arrayItem as &$object)
{
// extract and convert it...
//$openinghours = unserialize(base64_decode($object->venue_opening_hours));
// Update it...
$object->venue_opening_hours = $unserialize(base64_decode($object->venue_opening_hours));
}
//}
The &$object is a reference, so the assignment will change the value in the original result set...
Also I forgot to also loop the array ... I think! so added the outer loop :)

PHP Array_intersect on multidimensional array with unknown number of keys

I'm trying to make advanced search filters in an application that holds resources (people). I've got all the results in 1 multidimensional array. A user of the application can search for the persons Job title, skills, work field and country.
I've already made the part where I look up the people that meet the criteria given by the user. These results are stored in a multidimensional array. If the user is looking for someone with a specific resource with a job title and a specific skill the return value is this:
$realfilters = array(2) {
["resourcesWithJobtitle"]=> array(6) {
[0]=> string(1) "1"
[1]=> string(2) "48"
[2]=> string(2) "88"
}
["resourcesWithSkill"]=> array(9) {
[0]=> string(1) "4"
[1]=> string(1) "8"
[2]=> string(1) "48"
[3]=> string(2) "50"
}
When the user also looks for a work field this is added to the result:
["resourcesWithWorkfield"]=> array(3) {
[0]=> string(2) "48"
[1]=> string(2) "96"
[2]=> string(2) "97"
}
I need to know which resources meet all dimensions of the array so I can display them. (So in this example I need an array with just 1 value: 48). I think I need to use array_intersect but can't seem to get it right.
One of the possible solutions: you may first extract() the $realfilters array values to variables, and then apply the array_intersect() to them. But this solution is applicable only if there are not many possible filters.
Another one and probably the best solution would be to intersect in a loop, something like:
$res_arr = array_shift($realfilters);
foreach($realfilters as $filter){
$res_arr = array_intersect($res_arr, $filter);
}
$intersection = call_user_func_array('array_intersect', $array);
That will give you the elements present in all the sub arrays of $array.
edit-
This above is like a shortcut for writing:
$intersection = array_intersect($array['a'], $array['b'], ...and so on for all elements...);
A loop could be used as well
$intersection = reset($array);
foreach ($array as $subarr) {
$intersection = array_intersect($intersection, $subarr);
}
print_r($intersection);

PHP var_dump array

array(14) {
[0]=>
string(1) "1"
["id"]=>
string(1) "1"
[1]=>
string(7) "myUserName"
["UserID"]=>
string(7) "myUserName"
[2]=>
string(10) "myPassword"
["passwordID"]=>
string(10) "myPassword"
[3]=>
string(24) "myEmail#domain.com"
["emailAddress"]=>
string(24) "myEmail#domain.com"
[4]=>
string(7) "myFirstName"
["firstName"]=>
string(7) "myFirstName"
[5]=>
string(8) "myLastName"
["lastName"]=>
string(8) "myLastName"
[6]=>
string(1) "1"
["active"]=>
string(1) "1"
}
how do i access the contents of this array using PHP?
the above was a var_dump($info)
It depends on which part of the array you are trying to access.
If you are trying to access a specific item, you can access it by its index ; for instance :
echo $info['passwordID'];
Should give you :
myPassword
(edit after the comment)
For the email address, there is this portion in your var_dump's output :
["emailAddress"]=>
string(24) "myEmail#domain.com"
This indicates that the e-mail address is stored in the array as an element that has the key "emailAddress".
Which means you should be able to get that e-mail address like this :
echo $info['emailAddress'];
And as you also have this portion of text in the var_dump's output :
(About that duplication of data, you should read Pekka's answer, who provides an idea of why your data is in your array twice, with both integers and strings as keys)
[3]=>
string(24) "myEmail#domain.com"
You could also use :
echo $info[3];
(of course, in each of those cases, you could also store this to a variable for futures re-use)
Another solution, if you want to access each item, would be to use some foreach loop ; for instance :
foreach ($info as $key => value) {
echo "Value for key $key is $value <br />";
}
You might want to go through the arrays section of the PHP manual, for more informations.
And, also, the section about array functions.
You can use either the numeric or the associative key:
echo $array[0]; // outputs 1
echo $array["id"]; // outputs 1
I'm guessing this is the result of a mysql_fetch_array() operation, isn't it? You may want to specify whether you want a numeric or associative array using the second parameter to that function.
Example:
$record = mysql_fetch_array($query, MYSQL_ASSOC); // for associative keys only
$record = mysql_fetch_array($query, MYSQL_NUM); // for numeric keys only
The array appears to have both string and numeric keys. You can access the fields using the array index operator []. Supply either the numeric key or the column name:
echo $info['UserID']; // output "myUserName"
echo $info['emailAddress']; // output "myEmail#domain.com"
if $info is the array, then you can echo $info[6] for example.
If you want it as a string then $s=print_r($info,true);
It seems you are doing some thing wrong, as there shouldn't be a need to access array like that, and there other ways to access objects as arrays.

Categories