2D Array不存储和显示数据PHP

时间:2019-11-15 06:14:50

标签: php arrays multidimensional-array

我正在创建一个数组 Elements ,其每个索引包含另一个数组 Item ,但是此item数组未存储到Elements中,因此不会显示。 请帮忙。

$item = array();
$elements[] = array();

foreach($html->find('div.jobsearch-SerpJobCard') as $element)
{ 
    $item['title']     = $element->find('div.title', 0)->plaintext;
    $item['salaryText']     = $element->find('span.salaryText', 0);
    $item['summary']     = $element->find('div.summary', 0)->plaintext;
    $elements[]= $item;
}

foreach($elements as $k=>$v)
{
    array_splice($v, 1,1);
    $array[$k] = $v;
    print_r($array[$k]);
}

2 个答案:

答案 0 :(得分:0)

也许您可以尝试数组推送或放置索引

https://www.php.net/manual/en/function.array-push.php

<?php
    //Enter your code here, enjoy!

$jobsearch = array(
    0 => array(
        "title" => "ceo",
        "salary" => 5000,
        "summary" => "high"
        ),
     1 => array(
        "title" => "cto",
        "salary" => 4000,
        "summary" => "low"
        ),
     2 => array(
        "title" => "cio",
        "salary" => 4500,
        "summary" => "medium"
        )
); // example data

$item=array();
$elements[]=array();
foreach($jobsearch as $k => $element)
{
 $item['Ititle'] = $element['title'];
 $item['Isalary']  = $element['salary'];
 $item['Isummary'] = $element['summary'];
 $elements[$k] = $item;


}
var_dump($elements); // got output

答案 1 :(得分:0)

从$ elements [] = array();中删除[];

它正在为$ elements数组在0索引处创建一个空数组。