Skip to content
Advertisement

Parsing JSON array in PHP that doesn’t have a key value

I am receiving an array in php after making a request but the array doesn’t have a key value. Not sure if I need to parse or convert to an object first. Im using the simple_html_dom PHP library.

Here is the PHP script that grabs the table data from a website –

$table = array();

$html = file_get_html('www.mysite.com');
foreach($html->find('tr') as $row) {
    $zero = $row->find('td',0)->plaintext;
    $one = $row->find('td',1)->plaintext;

    $table[$zero][$one] = true;
}

The Array I get as a response is structured like so –

Array(

[Model] => Array
    (
        [PA-31-310] => 1
    )

[Year Manufacturer] => Array(
        [1975] => 1
    )
)

Ive tried parsing it like so but am not outputting any data –

foreach($table as $value){
    $model = $value['Model'];
}

I feel like the array within the array is whats causing me issues, do I need to convert the array in order to get “Model”?

Advertisement

Answer

Change out the

$table[$zero][$one] = true;

for

$table[$zero] = $one;

This will make a simple key value array.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement