php: write an array by clone [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
i got these arrays;
$arr1=array("1","2","3","4");
$arr2=array("a","b","c","d");
$x=array(1,3,4,2,4,1,2,2,3,4,4,4,1);
I want to print $x to the screen like this:
a,c,d,b,d,a,b,b,c,d,d,d,a

While you could use a foreach loop like a primitive oaf (I'm kidding, don't hate me!), you could instead use functions that are designed for such things.
$map = array_combine($arr1,$arr2);
$result = array_map(function($i) use ($map) {return $map[$i];}, $x);
echo implode(",",$result);
Documentation: array_combine, array_map, anonymous functions, implode.
As a general rule, using built-in functions will often be faster than iterating yourself.

#Niet the Dark Absol Good answer
here is the one using foreach - using #sjagr suggested code
$arr1=array("1","2","3","4");
$arr2=array("a","b","c","d");
$x=array(1,3,4,2,4,1,2,2,3,4,4,4,1);
foreach ($x as $k) { echo $arr2[$k-1].','; }

Related

I'm learning functional programming & i'm wondering if there's a better way to do this? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
Is there a more "functional" way to do this? Yes i know it's PHP and i'm trying to fit a square peg into a round hole. I'm trying to be more functional minded even though it's not a functional language.
$c = [];
foreach ($classes as $cl) {
$c[$cl->Id] = $cl->Name;
}
What you are doing is array_column.
Array_column can isolate a column in an array and set the key name with the third parameter.
$c= array_column($classes, 'Name', 'Id');
I would use this function:
$c = array_column($classes, 'Name', 'Id');
"Functional" way will be like this:
$result = array_reduce(
$classes,
function ($t, $cl) {
$t[$cl->Id] = $cl->Name;
return $t;
},
[]
);

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)

Appending to an array without copying [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
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.

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

How to get values from array [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am trying to get strings from a long array value.
for example
$array[1]='this is a good day. The number:1, class:math';
$array[2]='this is a bad day. The number:2, class:english';
$array[3]='this is a fine day. The number:3, class:physics';
I want to get the number:1 or class:math strings out of the array.
I tried
echo array_search('number:1',$array);
but it gave me nothing. I was wondering if there are better ways to do this. Thanks a lot!
I guess you're searching for something like the following. Searching for a needle inside of values of arrays.
<?php
function array_search_inline($needle, $haystack) {
foreach ($haystack as $key => $value) {
if (strpos($value, $needle) !== false) {
return $key;
}
}
return false;
}
?>
Note: array_search simply compares the array's values and does not search inside of them.

Categories