php change specific value of nested array item [duplicate] - php

This question already has answers here:
How to modify an array's values by a foreach loop?
(2 answers)
Closed 4 months ago.
I was wondering if it is possible to edit the current object that's being handled within a foreach loop
I'm working with an array of objects $questions and I want to go through and look for the answers associated with that question object in my db. So for each question go fetch the answer objects and update the current $question inside my foreach loop so I can output/process elsewhere.
foreach($questions as $question){
$question['answers'] = $answers_model->get_answers_by_question_id($question['question_id']);
}

There are 2 ways of doing this
foreach($questions as $key => $question){
$questions[$key]['answers'] = $answers_model->get_answers_by_question_id($question['question_id']);
}
This way you save the key, so you can update it again in the main $questions variable
or
foreach($questions as &$question){
Adding the & will keep the $questions updated. But I would say the first one is recommended even though this is shorter (see comment by Paystey)
Per the PHP foreach documentation:
In order to be able to directly modify array elements within the loop precede $value with &. In that case the value will be assigned by reference.

Surely using array_map and if using a container implementing ArrayAccess to derive objects is just a smarter, semantic way to go about this?
Array map semantics are similar across most languages and implementations that I've seen. It's designed to return a modified array based upon input array element (high level ignoring language compile/runtime type preference); a loop is meant to perform more logic.
For retrieving objects by ID / PK, depending upon if you are using SQL or not (it seems suggested), I'd use a filter to ensure I get an array of valid PK's, then implode with comma and place into an SQL IN() clause to return the result-set. It makes one call instead of several via SQL, optimising a bit of the call->wait cycle. Most importantly my code would read well to someone from any language with a degree of competence and we don't run into mutability problems.
<?php
$arr = [0,1,2,3,4];
$arr2 = array_map(function($value) { return is_int($value) ? $value*2 : $value; }, $arr);
var_dump($arr);
var_dump($arr2);
vs
<?php
$arr = [0,1,2,3,4];
foreach($arr as $i => $item) {
$arr[$i] = is_int($item) ? $item * 2 : $item;
}
var_dump($arr);
If you know what you are doing will never have mutability problems (bearing in mind if you intend upon overwriting $arr you could always $arr = array_map and be explicit.

Related

PHP saving a value of array outside of foreach [duplicate]

This question already has answers here:
How to modify an array's values by a foreach loop?
(2 answers)
Closed 4 months ago.
I was wondering if it is possible to edit the current object that's being handled within a foreach loop
I'm working with an array of objects $questions and I want to go through and look for the answers associated with that question object in my db. So for each question go fetch the answer objects and update the current $question inside my foreach loop so I can output/process elsewhere.
foreach($questions as $question){
$question['answers'] = $answers_model->get_answers_by_question_id($question['question_id']);
}
There are 2 ways of doing this
foreach($questions as $key => $question){
$questions[$key]['answers'] = $answers_model->get_answers_by_question_id($question['question_id']);
}
This way you save the key, so you can update it again in the main $questions variable
or
foreach($questions as &$question){
Adding the & will keep the $questions updated. But I would say the first one is recommended even though this is shorter (see comment by Paystey)
Per the PHP foreach documentation:
In order to be able to directly modify array elements within the loop precede $value with &. In that case the value will be assigned by reference.
Surely using array_map and if using a container implementing ArrayAccess to derive objects is just a smarter, semantic way to go about this?
Array map semantics are similar across most languages and implementations that I've seen. It's designed to return a modified array based upon input array element (high level ignoring language compile/runtime type preference); a loop is meant to perform more logic.
For retrieving objects by ID / PK, depending upon if you are using SQL or not (it seems suggested), I'd use a filter to ensure I get an array of valid PK's, then implode with comma and place into an SQL IN() clause to return the result-set. It makes one call instead of several via SQL, optimising a bit of the call->wait cycle. Most importantly my code would read well to someone from any language with a degree of competence and we don't run into mutability problems.
<?php
$arr = [0,1,2,3,4];
$arr2 = array_map(function($value) { return is_int($value) ? $value*2 : $value; }, $arr);
var_dump($arr);
var_dump($arr2);
vs
<?php
$arr = [0,1,2,3,4];
foreach($arr as $i => $item) {
$arr[$i] = is_int($item) ? $item * 2 : $item;
}
var_dump($arr);
If you know what you are doing will never have mutability problems (bearing in mind if you intend upon overwriting $arr you could always $arr = array_map and be explicit.

Join strings and separate with a comma using PHP [duplicate]

This question already has answers here:
Implode all the properties of a given name in an array of object - PHP [duplicate]
(6 answers)
Closed 2 years ago.
Using PHP, I would like to select certain values from an array of objects, join them to form one continuous string separated by commas, and save this to a variable named $isbn.
I have an array of objects named $items. A var_dump($items) produces this. I need to select the item_isbn value.
My desired result is;
echo $isbn
// would produce
// '0-7515-3831-0,978-0-141-38206-7,978-1-30534-114-1'
Getting rid of the hyphens would be a bonus but I think I can achieve this using str_replace.
Here we go:
$isbnList = [];
foreach ($arrayObject as $item) {
if (isset($item->item_isbn)) {
$isbnList[] = $item->item_isbn;
}
}
$isbn = implode(",", $isbnList);
Check it:
echo $isbn;
For your information:
The foreach works because it's an array. It doesn't care each of the item inside it.
Each of the object ($item) in the loop is the default object of php (as the dump data you provided). Which is supposed to be called as $object->property. This object's property are not sure to always available, so checking whether it's available by php built-in function names isset() is essential.
By knowing implode() is built-in function from php to build a string with separator base on an one side array (means each of item in this one side array must be a scalar value (int, string)). We build this array to ready for the call.
The rest is just about syntax to how-to work with array or object... You can easily lookup on php manual page, eg: http://php.net/manual/en/language.types.array.php
Most popular programming languages nowadays provide us built-in function like this implode(), just different naming.

Modify array values in foreach loop [duplicate]

This question already has answers here:
How to modify an array's values by a foreach loop?
(2 answers)
Closed 4 months ago.
I was wondering if it is possible to edit the current object that's being handled within a foreach loop
I'm working with an array of objects $questions and I want to go through and look for the answers associated with that question object in my db. So for each question go fetch the answer objects and update the current $question inside my foreach loop so I can output/process elsewhere.
foreach($questions as $question){
$question['answers'] = $answers_model->get_answers_by_question_id($question['question_id']);
}
There are 2 ways of doing this
foreach($questions as $key => $question){
$questions[$key]['answers'] = $answers_model->get_answers_by_question_id($question['question_id']);
}
This way you save the key, so you can update it again in the main $questions variable
or
foreach($questions as &$question){
Adding the & will keep the $questions updated. But I would say the first one is recommended even though this is shorter (see comment by Paystey)
Per the PHP foreach documentation:
In order to be able to directly modify array elements within the loop precede $value with &. In that case the value will be assigned by reference.
Surely using array_map and if using a container implementing ArrayAccess to derive objects is just a smarter, semantic way to go about this?
Array map semantics are similar across most languages and implementations that I've seen. It's designed to return a modified array based upon input array element (high level ignoring language compile/runtime type preference); a loop is meant to perform more logic.
For retrieving objects by ID / PK, depending upon if you are using SQL or not (it seems suggested), I'd use a filter to ensure I get an array of valid PK's, then implode with comma and place into an SQL IN() clause to return the result-set. It makes one call instead of several via SQL, optimising a bit of the call->wait cycle. Most importantly my code would read well to someone from any language with a degree of competence and we don't run into mutability problems.
<?php
$arr = [0,1,2,3,4];
$arr2 = array_map(function($value) { return is_int($value) ? $value*2 : $value; }, $arr);
var_dump($arr);
var_dump($arr2);
vs
<?php
$arr = [0,1,2,3,4];
foreach($arr as $i => $item) {
$arr[$i] = is_int($item) ? $item * 2 : $item;
}
var_dump($arr);
If you know what you are doing will never have mutability problems (bearing in mind if you intend upon overwriting $arr you could always $arr = array_map and be explicit.

Scoping Issue with PHP Arrays

I'm trying to solve this problem where I have a unique array of values within a specific range. Take this scenario: Generate a fixed value array (90) with unique entries. If you find a duplicate, remove, reindex, and fill the void. I'm running into the problem that conditional statements do not allow you to interact with an array outside of it's scope. I'm aware of array_unique but it doesn't refill those gaps, just makes them. How do I refill those gaps?
EDIT: This was a noobish question about scoping. A simple while loop demonstrated that if conditional statements can modify variables outside of it's scope.
?>
Not sure if this will help but you can always reiterate over the array once the duplicateds have been removed and reindex it if that is what you mean?
Making sure I understand this - you don't care for the array key elements?
You could still use array_unique();
// $array(); is already defined with elements in it
$temp_array = array_unique($array);
if ( !empty ( $temp_array ) ) {
unset($array);
$array = array();
foreach ( $temp_array as $t ) {
array_push($array, $t);
}
}
You'd probably want to put a clause in there stopping it if it goes over 90 elements.

drupal--why $node->taxonomy is an array

someone wrote this code.
foreach ($node->taxonomy as $term) {
$tids[] = 't.tid = %d';
$args[] = $term->tid;
}
how he knows that in foreach "$node->taxonomy" is an array? and when i loop it,
foreach ($node->taxonomy as $term) {
}
the output that i get will be the $term's value. i don't know how it is change into the 't.tid = %d' and $term->tid. thank you.
In Drupal-related code, a $node is almost always an object produced by the node_load() function. Since every module has the opportunity to add its own properties to this object, it's very hard to find a central documentation of these properties.
By experience and by variable inspection, seasoned Drupal developers know that when set $node->taxonomy is always an array of term object (as returned by the taxonomy_get_term() function) indexed by their respective ids (named tids, for Term ID). This array is set by the taxonomy_nodeapi() function when $op == 'load' and is produced by the taxonomy_get_terms() function.
The question give little information but we can guess that the loop is meant to build two arrays used to generate a database query that filter on the tid column matching those of the $node object. Because the terms' data is already stored in the items of $node->taxonomy, let's hope that this query is not used to re-load the terms to display some of their name and/or description. Collecting 't.tid = %d' is probably a bad idea, the query would be better build with a single "tid in (". db_placeholder($args) .")" WHERE clause after collecting all the tids in $args.
The question is very unclear. All Items under the node object are arrays. You can check it yourself bu using:
print_r($node);
die;
Or using any PHP debugger.
for the foreach, It is very simple foreach... I don't understand what is the problem with that.
t.tid is simply an SQL query. %d is a placeholder for $args[], which consists of $term->tid. It's like this structure: PDO connections.

Categories