I would like to grab a data of words and store into an array tree.
Let’s say i have a column of words in a db table:
altar, offer
and
turn, unto
I want to store like this:
Array(Array(“altar”, “offer”), Array(“turn”, “unto”))
I would like to grab a data of words and store into an array tree.
Let’s say i have a column of words in a db table:
altar, offer
and
turn, unto
I want to store like this:
Array(Array(“altar”, “offer”), Array(“turn”, “unto”))
Those are two separate records right?
So you’d initialize an array, $myArray = array();
Then as you fetch each record you’d push the column values into that array, splitting on the delimiter so it create an internal array.
array_push($myArray, $columnValue.split(','));
oh sorry i forgot to mention this. What you said is simple. But i need to explode the string of numbers, not words, let’s say 1-6 in one record and then 2-7. This is what I have so far:
if(0<count($joinids12)){
for($j=0;$j<count($joinids12);$j++){
if($joinids12[$j]!="" || $joinids12[$j]!=NULL){
//echo $joinids12[$j];
$join_ids = explode("-", $joinids12[$j]);
//print_r($join_ids);
$sql13part = "SELECT * FROM ".$dbTable3." WHERE ";
$sql13parta = "(book = ".$b1." AND chapter = ".$c1.") AND (";
$sql13partb = "(book = ".$b2." AND chapter = ".$c2.") AND (";
$sql13 = "";
//echo count($join_ids);
for($k=0;$k<count($join_ids);$k++){
$sql13 .= "text_data LIKE '%".$words12[$join_ids[$k]-1]."%' ";
//$word-color[] =
if($k<count($join_ids)-1){
$sql13 .= "OR ";
}
$joinidsArr[$k] = Array();
array_push($joinidsArr[$k], $join_ids[$k]);
}
$sql13 .= ") ";
$sql13a = $sql13part.$sql13parta.$sql13;
$sql13b = $sql13part.$sql13partb.$sql13;
}
}
}
but there is something wrong with the Array push part.
The result/outcome is:
array(2) {
[0]=>
array(1) {
[0]=>
string(1) "2"
}
[1]=>
array(1) {
[0]=>
string(1) "7"
}
}
So the value of $join_ids[$k]
is either
array(1) {
[0]=>
string(1) "2"
}
on the first result, and
array(1) {
[0]=>
string(1) "7"
}
on the second result. Are you simply wanting the outcome to be
array(2) {
[0]=>
string(1) "2"
[1]=>
string(1) "7"
}
If so, you need to replace array_push($joinidsArr[$k], $join_ids[$k]);
with
array_push($joinidsArr[$k], $join_ids[$k][0]);
I want to store like this:
Array(Array(1, 6), Array(2, 7))
the initial strings are 1-6 and 2-7. So i explode where - is. but i need to store in 2 arrays nested in an array.
oh, I see, so you are looking for a method that shrinks down the array or flattens it. I’ll see what I can come up with.
Given:
array(2) {
[0]=>
array(1) {
[0]=>
string(1) "2"
}
[1]=>
array(1) {
[0]=>
string(1) "7"
}
}
You want
array(2) {
[0]=>
string(1) "2"
[1]=>
string(1) "7"
}
Then you can push that array onto an existing.
Here is something that does it
<?php
$myArray = array(array("2"), array("7"));
var_dump($myArray);
/* produces
array(2) {
[0]=>
array(1) {
[0]=>
string(1) "2"
}
[1]=>
array(1) {
[0]=>
string(1) "7"
}
}
*/
function flatten($item) {
return $item[0];
}
$mappedArray = array_map("flatten", $myArray);
var_dump($mappedArray);
/* produces
array(2) {
[0]=>
string(1) "2"
[1]=>
string(1) "7"
}
*/
yes but it’s forgetting the previous array 1, 6
This is what i need:
array(2) {
[0]=>
[0]=>
string(1) “1”
[1]=>
string(1) “6”
[1]=>
[0]=>
string(1) “2”
[1]=>
string(1) “7”
}
Right, but if you apply the array_map where the push is happening, it should produce the output you are seeking.
i don’t understand where $item is coming from.
I strongly suggest reading the manual entry for array_map then.
array_map will call the function ‘flatten’ for each item in $myArray. When it does, it passes the element, I named that element $item, so I can interact with it in the function.
What is $item supposed to represent in this case then?
`function flatten($item) {
return $item[0];
}
/*
$mappedArray = array_map(“flatten”, $myArray);
var_dump($mappedArray);*/
if(0<count($joinids12)){
for($j=0;$j<count($joinids12);$j++){
if($joinids12[$j]!="" || $joinids12[$j]!=NULL){
//echo $joinids12[$j];
$join_ids = explode("-", $joinids12[$j]);
//print_r($join_ids);
$sql13part = "SELECT * FROM ".$dbTable3." WHERE ";
$sql13parta = "(book = ".$b1." AND chapter = ".$c1.") AND (";
$sql13partb = "(book = ".$b2." AND chapter = ".$c2.") AND (";
$sql13 = "";
//echo count($join_ids);
for($k=0;$k<count($join_ids);$k++){
$sql13 .= "text_data LIKE '%".$words12[$join_ids[$k]-1]."%' ";
//$word-color[] =
if($k<count($join_ids)-1){
$sql13 .= "OR ";
}
$joinidsArr[$k] = Array();
//array_push($joinidsArr[$k], $join_ids[$k]);
//array_push($joinidsArr[$k], $join_ids[$k][0]);
array_map("flatten", $join_ids[$k][0]);
}
$sql13 .= ") ";
$sql13a = $sql13part.$sql13parta.$sql13;
$sql13b = $sql13part.$sql13partb.$sql13;
}
}
}
var_dump($joinidsArr);`
if I had to guess, as the easiest way is to debug this program to figure out where in your logic you need to flatten an array, but I think there are two possibilities here
for($k=0;$k<count($join_ids);$k++){
$sql13 .= "text_data LIKE '%".$words12[$join_ids[$k]-1]."%' ";
//$word-color[] =
if($k<count($join_ids)-1){
$sql13 .= "OR ";
}
$joinidsArr[$k] = Array();
array_push($joinidsArr[$k], $join_ids[$k]);
}
Let’s start with
$joinidsArr[$k] = Array();
Why do you have this line? Where does $joinidsArr
get defined and how is it defined?
If you didn’t have that line you might be able to do the below and not have an array in an array. And have zero reason to flatten it.
array_push($joinidsArr, $join_ids[$k]);
Now, if that must be kept that way, then the other alternative, is to add this after your for loop (I think)
$joinidsArr = array_map("flatten", $joinidsArr);
You really should try to debug your application to figure out where it is creating this array of an array so you know what variable needs to be flattened.
first thing is to grab the records which are 1-6 and 2-7;
next is to explode where you find the “-”;
And following that is to store them in an array of arrays:
Array(Array(1,6), Array(2-7)).
I was doing 2 things in the for loop. The 2nd thing was to create 2 sql statements. But maybe these 2 should be mixed together.
This:
if(0<count($joinids12)){
for($j=0;$j<count($joinids12);$j++){
if($joinids12[$j]!="" || $joinids12[$j]!=NULL){
//echo $joinids12[$j];
$join_ids = explode("-", $joinids12[$j]);
//print_r($join_ids);
$sql13part = "SELECT * FROM ".$dbTable3." WHERE ";
$sql13parta = "(book = ".$b1." AND chapter = ".$c1.") AND (";
$sql13partb = "(book = ".$b2." AND chapter = ".$c2.") AND (";
$sql13 = "";
//echo count($join_ids);
for($k=0;$k<count($join_ids);$k++){
$sql13 .= "text_data LIKE '%".$words12[$join_ids[$k]-1]."%' ";
//$word-color[] =
if($k<count($join_ids)-1){
$sql13 .= "OR ";
}
$joinidsArr[$k] = Array();
//array_push($joinidsArr[$k], $join_ids[$k]);
array_push($joinidsArr, $join_ids[$k][0]);
//array_map("flatten", $join_ids[$k][0]);
}
$sql13 .= ") ";
$sql13a = $sql13part.$sql13parta.$sql13;
$sql13b = $sql13part.$sql13partb.$sql13;
}
}
}
Brought this (The 1st variable “1” is missing)
array(5) {
[0]=>
array(0) {
}
[1]=>
array(0) {
}
[2]=>
string(1) "6"
[3]=>
string(1) "2"
[4]=>
string(1) "7"
}
This actually worked!
$joinidsArr = Array();
if(0<count($joinids12)){
for($j=0;$j<count($joinids12);$j++){
if($joinids12[$j]!="" || $joinids12[$j]!=NULL){
//echo $joinids12[$j];
$join_ids = explode("-", $joinids12[$j]);
//print_r($join_ids);
$sql13part = "SELECT * FROM ".$dbTable3." WHERE ";
$sql13parta = "(book = ".$b1." AND chapter = ".$c1.") AND (";
$sql13partb = "(book = ".$b2." AND chapter = ".$c2.") AND (";
$sql13 = "";
//echo count($join_ids);
for($k=0;$k<count($join_ids);$k++){
$sql13 .= "text_data LIKE '%".$words12[$join_ids[$k]-1]."%' ";
//$word-color[] =
if($k<count($join_ids)-1){
$sql13 .= "OR ";
}
//$joinidsArr[$k] = Array();
//array_push($joinidsArr[$k], $join_ids[$k]);
array_push($joinidsArr, $join_ids[$k][0]);
//array_map("flatten", $join_ids[$k][0]);
}
$sql13 .= ") ";
$sql13a = $sql13part.$sql13parta.$sql13;
$sql13b = $sql13part.$sql13partb.$sql13;
}
}
}
var_dump($joinidsArr);
Result:
array(4) {
[0]=>
string(1) "1"
[1]=>
string(1) "6"
[2]=>
string(1) "2"
[3]=>
string(1) "7"
}
BUt I just noticed there’s no array within an array.
What if you take
array_push($joinidsArr, $join_ids[$k][0]);
and move it outside of that for loop and change it to
array_push($joinidsArr, $join_ids);
What does that create?
what’s the point taking it outside if it relies on the for loop to fetch the next record?
Sorry, let me be more specific about which for loop.
This one, try it like the following:
for($k=0;$k<count($join_ids);$k++){
$sql13 .= "text_data LIKE '%".$words12[$join_ids[$k]-1]."%' ";
//$word-color[] =
if($k<count($join_ids)-1){
$sql13 .= "OR ";
}
//$joinidsArr[$k] = Array();
//array_push($joinidsArr[$k], $join_ids[$k]);
//array_push($joinidsArr, $join_ids[$k][0]);
//array_map("flatten", $join_ids[$k][0]);
}
array_push($joinidsArr, $join_ids);
It almost worked:
$joinidsArr = Array();
if(0<count($joinids12)){
for($j=0;$j<count($joinids12);$j++){
if($joinids12[$j]!="" || $joinids12[$j]!=NULL){
//echo $joinids12[$j];
$join_ids = explode("-", $joinids12[$j]);
//print_r($join_ids);
$sql13part = "SELECT * FROM ".$dbTable3." WHERE ";
$sql13parta = "(book = ".$b1." AND chapter = ".$c1.") AND (";
$sql13partb = "(book = ".$b2." AND chapter = ".$c2.") AND (";
$sql13 = "";
//echo count($join_ids);
for($k=0;$k<count($join_ids);$k++){
$sql13 .= "text_data LIKE '%".$words12[$join_ids[$k]-1]."%' ";
//$word-color[] =
if($k<count($join_ids)-1){
$sql13 .= "OR ";
}
//$joinidsArr[$j] = Array();
//array_push($joinidsArr[$k], $join_ids[$k]);
//array_push($joinidsArr, $join_ids[$k][0]);
//array_map("flatten", $join_ids[$k][0]);
//unset($joinidsArr);
}
array_push($joinidsArr, $join_ids);array_push($joinidsArr, $join_ids);
$sql13 .= ") ";
$sql13a = $sql13part.$sql13parta.$sql13;
$sql13b = $sql13part.$sql13partb.$sql13;
}
}
}
//var_dump($joinidsArr);
print_r($joinidsArr);
Brought this:
Array
(
[0] => Array
(
[0] => 1
[1] => 6
)
[1] => Array
(
[0] => 1
[1] => 6
)
[2] => Array
(
[0] => 2
[1] => 7
)
[3] => Array
(
[0] => 2
[1] => 7
)
)
So for some reason it yielded double the result.
You have it in there twice…