解析json_decode()多个结果

时间:2014-08-12 14:26:44

标签: php linkedin json

如果我的术语不正确,在这方面相当新手,请原谅我。我一直在使用Linked in API,并设法返回JSON格式。像这样:

 "skills": {
"_total": 5,
"values": [
  {
    "id": 1,
    "skill": {"name": "British Sign Language"}
  },
  {
    "id": 7,
    "skill": {"name": "Drupal"}
  },
  {
    "id": 8,
    "skill": {"name": "WordPress"}
  },
  {
    "id": 9,
    "skill": {"name": "iOS development"}
  },
  {
    "id": 10,
    "skill": {"name": "Magento"}
  }
]
}

我需要将其中的每一个添加到一个数组中,然后将它们添加到数据库中。我尝试像这样解码JSON:

$linkedInResults = json_decode($JSON_response, true);

然后尝试将它们添加到这样的数组中:

$arr_skills = array();
foreach ($linkedInResults->skills as $addSkill) {
    $arr_skills[] = $addSkill;
}
var_dump($arr_skills);

但是我得到了这个错误:

Warning: Invalid argument supplied for foreach()

并且var转储的输出是:

array(0) { } 

编辑:使用:$ linkedInResults ['skills']给出:

 array(2) {
[0] => int(13)[1] => array(13) {
    [0] => array(2) {
        ["id"] => int(1)["skill"] => array(1) {
            ["name"] => string(21)
            "British Sign Language"
        }
    }[1] => array(2) {
        ["id"] => int(7)["skill"] => array(1) {
            ["name"] => string(6)
            "Drupal"
        }
    }[2] => array(2) {
        ["id"] => int(8)["skill"] => array(1) {
            ["name"] => string(9)
            "WordPress"
        }
    }[3] => array(2) {
        ["id"] => int(9)["skill"] => array(1) {
            ["name"] => string(15)
            "iOS development"
        }
    }[4] => array(2) {
        ["id"] => int(10)["skill"] => array(1) {
            ["name"] => string(7)
            "Magento"
        }
    }[5] => array(2) {
        ["id"] => int(11)["skill"] => array(1) {
            ["name"] => string(10)
            "Web Design"
        }
    }[6] => array(2) {
        ["id"] => int(12)["skill"] => array(1) {
            ["name"] => string(8)
            "Blogging"
        }
    }[7] => array(2) {
        ["id"] => int(13)["skill"] => array(1) {
            ["name"] => string(30)
            "iPhone Application Development"
        }
    }[8] => array(2) {
        ["id"] => int(14)["skill"] => array(1) {
            ["name"] => string(6)
            "HTML 5"
        }
    }[9] => array(2) {
        ["id"] => int(15)["skill"] => array(1) {
            ["name"] => string(6)
            "jQuery"
        }
    }[10] => array(2) {
        ["id"] => int(16)["skill"] => array(1) {
            ["name"] => string(5)
            "XHTML"
        }
    }[11] => array(2) {
        ["id"] => int(17)["skill"] => array(1) {
            ["name"] => string(3)
            "CSS"
        }
    }
}
}

编辑:这是foreach $ linkedInResults ['skills'] ['values']之后的var_dump:

// Just a sample as its quite long

array(13) {
  [0]=>
  array(2) {
    ["id"]=>
    int(1)
    ["skill"]=>
    array(1) {
      ["name"]=>
      string(21) "British Sign Language"
    }
  }
  [1]=>
  array(2) {
    ["id"]=>
    int(7)
    ["skill"]=>
    array(1) {
      ["name"]=>
      string(6) "Drupal"
    }
  }
  [2]=>
  array(2) {
    ["id"]=>
    int(8)
    ["skill"]=>
    array(1) {
      ["name"]=>
      string(9) "WordPress"
    }
  }
  [3]=>
  array(2) {
    ["id"]=>
    int(9)
    ["skill"]=>
    array(1) {
      ["name"]=>
      string(15) "iOS development"
    }
  }
  [4]=>
  array(2) {
    ["id"]=>
    int(10)
    ["skill"]=>
    array(1) {
      ["name"]=>
      string(7) "Magento"
    }
  }
  [5]=>
  array(2) {
    ["id"]=>
    int(11)
    ["skill"]=>
    array(1) {
      ["name"]=>
      string(10) "Web Design"
    }
  }

如何获取“name”的所有值并将它们添加到数组中?

1 个答案:

答案 0 :(得分:2)

您需要foreach通过数组。在您的示例中,唯一的数组是$linkedInResults['skills']['values']

然后从那里,你可以得到他们的每个名字。

$arr_skills = array();
foreach($linkedInResults['skills']['values'] as $addSkill){
    $arr_skills[] = $addSkill['skill']['name'];
}