Build multidimensional array from mysql database using php - 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
)
)

Related

Get shortest path through nodes

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?

Merge two array with indexes

I have two array. I want to merge these two array with the same index.
Array
(
[0] => a
[1] => b
[2] => c
)
Array
(
[0] => 1
[1] => 2
[2] => 3
)
And I want this after merge
Array
(
[0] => a
[1] => 1
[2] => b
[0] => 2
[1] => c
[2] => 3
)
in php you can do simply
$arr=array();
$arr_one=array('a','b','c');
$arr_two=array(1,2,3);
array_push($arr,$arr_one);
array_push($arr,$arr_two);
print_r($arr);

PHP: How to pairing up an arrey values

Array is like..
array (
[0] => a
[1] => b
[2] => c
[3] => d
[4] => e
[5] => f
[6] => g
[7] => h
)
I want pair them like..
Array
( [0] => Array (
[0] => a
[1] => b)
[1] => Array (
[0] => c
[1] => d)
[2] => Array (
[0] => e
[1] => f)
[3] => Array (
[0] => g
[1] => h)
)
How can i do this? there are 8 values and I want 4 pairs from these 8 values. I am new to php so I don't really knw much about php and I searched google but can't find a solution for this.
You can use array_chunk():
print_r(array_chunk($arr, 2));
Demo

Does sort function consider the leaf nodes in a multilevel array?

This following piece of code is meant to create a multilevel array, print it, then shuffle it, again print it, and sort the array.
$arr=array(
array(
array('a','b','c')
),
array(
array('d','e','f')
),
array(
array('g','h','i')
),
);
print_r($arr);
shuffle($arr);
print_r($arr);
sort($arr);
print_r($arr);
Now the odd thing that i observed, when shuffle() is used, it only shuffles the indexes of the array that is being shuffled, it doesnt shuffle, the inner most elements a,b,c to something else, but when the the sort() function is used, it sorts back the array into the normal status and the leaf nodes are back to alphabetical order. Why does this happen?
Here is the sample output:
*Original Array*
Array
(
[0] => Array
(
[0] => Array
(
[0] => a
[1] => b
[2] => c
)
)
[1] => Array
(
[0] => Array
(
[0] => d
[1] => e
[2] => f
)
)
[2] => Array
(
[0] => Array
(
[0] => g
[1] => h
[2] => i
)
)
)
Shuffled Array
Array
(
[0] => Array
(
[0] => Array
(
[0] => g
[1] => h
[2] => i
)
)
[1] => Array
(
[0] => Array
(
[0] => a
[1] => b
[2] => c
)
)
[2] => Array
(
[0] => Array
(
[0] => d
[1] => e
[2] => f
)
)
)
Sorted Array
Array
(
[0] => Array
(
[0] => Array
(
[0] => a
[1] => b
[2] => c
)
)
[1] => Array
(
[0] => Array
(
[0] => d
[1] => e
[2] => f
)
)
[2] => Array
(
[0] => Array
(
[0] => g
[1] => h
[2] => i
)
)
)
See the array section of PHP's comparison operator reference, especially the array comparison transcription. Basically PHP first compares the amount of keys in the array, then checks if the arrays have the same keys (in this case, 0 for the inner arrays) and then compares the values. Because you have a nested array there, it proceeds to compare the leaf nodes in sort(), which leads to the array being sorted in this case by the first value of the leaf arrays (a, d and g).
shuffle() simply reorders the indexes of the array you give it regardless of what the array contains, thus it does not touch the inner arrays at all.

php - matching elements in an array

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

Categories