如何从字符串创建多维数组

时间:2016-10-11 17:44:57

标签: php arrays multidimensional-array

希望从字符串创建多维数组。我的字符串是:

13,4,3 | 65,1,1 | 27,3,2

我想将它存储在一个数组中,我假设它看起来像这样:

$multi_array = array
  (
  array(13,4,3),
  array(65,1,1),
  array(27,3,2)
  );

所以我可以用$ multi_array [1] [1]来调用它,它应该返回" 4"。

这是我到目前为止的代码:

$string = "13,4,3|65,1,1|27,3,2";
$explode = explode("|", $string);
$multi_array = array(); //declare array

  $count = 0;

foreach ($explode as $value) {

  $explode2 = explode(",", $value);

  foreach ($explode2 as $value2) {
    // I'm stuck here....don't know what to do.
  }
  $count++;
}
echo '<pre>', print_r($multi_array), '</pre>';

3 个答案:

答案 0 :(得分:3)

你的外foreach循环是正确的。虽然explode返回一个数组,但您不需要内循环。只需将此数组附加到结果数组,您就可以获得2D数组

$input = "13,4,3|65,1,1|27,3,2";

$result = [];

foreach (explode('|', $input) as $split)
    $result[] = explode(',', $split);

print_r($result);

答案 1 :(得分:1)

试试这种方式,

$data = '13,4,3|65,1,1|27,3,2';

$return_2d_array = array_map (
  function ($_) {return explode (',', $_);},
  explode ('|', $data)
);

print '<pre>';
print_r ($return_2d_array);
print '</pre>';
使用您自己的代码

$string = "13,4,3|65,1,1|27,3,2";
$explode = explode("|", $string);
$multi_array = array(); //declare array

$count = 0;

foreach ($explode as $key=>$value) { // see changes on this line

  $explode2 = explode(",", $value);

  foreach ($explode2 as $value2) {
    $multi_array[$key][$count] = $value2;
    $count++; // see count variable position changes here
  }

}
echo '<pre>', print_r($multi_array), '</pre>';

答案 2 :(得分:0)

您可以使用爆炸功能将字符串拆分为分隔符,在本例中为source 'https://rubygems.org' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'tzinfo-data', platforms: [:mingw, :mswin, :jruby] # Use sqlite3 as the database for Active Record group :development, :test do gem 'sqlite3' end # Use SCSS for stylesheets gem 'sass-rails', '~> 5.0' # Use Uglifier as compressor for JavaScript assets gem 'uglifier', '>= 1.3.0' # Use CoffeeScript for .coffee assets and views gem 'coffee-rails', '~> 4.1.0' # See https://github.com/rails/execjs#readme for more supported runtimes # gem 'therubyracer', platforms: :ruby # Use jquery as the JavaScript library gem 'jquery-rails' # Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks gem 'turbolinks' # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder gem 'jbuilder', '~> 2.0' # bundle exec rake doc:rails generates the API under doc/api. gem 'sdoc', '~> 0.4.0', group: :doc gem 'bourbon' gem 'neat' gem 'meta-tags', '~> 2.0.0' # Use ActiveModel has_secure_password # gem 'bcrypt', '~> 3.1.7' # Use Unicorn as the app server # gem 'unicorn' # Use Capistrano for deployment # gem 'capistrano-rails', group: :development group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug' end group :development do # Access an IRB console on exception pages or by using <%= console %> in views gem 'web-console', '~> 2.0' # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring gem 'spring' end group :production do gem 'pg' gem 'rails_12factor' gem 'dragonfly-s3_data_store' end group :staging do gem 'rails_12factor' end gem 'refinerycms', git: 'https://github.com/refinery/refinerycms', branch: '3-0-stable' gem 'quiet_assets', group: :development # Add support for searching inside Refinery's admin interface. gem 'refinerycms-acts-as-indexed', ['~> 2.0', '>= 2.0.0'] # Add support for Refinery's custom fork of the visual editor WYMeditor. gem 'refinerycms-wymeditor', ['~> 1.0', '>= 1.0.6'] # The default authentication adapter gem 'refinerycms-authentication-devise', '~> 1.0' gem 'mailchimp-api', require: 'mailchimp' gem 'disqus_rails' gem 'figaro' gem 'refinerycms-blog', git: 'https://github.com/refinery/refinerycms-blog', branch: 'master' gem 'puma' gem 'refinerycms-disqus', '~> 0.0.1' gem 'refinerycms-productos', path: 'vendor/extensions' ,如下所示:

PHP:

C:\Sites\ifurniture>bundle install
Your Gemfile lists the gem rails_12factor (>= 0) more than once.
You should probably keep only one of them.
While it's not a problem now, it could cause errors if you change the version of
 just one of them later.
Fetching gem metadata from https://rubygems.org/
Fetching version metadata from https://rubygems.org/
Fetching dependency metadata from https://rubygems.org/
Could not find gem 'refinerycms-disqus (~> 0.0.1)' in any of the gem sources
listed in your Gemfile or available on this machine.

以下是explode:http://php.net/manual/en/function.explode.php

的文档

问候!