I'm trying to loop through a multidimensional array, and add a new sub array. My code doesn't return any errors, but it also doesn't add the new item.
I have the following code:
foreach ($data['switches'] as $switch) {
foreach ($switch['atags'] as $attributelist) {
$nohardwareAttribFound = false;
foreach ($attributelist as $attribute) {
$pos = strpos(trim($attribute),'$attr_2_');
if ($pos !==false) {
//echo 'in the loop';
//found it. extract and exit loop
$modelnumber = substr(trim($attribute),8);
$hardwaremodel = array();
$hardwaremodel['tag'] = 'hardware_model:'.$modelnumber;
array_push($switch['atags'],$hardwaremodel);
print_r($switch);
//echo '<br>=====<br>';
$nohardwareAttribFound = true;
}
}//end foreach ($attributelist
}// end foreach ($switch['atags']
if ($nohardwareAttribFound==false) {
$hardwaremodel['tag'] = 'Unknown';
array_push($switch['atags'],$hardwaremodel);
}//end if
}// end foreach ($data['switches']
I would like the data to look like:
[atags] => Array (
[0] => Array ( [tag] => $id_365 )
[1] => Array ( [tag] => $typeid_8 )
[2] => Array ( [tag] => $any_object )
[3] => Array ( [tag] => $casd )
[4] => Array ( [tag] => $unmounted )
[5] => Array ( [tag] => $no_asset_tag )
[6] => Array ( [tag] => $attr_2_1086 )
[7] => Array ( [tag] => $untagged )
[8] => Array ( [tag] => hardware_model:1086 ) ) )
where the last array - element [8], represents a new subarray that I've added. The print_r() statement looks correct, but when I loop through the results that are passed to my view, i can see that in fact, a new tag array has not been added.
Do i need to some sort of a replace instead of the array_push()?
If it's not a good idea to modify an array while looping through it, could I simply check to see if an item exists.
how would i check if the ['atags'] array for each switch contains a [tag] with a value that looks like "$attr_2_NNNN" where N is a number? For example, check out element 6 in the sample array above. The challenge is that it's not always element 6, and you're not always guaranteed that a tag will have the attr_2 value.
I know there is an in_array() function ... i will try something like:
if (in_array(array('$attr_2_'), $switches['atags']))
I have a bug with the logic around the $nohardwareAttribFound variable, which i'm going to fix.
Thanks
First of all: it is not advised to change an array or collection while you are iterating over it.
If you really want to do it like this, then you should take $switch by reference instead of by value. Otherwise you can change $switch to whatever you want, it will not be reflected in the $data['switches'] array.
To take $switch by reference just add the &:
foreach ($data['switches'] as &$switch) {
}
Check out foreach in PHP manual.
EDIT After studying your code, I think this is what you are looking for:
foreach ($data['switches'] as &$switch) {
$hardwaremodel = array();
$hardwaremodel['tag'] = NULL; // initialize to NULL so we can check at the end of the loop if we have found a hardwaremodel or not (so we don't need that bool)
foreach ($switch['atags'] as $attributelist) {
foreach ($attributelist as $attribute) {
$pos = strpos(trim($attribute), '$attr_2_');
if ($pos !== false) {
$modelnumber = substr(trim($attribute), 8);
$hardwaremodel['tag'] = 'hardware_model:' . $modelnumber;
break;
}
}
if ($hardwaremodel['tag'] !== NULL)
break; // exit the loop because we already found a tag
}
if ($hardwaremodel['tag'] === NULL)
$hardwaremodel['tag'] = 'hardware_model:unknown';
// Note that this is a safe place to modify the $switch array
// as we are not currently iterating it
array_push($switch['atags'], $hardwaremodel);
}
Everytime you enter the loop your interation variable (ie. $switch) is a copy - youre not modifying the original array $data. To do that you need to modify the full path like:
array_push($data['switches']['atags'], $newVal)
Or you can pass by reference when you enter the loop like:
foreach ($data['switches'] as &$switch)
{
// ...
}
I think you should replace
foreach ($data['switches'] as $switch)
with
foreach ($data['switches'] as &$switch)
Using a reference should do.
Note: use unset($switch) afterwards to destroy the reference.
Related
I have a multidimensional array which Im trying to pull all the values of a certain key and assign it to a variable.
This is the array:
Array
(
[I_would_not_know_the_name_of_this_key] => Array
(
[interval] => 3600
[display] => Once Hourly
)
[nor_this_one] => Array
(
[interval] => 43200
[display] => Twice Daily
)
[nor_this_one] => Array
(
[interval] => 86400
[display] => Once Daily
)
)
I want to always get the [display] value even when I do not know what the upper level value is.
function which contains the array above, more schedules can be added which is why I said I would not always know the top level key: https://codex.wordpress.org/Function_Reference/wp_get_schedules
My code so far:
$active_cron_schedules = wp_get_schedules(); //this is the
foreach ($active_cron_schedules as $key => $value) {
echo $key;
}
?>
This outputs for example: 'I_would_not_know_the_name_of_this_key', 'nor_this_one', 'nor_this_one', I need to get in deeper.
Arrays have always given me a run for my money in PHP can't figure out how to loop through it :(
Thank you
I think what you are trying to do will be solved with a foreach() loop or array_column() depending on your version of php. The variable part is hard to answer because you have not given an example of what you would be doing with the variable. A common mistake is to overwrite the variable in a loop, but if all you want are all the display values (or any other key), try:
function getValByKey($array, $getKey = 'display')
{
$new = array();
foreach($array as $arr) {
if(empty($arr[$getKey]))
continue;
$new[] = $arr[$getKey];
}
return $new;
}
$result = array(
'key1'=> array('interval'=>1,'display'=>'1d'),
'key2'=> array('interval'=>2,'display'=>'2d'),
'key3'=> array('interval'=>3,'display'=>'3d')
);
// To use
$display = getValByKey($result);
print_r($display);
// Array column has the same basic function, but
// Only available in PHP 5 >= 5.5.0, PHP 7
$display = array_column($result,'display');
print_r($display);
Both give you:
Array
(
[0] => 1d
[1] => 2d
[2] => 3d
)
whatever is the key, you dont even need to know it in a foreach.
here is a sample. $key can be anything. you just have to check for it s interval child element.
$interval_list = array();
foreach ($array as $key => $el) {
if (isset($el['interval'])) {
$interval_list[] = $el['interval'];
}
}
I use PHPExcel to put elements of my Excel file into an array. I then do
print_r($worksheets);
Where $worksheets is the array created through PHPExcel. The output is
Array (
[enquiry] => Array (
[0] => Array (
[0] => 86.141.247.93
)
[1] => Array (
[0] => 188.141.76.143
)
[2] => Array (
[0] => 2.29.20.161
)
)
)
What I need to do is pass each of these IPs as a String to a function. So what I am trying is this
foreach($worksheets as $ip) {
$count = 0;
if ($SpamProtecter->CheckIP($ip[$count][0])) {
print_r("SPAM");
} else {
print_r("GOOD");
}
$count++;
}
The problem I have is that it only prints out one result. How can I pass each array element as a String to CheckIP?
Thanks
You have an intermediate level in your array. Remember, foreach iterates over the top level of elements. You have 3 tiers here
foreach($worksheets['enquiry'] as $ip) { //iterate tier 1
if ($SpamProtecter->CheckIP($ip[0])) { //grab tier 3
print_r("SPAM");
} else {
print_r("GOOD");
}
}
Your $worksheets array only has a single key: enquiry, which is why you're only getting a single result output. Try this:
foreach($worksheets['enquiry']) as $ip) {
if($SpamProtector->CheckIP($ip[0]) {
// ...
I think you can also get rid of that inner $count variable since it is not used anymore.
This could work
foreach($worksheets['enquiry'] as $ip) {
if ($SpamProtecter->CheckIP($ip[0])) {
print_r("SPAM");
} else {
print_r("GOOD");
}
}
array_walk works well in situtations where you want to perform an action on each element in an array.
array_walk($worksheets["enquiry"],
function ($a) use ($SpamProtecter) {
echo $SpamProtecter->CheckIP($a[0])?"GOOD":"BAD";
});
I have the following code, as you can see I would like to create a new array inside the foreach. Even though its adding perfectly fine WITHIN the loop , all seems to be forgotten once the loop is finished.
foreach ($results as $result) {
$result['categories'] = array();
echo '<pre>';print_r($result);echo '</pre>';
}
echo '<pre>';print_r($results);echo '</pre>';
Result of first print_r
Array
(
[word_two_id] => 2
[categories] => Array
(
)
)
Array
(
[word_two_id] => 3
[categories] => Array
(
)
)
Array
(
[word_two_id] => 5
[categories] => Array
(
)
)
Array
(
[word_two_id] => 12
[categories] => Array
(
)
)
Result of second print_r
Array
(
[0] => Array
(
[word_two_id] => 2
)
[1] => Array
(
[word_two_id] => 3
)
[2] => Array
(
[word_two_id] => 5
)
[3] => Array
(
[word_two_id] => 12
)
)
$result in the foreach is going to be overwritten in your loop on every iteration. e.g. every time the loop rolls around, a NEW $result is created, destroying any modifications you'd done in the previous iteration.
You need to refer to the original array instead:
foreach ($results as $key => $result) {
^^^^^^^
$results[$key]['categories'] = array();
^^^^^^^
Note the modifications. You may be tempted to use something like
foreach($results as &$result)
^---
which would have worked, but also leave $result a reference pointing somewhere inside your $results array. Re-using $result for other purposes later on in the code would then be fiddling with your array, leading to very-hard-to-track bugs.
In PHP, the foreach loop operates on a shallow copy of the array, meaning that changes to the elements of the array won't propagate outside of that loop.
To pass the array elements by reference instead of by value, you put an ampersand (&) before the name of the element variable, like so:
foreach ($results as &$result) {
$result['categories'] = array();
echo '<pre>';print_r($result);echo '</pre>';
}
This way, any changes to the array elements are instead performed on a reference to that element in the original array.
Marc B made a good point in his answer regarding a consequence of using this method. After the foreach loop is done and the code continues, the variable $result will continue to exist as a reference to the last element in the array. So, you shouldn't reuse the $result variable without removing its reference first:
unset($result);
You will need the key too
try this
foreach ($results as $key=>$result) {
$result['categories'] = array();
$results[$key] = $result;
}
echo '<pre>';print_r($results);echo '</pre>';
I have a question for you, I need to through an array with other arrays in php but i through only the last array, my array is:
Array
(
[0] => Array
(
[syn_id] => 17070
[syn_label] => fd+dfd
)
[1] => Array
(
[syn_id] => 17068
[syn_label] => fds+dsfds
)
[2] => Array
(
[syn_id] => 17069
[syn_label] => klk+stw
)
)
My php:
$a_ddata = json_decode(method(), true);
foreach ($a_ddata as $a_data)
{
$a_data['syn_label'] = urldecode(utf8_decode($a_data['syn_label']));
}
With this code I through only the last array [2], but how to through array?please help me
I need to get the array:
Array
(
[0] => Array
(
[syn_id] => 17070
[syn_label] => fd dfd
)
[1] => Array
(
[syn_id] => 17068
[syn_label] => fds dsfds
)
[2] => Array
(
[syn_id] => 17069
[syn_label] => klk stw
)
)
$a_ddata = json_decode(method(), true); $i=0;
foreach ($a_ddata as $a_data)
{
$a_data_f[$i]['syn_id'] = $a_data['syn_id'];
$a_data_f[$i]['syn_label'] = urldecode(utf8_decode($a_data['syn_label']));
$i++;
}
This should be your answer..
When you iterate through something using foreach, by default PHP makes a copy of each element for you to use within the loop. So in your code,
$a_ddata = json_decode(method(), true);
foreach ($a_ddata as $a_data)
{
// $a_data is a separate copy of one of the child arrays in $a_ddata
// this next line will modify the copy
$a_data['syn_label'] = urldecode(utf8_decode($a_data['syn_label']));
// but at the end of the loop the copy is discarded and replaced with a new one
}
Fortunately the manual page for foreach gives us a way to override this behavior with the reference operator &. If you place it between the as keyword and your loop variable, you're able to update the source array within your loop.
$a_ddata = json_decode(method(), true);
foreach ($a_ddata as &$a_data)
{
// $a_data is now a reference to one of the elements to $a_ddata
// so, this next line will update $a_ddata's individual records
$a_data['syn_label'] = urldecode(utf8_decode($a_data['syn_label']));
}
// and you should now have the result you want in $a_ddata
This should help:
$a_data['syn_label'][] = urldecode(utf8_decode($a_data['syn_label']));
For each iteration you are only replacing $a_data['syn_label']. By adding [] you are making it a multi dimension array which increments for every iteration.
Array
(
[menu-162] => Array
(
[attributes] => Array
(
[title] => example1
)
[href] => node/13
[title] => test1
)
[menu-219] => Array
(
[attributes] => Array
(
[title] => example2
)
[href] => node/30
[title] => test2
)
)
If I assign the above array to a variable named $hello, now, I want to use a loop only output the menu-162, menu-219.
If I want to only output the attributes title value, if I only want to output the href's value.
How do I write these loops?
foreach ($hello as $item) {
$attr = $item['attributes']['title'];
$href = $item['href'];
echo "attr is {$attr}";
echo "href is {$href}";
}
That should output the attr and href.
You can access titles value within the attributes array like so: $hello['menu-162']['attributes']['title'] and for any other 'menu' you can substitute menu-162 with the appropriate menu-number combination. As for the href a simple $hello['menu-162']['href']
As for a loop to access both the values a simple foreach should suffice:
foreach($hello as $value) {
echo $value['attributes']['title'];
echo $value['href'];
}
foreach($hello as $key => $value) {
switch($key) {
case 'menu-162':
case 'menu-219':
if($value['href'] && $value['attribute'] && $value['attribute']['title']) {
$href = $value['href'];
$attr = $value['attribute']['title'];
}
break;
default:
continue; //didn't find it
break;
}
}
If you do not need the specific menu finding, remove the switch statement. If you do need the specific ids using this is the more scalable solution, and is faster than a nested if. It will also not create notices for variables that don't exist and will only return if both attribute title, and href exist.