将字符串数组转换回数组

时间:2018-10-19 01:58:57

标签: php arrays string parsing extraction

我正在使用从Python抓取网络的信息中创建的Excel文件,其中包含许多不同的数据点。

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

使用嵌套数组的整个想法是,我可以将每对图像和缩略图插入到单独MySQL表的一行中的相应列中。

嵌套数组

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']
]

用于处理Excel文件的PHP脚本

$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循环的“无效参数”。

是否可以将字符串转换回数组?

会简单地删除双引号吗?

2 个答案:

答案 0 :(得分:2)

如果-W的格式是固定的,则可以使用explode对其进行拆分:

p_last_name

但是,如果可以随间距变化,则最好使用preg_split

$images_and_thumbnails

如果您 100%确定该数据是安全的,则也可以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']
]";
foreach (explode('],', $images_and_thumbnails) as $i_and_t) {
    $value = explode("', '", trim($i_and_t, "[]' \t\r\n"));
    print_r($value);
}

这些方法中的任何一种都会给您相同的结果:

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

Demo on 3v4l.org

答案 1 :(得分:1)

作为带有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',
)
---