As I kept on resolving the issue I moved further from the last issue that was mentioned here for the people. However, I find it very difficult to get out of the problem accessing the elements in my array as the structure itself seems to be unfamiliar to me. The resultant array is obtained after being assigned with 2 SQL results from the DB as seen below. When looking at the key after printing it out, it seems to appear different from the structures I have seen so far.
If the structure is in the correct form I would like to know how to access article_title,article_desc,article_category from the first row in the array and all the titles from the following rows.I am grateful for your time and the advises. I keep getting the error saying "illegal offset" when trying to access. Please have a look and advise as to what is wrong with it.
$sqlarticle1 = "SELECT article_title, article_desc, article_category FROM crd_article_desc where article_id='$CurrentTitleId'";//Get article based on ID.
$sqlarticle2 = "SELECT article_title FROM crd_article_desc where article_category = '$CurrentCategory'";//Fetch all the categories
$result1 = mysqli_query($con,$sqlarticle1);
$result2 = mysqli_query($con,$sqlarticle2);
$finalarray = array();
while($record1 = mysqli_fetch_array($result1)) {
$finalarray = $record1;
}
while($record2 = mysqli_fetch_array($result2)) {
$finalarray[] = $record2;
}
Structure of the array is
Array (
[0] => Array ( [0] => HTM applications
[article_title] => HTM applications
[1] => Uses of HTML
[article_desc] => Uses of HTML
[2] => HTML
[article_category] => HTML
)
[1] => Array ( [0] => HTML Tags
[article_title] => HTML Tags
)
[2] => Array ( [0] => HTM applications
[article_title] => HTM applications
)
[3] => Array ( [0] => Usage of HTML Elements
[article_title] => Usage of HTML Elements
)
[4] => Array ( [0] => Elements
[article_title] => Elements
)
[5] => Array ( [0] => jobs_codeigniter_page
[article_title] => jobs_codeigniter_page
)
);
Below is how I am trying to access the elements from the array.
foreach ( $finalarray as $key=>$value ){
echo $key;
foreach($value as $innerkey=>$innervalue) {
echo $innervalue['article_title'];
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…