I have this array:
Array
(
[V] => Array
(
[0] => S
)
[L] => Array
(
[0] => M
[1] => Z
[2] => G
)
[S] => Array
(
[0] => T
[1] => X
[2] => K
)
[T] => Array
(
[0] => D
)
[B] => Array
(
[0] => E
[1] => N
[2] => Y
)
[W] => Array
(
[0] => J
[1] => Y
)
[G] => Array
(
[0] => A
[1] => K
)
[J] => Array
(
[0] => T
)
[X] => Array
(
[0] => H
)
[E] => Array
(
[0] => Z
)
[N] => Array
(
[0] => B
)
[Z] => Array
(
[0] => A
[1] => M
)
[U] => Array
(
[0] => R
)
[F] => Array
(
[0] => C
[1] => V
)
[I] => Array
(
[0] => F
)
[C] => Array
(
[0] => O
[1] => P
[2] => T
)
[Q] => Array
(
[0] => L
[1] => V
)
[Y] => Array
(
[0] => C
)
[K] => Array
(
[0] => T
[1] => P
[2] => A
)
[A] => Array
(
[0] => J
)
[M] => Array
(
[0] => W
)
[R] => Array
(
[0] => I
)
[P] => Array
(
[0] => K
[1] => N
)
[O] => Array
(
[0] => G
)
[H] => Array
(
[0] => Q
)
[D] => Array
(
[0] => U
)
)
Serialized:
a:26:{s:1:"V";a:1:{i:0;s:1:"S";}s:1:"L";a:3:{i:0;s:1:"M";i:1;s:1:"Z";i:2;s:1:"G";}s:1:"S";a:3:{i:0;s:1:"T";i:1;s:1:"X";i:2;s:1:"K";}s:1:"T";a:1:{i:0;s:1:"D";}s:1:"B";a:3:{i:0;s:1:"E";i:1;s:1:"N";i:2;s:1:"Y";}s:1:"W";a:2:{i:0;s:1:"J";i:1;s:1:"Y";}s:1:"G";a:2:{i:0;s:1:"A";i:1;s:1:"K";}s:1:"J";a:1:{i:0;s:1:"T";}s:1:"X";a:1:{i:0;s:1:"H";}s:1:"E";a:1:{i:0;s:1:"Z";}s:1:"N";a:1:{i:0;s:1:"B";}s:1:"Z";a:2:{i:0;s:1:"A";i:1;s:1:"M";}s:1:"U";a:1:{i:0;s:1:"R";}s:1:"F";a:2:{i:0;s:1:"C";i:1;s:1:"V";}s:1:"I";a:1:{i:0;s:1:"F";}s:1:"C";a:3:{i:0;s:1:"O";i:1;s:1:"P";i:2;s:1:"T";}s:1:"Q";a:2:{i:0;s:1:"L";i:1;s:1:"V";}s:1:"Y";a:1:{i:0;s:1:"C";}s:1:"K";a:3:{i:0;s:1:"T";i:1;s:1:"P";i:2;s:1:"A";}s:1:"A";a:1:{i:0;s:1:"J";}s:1:"M";a:1:{i:0;s:1:"W";}s:1:"R";a:1:{i:0;s:1:"I";}s:1:"P";a:2:{i:0;s:1:"K";i:1;s:1:"N";}s:1:"O";a:1:{i:0;s:1:"G";}s:1:"H";a:1:{i:0;s:1:"Q";}s:1:"D";a:1:{i:0;s:1:"U";}}
This means...
V can go to S
L can go to M, Z and G
S can go to T, X and K
...
I need an algorithm to find the shortest way from A to Z. This way should be saved as a list of the nodes, something like AJTDURIFCPNBEZ.
I just tried it with this recursion:
function getWays($key = "A") {
global $roads;
echo $key;
if($key == "Z"){
echo "<br />";
return;
}
foreach($roads[$key] as $next)
if($next != "A")
getWays($next);
}
This do not work, it is much more complicate. You have to prevent loops and increase performance (ignore long pathes early, if you already found shorter one).
Can anybody provide help?
Related
I am trying to use a csv file in my php code. I tried this:
if (($handle = fopen("licences/myFile.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000)) !== FALSE) {
$num = count($handle);
$row++;
for ($cc=0; $cc < $num; $cc++) {
$UserLine = explode(";",trim($data[$cc]));
$Users =$UserLine[$cc];
echo "</br>" . print_r($UserLine) . "</br>";
}
}
}
But the output is not what I expect. I get this when I do the print_r:
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
It's repeating the values again and again.
The content of "myFile.csv" is:
GL;
papb1;
RES;
ecllit;
And my variable $users as the value:
GLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllit
Can someone help me to only have the values one time, if it's not in the array at least in $users. Because I have a lot of users to add in the csv, but I want this step to work before writing all the users.
Sorry if it's a basic question or if I haven't given enough informations or if this is not clear, this is my first post :/
Thanks for any future replies,
Sellion
Use this updated.
Your csv file needs to be something like this.
GL
papb1
RES
ecllit
Then use the updated code as below.
if (($handle = fopen("file.csv", "r")) !== FALSE)
{
$line = 1;
while (($data = fgetcsv($handle, 1000)) !== FALSE)
{
echo "Line # $line : " . $data[0] . "</br>";
$line++;
}
}
This will give you output like below :
Line # 1 : GL
Line # 2 : papb1
Line # 3 : RES
Line # 4 : ecllit
I hope that is something you are looking for.
I Have following array and
Array (
[0] => Array ( [70] => Array ( [0] => d ) )
[1] => Array ( [71] => Array ( [0] => a ) )
[2] => Array ( [71] => Array ( [0] => c ) )
[3] => Array ( [71] => Array ( [0] => d ) )
[4] => Array ( [72] => Array ( [0] => b ) )
[5] => Array ( [73] => Array ( [0] => a ) )
[6] => Array ( [73] => Array ( [0] => c ) )
[7] => Array ( [74] => Array ( [0] => a ) )
[8] => Array ( [75] => Array ( [0] => a ) )
)
I want to make the above array like as following one
Array (
[70] => Array ( [0] => c )
[71] => Array ( [1] => b [2] => c )
[72] => Array ( [2] => c )
[73] => Array ( [0] => a [2] => c )
[74] => Array ( [4] => c )
[75] => Array ( [5] => c )
)
Please help me how to make this in php?
I have a theoretical question that I cannot seem to figure out. Imagine I have the following data in a database:
Main Sub1 Sub2
a x y
x t u
u f g
I want to make a multidimensional array in PHP/mYSQL by essentially asking "what is each 'main' component made of?"
The result would be something like this:
Array
(
[0] => a
(
[0] => x
(
[0] => t
[1] => u
)
(
[0] => f
[1] => g
)
[1] => y
)
)
My efforts result in lots of arrays, instead of a multidimensional array.
You can use references to solve this, although the results will get a bit messy:
$res = [];
foreach ($rows as $row) {
// check if we have each sub component
if (!isset($res[$row['sub1']])) {
$res[$row['sub1']] = $row['sub1'];
}
if (!isset($res[$row['sub2']])) {
$res[$row['sub2']] = $row['sub2'];
}
// build new component with references to the sub components
$res[$row['main']] = [&$res[$row['sub1']], &$res[$row['sub2']]];
}
print_r($res);
Output
Array
(
[x] => Array
(
[0] => t
[1] => Array
(
[0] => f
[1] => g
)
)
[y] => y
[a] => Array
(
[0] => Array
(
[0] => t
[1] => Array
(
[0] => f
[1] => g
)
)
[1] => y
)
[t] => t
[u] => Array
(
[0] => f
[1] => g
)
[f] => f
[g] => g
)
You can clean up the results by filtering out only the arrays:
print_r(array_filter($res, 'is_array'));
Output
Array
(
[x] => Array
(
[0] => t
[1] => Array
(
[0] => f
[1] => g
)
)
[a] => Array
(
[0] => Array
(
[0] => t
[1] => Array
(
[0] => f
[1] => g
)
)
[1] => y
)
[u] => Array
(
[0] => f
[1] => g
)
)
I need to merge a PHP array, this array has 2 arrays into it named "targetXX", I can have 2 or more. Each target have the same keys, for each key I have an array with 2 values a and b, a is always the same in both targets, but I need to merge both B values like this:
Array
(
[0] => Array
(
[target] => hitcount(stats.asdf1.requests, "1min")
[datapoints] => Array
(
[0] => Array
(
[0] => 1200
[1] => 1392282200
)
[1] => Array
(
[0] => 1400
[1] => 1392282260
)
[2] => Array
(
[0] => 600
[1] => 1392282320
)
[3] => Array
(
[0] => 200
[1] => 1392282380
)
[4] => Array
(
[0] => 400
[1] => 1392282440
)
[5] => Array
(
[0] => 600
[1] => 1392282500
)
)
)
[1] => Array
(
[target] => hitcount(stats.asdf.requests, "1min")
[datapoints] => Array
(
[0] => Array
(
[0] => 4321
[1] => 1392282200
)
[1] => Array
(
[0] => 76567
[1] => 1392282260
)
[2] => Array
(
[0] => 5556
[1] => 1392282320
)
[3] => Array
(
[0] => 7675
[1] => 1392282380
)
[4] => Array
(
[0] => 2344
[1] => 1392282440
)
[5] => Array
(
[0] => 0999
[1] => 1392282500
)
)
)
Result:
Array
(
[0] => Array
(
[target] => hitcount(stats.asdf1.requests, "1min")
[datapoints] => Array
(
[0] => Array
(
[0] => 1200
[1] => 1392282200
[2] => 4321
)
[1] => Array
(
[0] => 1400
[1] => 1392282260
[2] => 76567
)
[2] => Array
(
[0] => 600
[1] => 1392282320
[2] => 5556
)
[3] => Array
(
[0] => 200
[1] => 1392282380
[2] => 7675
)
[4] => Array
(
[0] => 400
[1] => 1392282440
[2] => 2344
)
[5] => Array
(
[0] => 600
[1] => 1392282500
[2] => 0999
)
)
)
Use array_merge() to achieve this:
$newArray = array();
foreach ($myArray['target2'] as $key => $innerArr1) {
$newArray['target'][$key] = array_merge(
$myArray['target1'][$key], /* 0th and 1st index */
array($innerArr1[1]) /* 2nd index */
);
}
print_r($newArray);
Output:
Array
(
[target] => Array
(
[0] => Array
(
[0] => 333333
[1] => 13
[2] => 99
)
[1] => Array
(
[0] => 444444
[1] => 15
[2] => 98
)
[2] => Array
(
[0] => 555555
[1] => 17
[2] => 97
)
)
)
Demo
The built-in function array_merge may do the work for you. You need to merge each subarrays in fact, as the array_merge_recursive function doesn't handle indexes.
$newArray = array();
foreach ($myArray['target2'] as $key => $arr) {
$newArray['target'][$key] = array_merge($myArray['target1'][$key], $arr[1]);
}
Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.
If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.
If you have more than 2 keys to merge, you can loop on the algorithm multiple times.
I've been playing with this all day and haven't figured out a good way to do it...
I have two arrays and am trying to create an array based on matching values.
//$original
Array
(
[0] => Array
(
[items] => Array
(
[0] => Array
(
[0] => PA
[1] => DZ
[2] => ER
[3] => TY
)
[1] => Array
(
[0] => KV
[1] => EN
[2] => CR
)
[2] => Array
(
[0] => HU
[1] => GO
[2] => GA
[3] => FI
)
)
)
)
//$compare
Array
(
[0] => Array
(
[items] => Array
(
[0] => Array
(
[0] => PA
[1] => AN
[2] => ER
)
[1] => Array
(
[0] => KV
)
[2] => Array
(
[0] => HU
[1] => XV
[2] => ZL
[3] => FI
)
)
)
)
And I'm trying to produce
//$similar
Array
(
[0] => Array
(
[items] => Array
(
[0] => Array
(
[0] => PA
[2] => ER
)
[1] => Array
(
[0] => KV
)
[2] => Array
(
[0] => HU
[3] => FI
)
)
)
)
Use array_intersect.
$similar = $compare;
$similar[0]['items'] = array_intersect($compare[0]['items'], $original[0]['items']);
codepad example