Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Liam Maclachlan
Liam Maclachlan
22,805 Points

PHP thinks array is a string. How do I make PHP believe me!?

I have created an array which pulls in data from a table. using the below code. When I check where $args is a string, php says "yes it is!"... but when I try to parse it in to the array and ammend it... computer says no -_-... any ideas?

<?php 
        //new array is created. Don't worry about this bit
        if ( isset($_POST['publisher-name']) && $_POST['publisher-name'] != '' ) {

            $publisherTitle = esc_html($_POST['publisher-name']);

            while ( isset($_POST['category-input-' . $categoryNumber]) && $_POST['category-input-' . $categoryNumber] ) {

                $fullCategoryList["category-name-".$categoryNumber] = esc_html( $_POST['category-input-' . $categoryNumber] );

                $categoryNumber++;
            }
            var_dump($fullCategoryList); // successfully dumps array

        } else {
            echo "You must have a publisher name";
        }

        // new array is compiled to add to the array just created
        $arr = array(
                'hello' => array('array'),
                'chick' => array('peas')
            );

        if (is_array ( $arr )) {
            echo "yes it is!"; // this proves true and echoes
        } else {
            echo "There is your problem!";
        }

        if (is_array ( $fullCategoryList )) {
            echo "it also is!"; // this also proves true and echoes
        } else {
            echo "There is your problem!";
        }

        array_push($fullCategoryList['category-name-1'][], $arr); //calls a fatal error sayin gthat I'm trying to pass a string....
        // Fatal error: [] operator not supported for strings in  C:/......
        var_dump($fullCategoryList); //doesn't even get called

?>
Liam Maclachlan
Liam Maclachlan
22,805 Points

Okay... I found a problem.

After runnign this piece of code:

<?php 

        if (is_array ( $fullCategoryList["category-name-1"] )) {
            echo "it also is!"; // this proves true and echoes
        } else {
            echo "There is your problem!";
        }

?>

It turns out it is not an array. I know this, as it is a value in the

1 Answer

Liam Maclachlan
Liam Maclachlan
22,805 Points

SOLVED: Helps is you don't try to add the array to a string... Dumb moment. All sorted :)

Added an array to the end of the array and was able to pass my variables in to that one. Life is good again (Y)

<?php

        if ( isset($_POST['publisher-name']) && $_POST['publisher-name'] != '' ) {

            $publisherTitle = esc_html($_POST['publisher-name']);

            while ( isset($_POST['category-input-' . $categoryNumber]) && $_POST['category-input-' . $categoryNumber] ) {

                $fullCategoryList["category-name-".$categoryNumber] = esc_html( $_POST['category-input-' . $categoryNumber] );

                $categoryNumber++;
                // adds an array to the end of the array items so it can be edited
                if ( !isset($_POST['category-input-' . $categoryNumber] ) ) {
                    $fullCategoryList["category-name-". ($categoryNumber + 1)] = array();
                }
            var_dump($fullCategoryList);
            }

        } else {
            echo "You must have a publisher name";
        }

        // new array is compiled to add to the array just created
        $arr = array(
                'hello' => array('array'),
                'chick' => array('peas')
            );

        if (is_array ( $arr )) {
            echo "yes it is!"; // this proves true and echoes
        } else {
            echo "There is your problem!";
        }

                if (is_array ( $fullCategoryList["category-name-1"] )) {
            echo "it also is!"; // this proves true and echoes
        } else {
            echo "There is your problem!";
        }

        array_push($fullCategoryList["category-name-5"], $arr); //calls a fatal error sayin that I'm trying to pass a string....
        var_dump($fullCategoryList); // Now even gets called

?>
Liam Maclachlan
Liam Maclachlan
22,805 Points

With the erro in mind, this is the edited code

<?php 

        if ( isset($_POST['publisher-name']) && $_POST['publisher-name'] != '' ) {

            $publisherTitle = esc_html($_POST['publisher-name']);

            while ( isset($_POST['category-input-' . $categoryNumber]) && $_POST['category-input-' . $categoryNumber] ) {

                $fullCategoryList["category-name-".$categoryNumber] = esc_html( $_POST['category-input-' . $categoryNumber] );

                $categoryNumber++;
            }

        } else {
            echo "You must have a publisher name";
        }

        // new array is compiled to add to the array just created
        $arr = array(
                'hello' => array('array'),
                'chick' => array('peas')
            );

        array_push($fullCategoryList, $arr); // It really should have been added to the array...
        // array_push($fullCategoryList[], $arr); didn't exsist as an array, or any itteration of that
        var_dump($fullCategoryList);

?>