0

I'm creating an excel file with a bunch of different data points from information that I'm scraping off the web with Python.

One of those data points is a nested array, which is becoming a string when either it's inserted into the CSV file or read from the PHP file on my server.

The whole idea behind using the nested array is so that I can insert each pair of images and thumbnails into their respective columns in a single row on a separate MySQL table.

Nested Array

images_and_thumbnails = [
  ['https://images-na.ssl-images-amazon.com/images/I/615JCt72MXL._UY575_.jpg', 'https://images-na.ssl-images-amazon.com/images/I/41rExpVS75L._US40_.jpg'],
  ['https://images-na.ssl-images-amazon.com/images/I/71Ss5tJW-4L._UY575_.jpg', 'https://images-na.ssl-images-amazon.com/images/I/41RpAwvZJ5L._US40_.jpg'],
  ['https://images-na.ssl-images-amazon.com/images/I/6157znz2BeL._UY575_.jpg', 'https://images-na.ssl-images-amazon.com/images/I/41mSje9rDSL._US40_.jpg'],
  ['https://images-na.ssl-images-amazon.com/images/I/815wlLde-gL._UY575_.jpg', 'https://images-na.ssl-images-amazon.com/images/I/51jty5d4BpL._US40_.jpg'],
  ['https://images-na.ssl-images-amazon.com/images/I/71D2gVlCUOL._UY575_.jpg', 'https://images-na.ssl-images-amazon.com/images/I/41kCBJYI%2BCL._US40_.jpg'],
  ['https://images-na.ssl-images-amazon.com/images/I/71EfsMWdx0L._UY575_.jpg', 'https://images-na.ssl-images-amazon.com/images/I/41utl4%2B%2B%2BoL._US40_.jpg'],
  ['https://images-na.ssl-images-amazon.com/images/I/61m4mFpIvVL._UY575_.jpg', 'https://images-na.ssl-images-amazon.com/images/I/41S27BGn0UL._US40_.jpg']
]

PHP Script to Process the Excel File

$str2 = 'INSERT INTO deals_images_and_thumbnails (asin, image, thumbnail) VALUES (:asin, :image, :thumbnail)';
$sta2 = $conn->prepare($str2);

$file = fopen($_SESSION['file'], 'r');

while (!feof($file)) {
  while($row = fgetcsv($file)) {
    if (count($row) === 31) {

      $images_and_thumbnails = $row[8];

      foreach ($images_and_thumbnails as $value) {
        $sta2->execute([
          'asin' => $asin,
          'image' => $value[0],
          'thumbnail' => $value[1]
        ]);
      }

The issue is that $images_and_thumbnails is a string, which is obviously "an invalid argument" for the foreach loop.

Is there any way to convert the string back to an array?

Will simply removing the double quotes do the job?

2
  • instead of overriding images_and_thumbnails just create a new variable name for $images_and_thumbnails = $row[8]; Commented Oct 19, 2018 at 2:17
  • @Beginner what do u mean "instead of overwriting it"? where am i overwriting it? Commented Oct 19, 2018 at 2:34

2 Answers 2

2

If the format of $images_and_thumbnails is fixed, you could use explode to split it up:

$images_and_thumbnails = "[
  ['https://images-na.ssl-images-amazon.com/images/I/615JCt72MXL._UY575_.jpg', 'https://images-na.ssl-images-amazon.com/images/I/41rExpVS75L._US40_.jpg'],
  ['https://images-na.ssl-images-amazon.com/images/I/71Ss5tJW-4L._UY575_.jpg', 'https://images-na.ssl-images-amazon.com/images/I/41RpAwvZJ5L._US40_.jpg'],
  ['https://images-na.ssl-images-amazon.com/images/I/6157znz2BeL._UY575_.jpg', 'https://images-na.ssl-images-amazon.com/images/I/41mSje9rDSL._US40_.jpg'],
  ['https://images-na.ssl-images-amazon.com/images/I/815wlLde-gL._UY575_.jpg', 'https://images-na.ssl-images-amazon.com/images/I/51jty5d4BpL._US40_.jpg'],
  ['https://images-na.ssl-images-amazon.com/images/I/71D2gVlCUOL._UY575_.jpg', 'https://images-na.ssl-images-amazon.com/images/I/41kCBJYI%2BCL._US40_.jpg'],
  ['https://images-na.ssl-images-amazon.com/images/I/71EfsMWdx0L._UY575_.jpg', 'https://images-na.ssl-images-amazon.com/images/I/41utl4%2B%2B%2BoL._US40_.jpg'],
  ['https://images-na.ssl-images-amazon.com/images/I/61m4mFpIvVL._UY575_.jpg', 'https://images-na.ssl-images-amazon.com/images/I/41S27BGn0UL._US40_.jpg']
]";
foreach (explode('],', $images_and_thumbnails) as $i_and_t) {
    $value = explode("', '", trim($i_and_t, "[]' \t\r\n"));
    print_r($value);
}

However if it can be variable with spacing, it is better to use preg_split:

foreach (preg_split('/\'\s*\]\s*,\s*\[\s*\'/', $images_and_thumbnails) as $i_and_t) {
    $value = preg_split('/\'\s*,\s*\'/', trim($i_and_t, "[]' \t\r\n"));
    print_r($value);
}

If you're 100% certain that the data is safe, you could also eval it i.e.

eval ("\$images_and_thumbnails = [
  ['https://images-na.ssl-images-amazon.com/images/I/615JCt72MXL._UY575_.jpg', 'https://images-na.ssl-images-amazon.com/images/I/41rExpVS75L._US40_.jpg'],
  ['https://images-na.ssl-images-amazon.com/images/I/71Ss5tJW-4L._UY575_.jpg', 'https://images-na.ssl-images-amazon.com/images/I/41RpAwvZJ5L._US40_.jpg'],
  ['https://images-na.ssl-images-amazon.com/images/I/6157znz2BeL._UY575_.jpg', 'https://images-na.ssl-images-amazon.com/images/I/41mSje9rDSL._US40_.jpg'],
  ['https://images-na.ssl-images-amazon.com/images/I/815wlLde-gL._UY575_.jpg', 'https://images-na.ssl-images-amazon.com/images/I/51jty5d4BpL._US40_.jpg'],
  ['https://images-na.ssl-images-amazon.com/images/I/71D2gVlCUOL._UY575_.jpg', 'https://images-na.ssl-images-amazon.com/images/I/41kCBJYI%2BCL._US40_.jpg'],
  ['https://images-na.ssl-images-amazon.com/images/I/71EfsMWdx0L._UY575_.jpg', 'https://images-na.ssl-images-amazon.com/images/I/41utl4%2B%2B%2BoL._US40_.jpg'],
  ['https://images-na.ssl-images-amazon.com/images/I/61m4mFpIvVL._UY575_.jpg', 'https://images-na.ssl-images-amazon.com/images/I/41S27BGn0UL._US40_.jpg']
];");
print_r($images_and_thumbnails);

Any of those methods will give you the same result:

Array ( 
    [0] => https://images-na.ssl-images-amazon.com/images/I/615JCt72MXL._UY575_.jpg
    [1] => https://images-na.ssl-images-amazon.com/images/I/41rExpVS75L._US40_.jpg 
)
Array (
    [0] => https://images-na.ssl-images-amazon.com/images/I/71Ss5tJW-4L._UY575_.jpg
    [1] => https://images-na.ssl-images-amazon.com/images/I/41RpAwvZJ5L._US40_.jpg 
)
Array (
    [0] => https://images-na.ssl-images-amazon.com/images/I/6157znz2BeL._UY575_.jpg
    [1] => https://images-na.ssl-images-amazon.com/images/I/41mSje9rDSL._US40_.jpg 
)
Array (
    [0] => https://images-na.ssl-images-amazon.com/images/I/815wlLde-gL._UY575_.jpg
    [1] => https://images-na.ssl-images-amazon.com/images/I/51jty5d4BpL._US40_.jpg 
)
Array (
    [0] => https://images-na.ssl-images-amazon.com/images/I/71D2gVlCUOL._UY575_.jpg
    [1] => https://images-na.ssl-images-amazon.com/images/I/41kCBJYI%2BCL._US40_.jpg 
)
Array (
    [0] => https://images-na.ssl-images-amazon.com/images/I/71EfsMWdx0L._UY575_.jpg
    [1] => https://images-na.ssl-images-amazon.com/images/I/41utl4%2B%2B%2BoL._US40_.jpg
)
Array ( 
    [0] => https://images-na.ssl-images-amazon.com/images/I/61m4mFpIvVL._UY575_.jpg
    [1] => https://images-na.ssl-images-amazon.com/images/I/41S27BGn0UL._US40_.jpg 
)

Demo on 3v4l.org

Sign up to request clarification or add additional context in comments.

12 Comments

i just had basically the same idea while i was in the shower. there is no simpler way to do it other than exploding it?
You could eval it but that is a last resort; this is the easiest solution I can think of without eval. Leave the question open a while and you might get some better ideas.
thanks buddy appreciate it! if nothing else comes in, ill mark this as the sol
i think im prolly gon mark this as the answer. it's pretty simple. just an extra foreach loop with the explode. im super new to a lot of these languages and didnt realize u could trim more than whitespace. anyways ur solution works like a charm!
hey what is 3v4l.org exactly? can i test php scripts on there?
|
1

As single call of preg_match_all() with the PREG_SET_ORDER flag will set up a multidimensional array that will make isolating your desired data a snap. Furthermore, if you wanted to perform validation on the input data, you could write a more strict pattern to ensure you are getting valid jpg strings.

If this was my task and I had no control over the format of the input data, this is how I would parse it. One call does it all.

Code: (Demo) (Regex Demo)

$string = <<<STRING
images_and_thumbnails = [
  ['https://images-na.ssl-images-amazon.com/images/I/615JCt72MXL._UY575_.jpg', 'https://images-na.ssl-images-amazon.com/images/I/41rExpVS75L._US40_.jpg'],
  ['https://images-na.ssl-images-amazon.com/images/I/71Ss5tJW-4L._UY575_.jpg', 'https://images-na.ssl-images-amazon.com/images/I/41RpAwvZJ5L._US40_.jpg'],
  ['https://images-na.ssl-images-amazon.com/images/I/6157znz2BeL._UY575_.jpg', 'https://images-na.ssl-images-amazon.com/images/I/41mSje9rDSL._US40_.jpg'],
  ['https://images-na.ssl-images-amazon.com/images/I/815wlLde-gL._UY575_.jpg', 'https://images-na.ssl-images-amazon.com/images/I/51jty5d4BpL._US40_.jpg'],
  ['https://images-na.ssl-images-amazon.com/images/I/71D2gVlCUOL._UY575_.jpg', 'https://images-na.ssl-images-amazon.com/images/I/41kCBJYI%2BCL._US40_.jpg'],
  ['https://images-na.ssl-images-amazon.com/images/I/71EfsMWdx0L._UY575_.jpg', 'https://images-na.ssl-images-amazon.com/images/I/41utl4%2B%2B%2BoL._US40_.jpg'],
  ['https://images-na.ssl-images-amazon.com/images/I/61m4mFpIvVL._UY575_.jpg', 'https://images-na.ssl-images-amazon.com/images/I/41S27BGn0UL._US40_.jpg']
]
STRING;

if (preg_match_all("~\s*\['([^']*)',\s*'([^']*)']~", $string, $out, PREG_SET_ORDER)) {
    foreach ($out as $row) {
        var_export($row);     // to demonstrate what is generated
        $image = $row[1];     // for your actual usage
        $thumbnail = $row[2]; // for your actual usage
        echo "\n---\n";
    }
}

Output:

array (
  0 => '  [\'https://images-na.ssl-images-amazon.com/images/I/615JCt72MXL._UY575_.jpg\', \'https://images-na.ssl-images-amazon.com/images/I/41rExpVS75L._US40_.jpg\']',
  1 => 'https://images-na.ssl-images-amazon.com/images/I/615JCt72MXL._UY575_.jpg',
  2 => 'https://images-na.ssl-images-amazon.com/images/I/41rExpVS75L._US40_.jpg',
)
---
array (
  0 => '  [\'https://images-na.ssl-images-amazon.com/images/I/71Ss5tJW-4L._UY575_.jpg\', \'https://images-na.ssl-images-amazon.com/images/I/41RpAwvZJ5L._US40_.jpg\']',
  1 => 'https://images-na.ssl-images-amazon.com/images/I/71Ss5tJW-4L._UY575_.jpg',
  2 => 'https://images-na.ssl-images-amazon.com/images/I/41RpAwvZJ5L._US40_.jpg',
)
---
array (
  0 => '  [\'https://images-na.ssl-images-amazon.com/images/I/6157znz2BeL._UY575_.jpg\', \'https://images-na.ssl-images-amazon.com/images/I/41mSje9rDSL._US40_.jpg\']',
  1 => 'https://images-na.ssl-images-amazon.com/images/I/6157znz2BeL._UY575_.jpg',
  2 => 'https://images-na.ssl-images-amazon.com/images/I/41mSje9rDSL._US40_.jpg',
)
---
array (
  0 => '  [\'https://images-na.ssl-images-amazon.com/images/I/815wlLde-gL._UY575_.jpg\', \'https://images-na.ssl-images-amazon.com/images/I/51jty5d4BpL._US40_.jpg\']',
  1 => 'https://images-na.ssl-images-amazon.com/images/I/815wlLde-gL._UY575_.jpg',
  2 => 'https://images-na.ssl-images-amazon.com/images/I/51jty5d4BpL._US40_.jpg',
)
---
array (
  0 => '  [\'https://images-na.ssl-images-amazon.com/images/I/71D2gVlCUOL._UY575_.jpg\', \'https://images-na.ssl-images-amazon.com/images/I/41kCBJYI%2BCL._US40_.jpg\']',
  1 => 'https://images-na.ssl-images-amazon.com/images/I/71D2gVlCUOL._UY575_.jpg',
  2 => 'https://images-na.ssl-images-amazon.com/images/I/41kCBJYI%2BCL._US40_.jpg',
)
---
array (
  0 => '  [\'https://images-na.ssl-images-amazon.com/images/I/71EfsMWdx0L._UY575_.jpg\', \'https://images-na.ssl-images-amazon.com/images/I/41utl4%2B%2B%2BoL._US40_.jpg\']',
  1 => 'https://images-na.ssl-images-amazon.com/images/I/71EfsMWdx0L._UY575_.jpg',
  2 => 'https://images-na.ssl-images-amazon.com/images/I/41utl4%2B%2B%2BoL._US40_.jpg',
)
---
array (
  0 => '  [\'https://images-na.ssl-images-amazon.com/images/I/61m4mFpIvVL._UY575_.jpg\', \'https://images-na.ssl-images-amazon.com/images/I/41S27BGn0UL._US40_.jpg\']',
  1 => 'https://images-na.ssl-images-amazon.com/images/I/61m4mFpIvVL._UY575_.jpg',
  2 => 'https://images-na.ssl-images-amazon.com/images/I/41S27BGn0UL._US40_.jpg',
)
---

3 Comments

@Anthony Too complicated? Need me to explain anything further?
@Anthony I got the impression from your comment that you were seeking a cleaner, more direct solution.
exploding it twice was sufficient, but very useful answer nonetheless. if i run into issues, ill def try your solution!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.