Appending to an array without copying [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Generally we add to an array with
$myarray[] = $myItem
but if $myItem is a massive object I don't want it to get copied, instead I want it to be assigned by reference. Something like
$myarray[] = &$myItem
but that doesn't work and instead replaces every element in $myarray with $myItem
I tried
$myarray[count($myarray)] = &$myItem
but it still replaces everything in $myarray
I am running PHP v5.5

Objects are always assigned by reference. So:
$collection = array();
$blah = new Blah();
$blah->param = "something";
$collection[] = $blah;
$blah->param = "changed";
echo $collection[0]->param; // will output "changed"

According to How to push a copy of an object into array in PHP
Objects are always passed by reference in php 5 or later.
Therefore this question isn't really a concern anymore.

Related

How do I configure this php array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a php array:
$msg['Destination']['ToAddresses'][] = "someone#example.com";
How would I configure this array to contain more than one email address?
I am having a hard time figuring out what this method of creating a php array is doing.
If you have already set $msg['Destination']['ToAddresses'] to be an array by using $msg['Destination']['ToAddresses'] = array(); then you can simply add as many email addresses as you want the same way you added the first:
$msg['Destination']['ToAddresses'][] = "someone#example.com";
$msg['Destination']['ToAddresses'][] = "someone2#example.com";
$msg['Destination']['ToAddresses'][] = "someone3#example.com";
You could also add all of them at one time:
$msg['Destination']['ToAddresses'] = array(
"someone#example.com",
"someone2#example.com",
"someone3#example.com"
);
Keep assigning the value to this array. This is basically the third dimension of the array.
$msg['Destination']['ToAddresses'][] = "someone1#example.com";
$msg['Destination']['ToAddresses'][] = "someone2#example.com";
$msg['Destination']['ToAddresses'][] = "someone3#example.com";
$msg['Destination']['ToAddresses'][] = "someone4#example.com";
The first value of array is first email, then second one and so on, if you want to assign an array to that first value you can do like
$msg['Destination']['ToAddresses'][] = array("someone1#example.com","email2#email2.com");
or may be just an array to ToAddresses like this :
$msg['Destination']['ToAddresses']= array("someone1#example.com","email2#email2.com");
There are a couple of ways:
1.
$msg['Destination']['ToAddresses'] = array(
'someone1#example.com',
'someone2#example.com',
// some other emails
);
2.
$msg['Destination']['ToAddresses'][] = "someone1#example.com";
$msg['Destination']['ToAddresses'][] = "someone2#example.com";
This "[]" notation means "append a new value to an array".
If $msg['Destination']['ToAddresses'] subarray doesn't exist before an execution of the first append operator then it will be created, hence you can start using this operator right away without having to create an empty array in advance.
Using the array_push() function in php which insert one or multiple value to the end of an array will solve your question. Find the code below
<?php
$msg['Destination']['ToAddresses'] = array("oyedele#yahoo.com","idow#gmail.com");
$new =array_push($msg['Destination']['ToAddresses'], "itz4mesays#google.net","zya3#demo.com","itz4mesays#google.net");
var_dump($msg['Destination']['ToAddresses']);
?>
You can add as many as possible to the array_push values.
I hope it solves your question.

How to acces the second value [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I was wondering if you could give two values ​​to a class and then acces to the second one by POST, something like this (part of the code):
echo "<select name='selecttsk' id='selecttsk'>";
while($w = $bd->obtener_fila($tasker, 0)){
echo "<option class='opcion1' value ='".$w[1]/$w[2]."'>".$w[0]."</option>";
}
echo "</select>";
?>
and then i need to do something like this in other file
$var = $_POST[selecttsk];
but i need $w[2]
thanks
I suppose your $_POST['selecttsk'] does have the values in the following format:
"foo/bar"
You could use "explode" in PHP to get the second part (bar), for example:
$postvar = $_POST['selecttsk'];
$vars = explode("/", $postvar);
// Then
$var = $vars[1]; // Will be the $w[2] from the form
Look into: http://php.net/explode
Beware that if $w[1] or $w[2] ever contains a "/" you might get unexpected results, you could use the limit function of explode to mitigate that issue.
However - I would generally not recommend this workflow.
Why do you need to send two variables with one select?
(could you show us som example of what $w contains)

PHP string assign to array variable [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have written some php code, trying what is to assign a string (tag_of_organization) to a variable and then latter to assign it to array newvar. As i am totally new to php and search a lot for it but cannot find anything, so i am asking it here how to do this. here it is
$organ='tag_of_organization';
$newvar = array();
$newvar["tag_of_organization"] =$organ;
Try with:
$newvar[$organ] = $organ;
You are already correct. put print_r to test print your array
$organ='tag_of_organization';
$newvar = array();
$newvar["tag_of_organization"] =$organ;
print_r($newvar);
Output
Array ( [tag_of_organization] => tag_of_organization )
Update
you want dynamic result
$organ='tag_of_organization';
$newvar = array();
$newvar[$organ] =$organ;
print_r($newvar);
Output
Array ( [tag_of_organization] => tag_of_organization )

JSON with PHP foreach [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Ok as per your suggestion I updated... By default Laravel returns JSON... I have set it to return an array but I am still getting the same row duplicated using:
$limits = array();
foreach($pieces as $coverage_limit){
$limits[] = coveragelimit::index($coverage_limit);
}
return json_encode($limits);
}
You're just overwriting $limits inside that foreach() loop. Perhaps you mean something more like
foreach($pieces as $coverage_limit){
$limits[] = coveragelimit::index($coverage_limit);
^^--- array push?
}
As well, you don't "implement" JSON instead of arrays. You work with NATIVE data structures, then encode that structure into JSON. JSON's a transport format, it's not something you should ever deal with natively.
the $limits array will hold the last value what coveragelimit::index() returns over the iterate, I would suggest a check on coveragelimit::index() return value if it falls with "Marc B"'s answer.
EDIT:
foreach($pieces as $key=>$coverage_limit) {
$limits[$key] = coveragelimit::index($coverage_limit);
}
or
foreach($pieces as $coverage_limit) {
array_push($limits, coveragelimit::index($coverage_limit));
}
both should returns the same as Marc B's answer

Make a 1 item array be seen as an array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am currently working on a php project where I am trying to loop through an array using a foreach. However, sometimes the array may only contain 1 item so when I try and do a foreach it fails as 1 item is just seen as a normal variable.
Is there a way that I can trick php into thinking that a 1 item array is actually an array and not just a variable so that I don't get this error.
Thanks for your help.
foreach will work fine with arrays of size 0,1 or bigger. I suspect your problem is, that the variable doesn't really contain an array, but some scalar value - in this case use something like
if (!is_array($var)) $var=array($var);
foreach ($var as $item) {
//...
}
if(is_array($arr))
$arr2=$arr
else
$arr2=array($arr)
and then you iterate over $arr2
I would recommend just using a standard for loop. It should work no matter what the length of the array is
for($i = 0, $l = count($myArray); $i < $l; $i+=1){
//code in here
}
But chances are you have an issue with your array to begin. Posting the structure would be helpful, or you should var_dump it to make sure it is indeed an array.

Categories