Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
183 views
in Technique[技术] by (71.8m points)

php - Error message: Illegal offset while accessing the array having a different structure than was seen so far

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You don't need to have two foreach loops to go over the results, your array is not that deep.

foreach ( $finalarray as $key=>$value ) {
    echo $key; 
    echo $value["article_title"];
}

Also your first while loop is pointless, as you're not appending to $finalarray. Not sure if that is intentional or not.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...