-5

Im not sure what I am doing wrong here. I have a txt file that stores and array:

 Array
      (
      [0] => Array
           (
                [sku] => 123123
           )
      )

When I go to read the file by php and loop over it, it gives me this error:

 Invalid argument supplied for foreach()

Code:

 $items = $file->getContents();
 foreach($items as $item){

 }

Alternate Code (serialize and unserialize):

 $items = $file->getContents();
 $itemsA = serialize($items);
 $itemsB = unserialize($itemsA);
 foreach($itemsB as $item){

 }

 Produces this error as well: Invalid argument supplied for foreach()
b.b3rn4rd
  • 8,494
  • 2
  • 45
  • 57
LargeTuna
  • 2,694
  • 7
  • 46
  • 92

4 Answers4

1

you can serialize it then unserialize or json_encode then json_decode

if your files are not dynamic (aka config files) check this answer

Community
  • 1
  • 1
Ahmed
  • 83
  • 2
  • 6
  • I think this brings up a great idea -- using JSON to store the data. It makes it a lot more transparent for debugging purposes and is also a growing standard for development. As far as I can think it through, the only drawback to using JSON over serialize/unserialize is if you're serializing custom classes, which I don't know the specifics for how JSON would unserialize back to their PHP originals. – Alec Deitloff Jan 29 '15 at 21:03
  • true the only drawback is json_encode messes up nested objects and objects properties – Ahmed Jan 29 '15 at 21:13
0

That is a var_dumped array. To store an array in a file you can serialize it and then write it and when you read it from the file you then unserialize it to get the array back.

inquam
  • 12,664
  • 15
  • 61
  • 101
0

Array ( [0] => Array ( [sku] => 123123 ) )

$items = $file->getContents();
$items = str_replace(array('> Array','[',']'),array('> new Array',"'","'"),$items );
file_put_content("TMPFILENAME","<\?PHP\n return new $items;");
$items = include "TMPFILENAME";

Replace " ' if needed If you need numeric key do some like first:

$items = preg_replace('#(?:\[([0-9]+)\])+#','$1',$items );

Still dirty! Convert var_dump of array back to array variable

\? to ? !!! WYSIWYG...

Community
  • 1
  • 1
s.d.a.p.e
  • 177
  • 3
0

Due to the lack of code, I will make a few assumptions.

These will be changed when the needed code is added.

There is no information about how that file was created.


The output looks like it came from either var_dump() or print_r().
I'm not entirely sure of which was used.
If these were used, forget the file.

If you want to recover this file, you have to manually convert it into actual working code (by adding quotes around the key names in the arrays), add return (with space after) to the file and include it.

To re-do the file, and for the next time you write it, you have several methods:

  1. Use the function serialize():
    This is easy to use!
    Simply pass the variables and you are set.
    Here's an example:

    $text=serialize($variable);

    Then you use unserialize() to extract the data.
    Like this:

    $data=unserialize($text);
  2. Use json_encode() to save the data.
    It works the same way:

    $text=json_encode($data);

    And use json_decode() to get the data back, like this:

    $data=json_decode($text);

    Notice that the JSON data can be used in many other languages, including Javascript!
    Also, this method is faster than serialize().

  3. Use var_export() to get valid PHP code in a string.
    Like this:

    $php=var_export($data);
    //write the file like this, prepending '<?php return ' to it:
    file_put_contents('file.php','<?php return '.$php);

    Which will write something like:

    return Array(1,2,3); //example code.

    To read the file, simply use include or require:

    $data=include 'file.php';

    Yes, include can receive the array data!
    This is, by far, the fastest method!

Ismael Miguel
  • 4,185
  • 1
  • 31
  • 42