username(
[0] => 'andrew';
[1] => 'teddy';
[2] => 'bear';
)
email(
[0] => 'andrew#andrew.com';
[1] => 'teddy#teddy.com';
[2] => 'bear#bear.com';
)
I got 2 Array coming in from post. I am processing this with PHP.
I would like to combine the array so it looks like this.
So I can use a loop on the array to insert a query on a database.
[1] => Array (
[0] => 'andrew';
[1] => 'andrew#andrew.com';
)
[2] => Array (
[0] => 'teddy';
[1] => 'teddy#teddy.com';
)
[3] => Array (
[0] => 'bear';
[1] => 'bear#bear.com';
)
Take a look at array_combine()
If that doesn't solve your problem, you can always just go with a simple loop:
foreach($usernameArray as $k=>$val)
{
if(array_key_exists($k, $emailArray))
{
$combinedArray[$k] = array($val, $emailArray[$k]);
}
}
You need something like:
$res = array ();
for($i=0;$i<count($username);$i++) {
$res[$i][0] = $username[$i];
$res[$i][1] = $email[$i];
}
Related
This question already has answers here:
Get all permutations of a PHP array?
(7 answers)
Closed 6 months ago.
Is there any way I could pull something like this when I call the function:
perm(array("one", "two", "tree"));
that would result to:
Array ( [0] => one [1] => two )
Array ( [0] => one [1] => tree )
Array ( [0] => two [1] => one )
Array ( [0] => two [1] => tree )
Array ( [0] => tree [1] => one )
Array ( [0] => tree [1] => two )
Array ( [0] => one [1] => two [2] => tree )
Array ( [0] => two [1] => one [2] => tree )
Array ( [0] => one [1] => tree [2] => two )
Array ( [0] => tree [1] => one [2] => two )
Array ( [0] => two [1] => tree [2] => one )
Array ( [0] => tree [1] => two [2] => one )
The problem is the array is dynamic and the value could become something like perm(array("one", "two", "tree", "five","8"));
I wanted to generate all the permutation even if the it becomes.
perm(array("one", "two")); or perm(array("two", "tree", "five","8"));
The problem of what I have below is that it starts from 1 permutation instead of 2 permutation above.
</php
function perm($arr, $temp_string, &$collect) {
if ($temp_string != "")
$collect []= $temp_string;
for ($i=0, $iMax = sizeof($arr); $i < $iMax; $i++) {
$arrcopy = $arr;
$elem = array_splice($arrcopy, $i, 1);
if (sizeof($arrcopy) > 0) {
perm($arrcopy, $temp_string ." " . $elem[0], $collect);
} else {
$collect []= $temp_string. " " . $elem[0];
}
}
}
?>
This may not have the same order but this is the same result you are expecting. You just have to add another if condition. I am assuming someone smarter than me here could recreate your function to make it more efficient with lower benchmarks. Otherwise you would have to go for this:
$collect = array();
function permute($arr, $temp_string, &$collect){
for($i=0, $iMax = sizeof($arr); $i < $iMax; $i++){
$arrcopy = $arr;
$elem = array_splice($arrcopy, $i, 1);
$jval = $temp_string." ".$elem[0];
if(!empty($temp_string)){
$collect [] = explode(" ",$jval);
}
if(sizeof($arrcopy) > 0){
permute($arrcopy, $jval, $collect);
}
}
}
permute(array('one', 'two', 'tree'), "", $collect);
Would result to:
Array ( [0] => [1] => one [2] => two )
Array ( [0] => [1] => one [2] => two [3] => tree )
Array ( [0] => [1] => one [2] => tree )
Array ( [0] => [1] => one [2] => tree [3] => two )
Array ( [0] => [1] => two [2] => one )
Array ( [0] => [1] => two [2] => one [3] => tree )
Array ( [0] => [1] => two [2] => tree )
Array ( [0] => [1] => two [2] => tree [3] => one )
Array ( [0] => [1] => tree [2] => one )
Array ( [0] => [1] => tree [2] => one [3] => two )
Array ( [0] => [1] => tree [2] => two )
Array ( [0] => [1] => tree [2] => two [3] => one )
I have this array:
SimpleXMLElement Object
(
[id] => Array
(
[0] => Koala.jpg
[1] => Jellyfish.jpg
)
[desc] => Array
(
[0] => koaladesc
[1] => jelly desc
)
[qtidade] => Array
(
[0] => 1
[1] => 5
)
I need create some php function that help me group the values like this:
[0] => Array
(
[0] => Koala.jpg
[1] => koaladesc
[2] => 1
)
[1] => Array
(
[0] => Jellyfish
[1] => jelly desc
[2] => 5
)
Could anyone help me?
Something like this should do the trick, but it's localized to what you're asking based on the vagueness of your question:
$new_array = array();
foreach($simple_xml_object as $obj) {
if(is_array($obj)) {
for($i = 0; $i < count($obj); $i++) {
$new_array[$i][] = $obj[$i];
}
}
}
I would suggest looking at the documentation on the foreach() construct, as well as looking over the SimpleXML manual.
So, you want to tranpose an array. Here's a magical way of transposing rectangular arrays:
array_unshift($array, null);
$array = call_user_func_array('array_map', $array);
let's suppose your array is saved in variable $arrayValues
$arrayValues = [id] => Array
(
[0] => Koala.jpg
[1] => Jellyfish.jpg
)
[desc] => Array
(
[0] => koaladesc
[1] => jelly desc
)
[qtidade] => Array
(
[0] => 1
[1] => 5
)
now you need to create the following code:
foreach($arrayValues as $array)
{
echo $array->id;
echo $array->desc;
echo $array->qtidade;
}
this may work well for you.
How would I go about flipping an array and establishing relationships between all the values and keys? For example:
I am trying to turn this:
Array (
[11913] => Array (
[0] => 4242
[1] => 3981
)
[9878] => Array (
[0] => 2901
[1] => 3981
)
[11506] => Array (
[0] => 3981
[1] => 2901
)
)
Into this:
Array (
[3981] => Array (
[0] => 11506
[1] => 9878
[2] => 11913
)
[2901] => Array (
[0] => 11506
[1] => 9878
)
[4242] => Array (
[0] => 11913
)
)
Are there any PHP functions that will already do this automatically? If not what would be a way of going about this? Can't seem to wrap my head around it.
Here you go.
$final_array = array();
foreach($initial_array as $key => $val){
foreach($val as $v){
$final_array[$v][] = $key;
}
}
I am trying to write some php code to process the second dimension's value of an array based on similar values of the first dimension values.
Following is the sample output.
[0] => Array (
[0] => 1
[1] => 0.091238491238491
)
[1] => Array (
[0] => 2
[1] => 0.2221793635487
)
[2] => Array (
[0] => 2
[1] => 0.10662717512033
)
[3] => Array (
[0] => 4
[1] => 0.44354338998346
)
[4] => Array (
[0] => 6
[1] => 0.2248243559719
)
[5] => Array (
[0] => 6
[1] => 0.31764705882353
)
[6] => Array (
[0] => 6
[1] => 0.15764625384879
)
[7] => Array (
[0] => 6
[1] => 0.19160083160083
)
[8] => Array (
[0] => 12
[1] => 0.31054875069499
)
[9] => Array (
[0] => 12
[1] => 0.10915034227918
)
[10] => Array (
[0] => 15
[1] => 0.32915461266474
)
//...........goes to 46000 elements
Now what I want to do is, if the index 0 values of each array is similar then I want to add the index 1's value.
So for example, if 0 index values for 4 arrays are same , I want to add index 1 values of all 4 arrays.
If there is a unique value on 0th index, dont add it with anything, simply store index 1's value and move on.
Thanks very much.
Ghanshyam
$added = array();
foreach ($array as $item) {
if (isset($added[$item[0]])) {
$added[$item[0]] += $item[1];
} else {
$added[$item[0]] = $item[1];
}
}
$p=0;
$temp = $final_prod_ex[0][1];
for($x=0; $x<count($final_prod)-1; $x++){
if($final_prod_ex[$x][0]==$final_prod_ex[$x+1][0]){
$temp = $temp + $final_prod_ex[$x+1][1];
}
else{
$ans[$p] = $temp." ".$final_prod_ex[$x][0];
$temp = $final_prod_ex[$x+1][1];
$p++;
}
}
Finally figured it out after a lot of thinking(I'm new to programming)...Array's name is $final_prod_ex. Comment on this if I can make it better. And sorry #deceze. I could not understand your solution. I know you were trying to give the value of one array as an index to another. But what the scenario is, that value isnt like 0,1,2,3,4.... Its like 1,3,5,6,7,10. We are missing numbers in between. Maybe I didnt understand your solution. Correct me if I am wrong.
Thanks for all the help.
i have a multidimensional array whose index/keys (not the values) are like this:
this is how the submitted array looks
[param] => Array
(
[3] => groupedlista
[0] => groupedlistb
[2] => groupedlistc
)
[f_name] => Array
(
[3] => grouplistaa
[0] => grouplistbb
[2] => grouplistcc
)
[f_label] => Array
(
[3] => grouplistL3
[0] => grouplistL0
[2] => grouplistL2
)
this is how the order looks
0,2,3
i want that Result
[param] => Array
(
[0] => groupedlistb
[1] => groupedlistc
[2] => groupedlista
)
[f_name] => Array
(
[0] => grouplistbb
[1] => grouplistcc
[2] => grouplistaa
)
[f_label] => Array
(
[0] => grouplistL0
[1] => grouplistL2
[2] => grouplistL3
)
that's it
PS: i use a jquery sort / add / delete feature in the form and i prefer to do the final sorting php-based. the index array [$i] is required to be declared at the form.
$order = '0,2,3';
$out = array(); // This will hold the sorted values
$order = explode(',',$order); // Turn the order into an array
foreach ($multiDimArray as $key => $subArray) { // Loop outer array
foreach ($order as $pos) { // Loop order array
if (isset($subArray[$pos])) { // Make sure the key exists
$out[$key][] = $subArray[$pos]; // Put the correct value in the correct place
}
}
}
print_r($out);