我想在foreach循环中分配每组数组,以便在唯一变量中分配
我已经形成了一个数组
Array
(
[0] => Array
(
[title] => Mobiles & Accessories
)
[1] => Array
(
[title] => Mobile Accessories
)
[2] => Array
(
[title] => Cables
)
)
Array
(
[0] => Array
(
[title] => Computers
)
[1] => Array
(
[title] => TV & Video Accessories
)
[2] => Array
(
[title] => Cables
)
)
Array
(
[0] => Array
(
[title] => Home Entertainment
)
[1] => Array
(
[title] => Video Players & Accessories
)
[2] => Array
(
[title] => Video Accessories
)
[3] => Array
(
[title] => Cables
)
)
我希望每个数组都在唯一变量中
喜欢
$a = (
[0] => Array
(
[title] => Mobiles & Accessories
)
[1] => Array
(
[title] => Mobile Accessories
)
[2] => Array
(
[title] => Cables
)
)
$b = Array
(
[0] => Array
(
[title] => Computers
)
[1] => Array
(
[title] => TV & Video Accessories
)
[2] => Array
(
[title] => Cables
)
)
$c = Array
(
[0] => Array
(
[title] => Home Entertainment
)
[1] => Array
(
[title] => Video Players & Accessories
)
[2] => Array
(
[title] => Video Accessories
)
[3] => Array
(
[title] => Cables
)
)
等,任何帮助请
答案 0 :(得分:0)
您可能正在寻找“Variable variables”。
有时能够拥有变量变量名称很方便。 也就是说,一个可以动态设置和使用的变量名。
<?php
// your arrays
$arrI = [
['title' => 'Mobiles & Accessories'],
['title' => 'Mobile Accessories'],
['title' => 'Cables']
];
$arrII = [
['title' => 'Computers'],
['title' => 'TV & Video Accessories'],
['title' => 'Cables']
];
$arrIII = [
['title' => 'Home Entertainment'],
['title' => 'Video Players & Accessories'],
['title' => 'Video Accessories'],
['title' => 'Cables']
];
// You may access the arrays through the following names
$names = array('a', 'b', 'c');
// The arrays
$arrays = array($arrI, $arrII, $arrIII);
// Now, create variables named after the $names array elements,
// and associate each one with the corresponding array in the $arrays by the defined order.
for($i = 0; $i<count($names); $i++)
$$names[$i] = $arrays[$i];
// Now, you can access the arrays using their new names!
echo "<pre>";
print_r($a);
echo "<hr>";
print_r($b);
echo "<hr>";
print_r($c);
echo "</pre>";
?>