将字符串数组转换回数组

Converting String Array Back to an Array

我正在创建一个 excel 文件,其中包含大量不同的数据点,这些数据来自我使用 Python 从网上抓取的信息。

其中一个数据点是一个嵌套数组,当它被插入 CSV 文件或从我服务器上的 PHP 文件中读取时,它会变成一个字符串。

使用嵌套数组的整个想法是,我可以将每对图像和缩略图插入单独行中各自的列中 MySQL table.

嵌套数组

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 处理 Excel 文件的脚本

$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]
        ]);
      }

问题是 $images_and_thumbnails 是一个字符串,对于 foreach 循环显然是 "an invalid argument"。

有什么方法可以将字符串转换回数组吗?

只需删除双引号就可以完成这项工作吗?

如果$images_and_thumbnails格式固定,可以用explode拆分:

$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);
}

不过如果可以变间距的话,还是用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);
}

如果您100% 确定数据是安全的,您也可以eval它,即

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);

任何这些方法都会给你相同的结果:

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

作为带有 PREG_SET_ORDER 标志的 preg_match_all() 的单次调用将设置一个多维数组,这将使隔离所需数据变得轻而易举。此外,如果您想对输入数据执行验证,您可以编写更严格的模式以确保您获得有效的 jpg 字符串。

如果这是我的任务,而且我无法控制输入数据的格式,那么我会这样解析它。一通电话搞定。

代码:(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";
    }
}

输出:

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',
)
---