错误消息:在访问具有与迄今为止所看到的不同结构的数组时,非法偏移 [英] Error message: Illegal offset while accessing the array having a different structure than was seen so far

查看:143
本文介绍了错误消息:在访问具有与迄今为止所看到的不同结构的数组时,非法偏移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我一直在解决这个问题的时候,我从这里提到的最后一个问题上进一步走向了人民。然而,我发现很难摆脱访问数组中元素的问题,因为结构本身对我来说似乎并不熟悉。从数据库分配2个SQL结果后,得到的数组如下所示。打印出来的时候看这个键,看起来和我目前看到的结构有所不同。
如果结构是正确的形式,我想知道如何从数组的第一行访问article_title,article_desc,article_category,以及下列行中的所有标题。我非常感谢您的时间和建议。试图访问时,我不断收到错误说非法抵消。

  $ sqlarticle1 =选择article_title,article_desc,article_category FROM crd_article_desc请检查并提醒您有什么问题。 where article_id ='$ CurrentTitleId'; //获取基于ID的文章。 
$ sqlarticle2 =SELECT article_title FROM crd_article_desc where article_category ='$ CurrentCategory'; //获取所有类别

$ 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;



$ b $数组的结构是
$ b $数组(
[0] =>数组([0] => HTM应用程序
[article_title] => HTM应用程序
[1] => HTML
的使用[article_desc] => HTML的使用b $ b [2] => HTML
[article_category] ​​=> HTML

[1] => Array([0] => HTML Tags
[article_title] => HTML Tags

[2] =& 0] => HTM应用程序
[article_title] => HTM应用程序

[3] => Array([0] => HTML元素的使用
[article_title] => HTML元素的使用

[4] =>数组([0] =>元素
[article_title] =>元素

[5] => Array([0] => jobs_codeigniter_page
[article_title] => jobs_codeigniter_page

);

以下是我试图从数组中访问元素的方法。 b

  foreach($ finalarray as $ key => $ value){
echo $ key;
foreach($ value as $ innerkey => $ innervalue){
echo $ innervalue ['article_title'];



$ div $解析方案

不需要有两个 foreach 循环来检查结果,你的数组并不那么深。

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

$ / code>

您的第一个 循环是毫无意义的,因为你不追加到 $ finalarray 。不知道这是否是有意的。


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']; 
    } 
}

解决方案

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.

这篇关于错误消息:在访问具有与迄今为止所看到的不同结构的数组时,非法偏移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆