Php 附加到关联数组?
我有一个源数组Php 附加到关联数组?,php,multidimensional-array,Php,Multidimensional Array,我有一个源数组 $A = array( 0=> array( 'title'=>'HTML5+CSS3', 'teacher'=>'jonh', 'id_post'=>1,/* link to $post_formation */ 'formation'=>1, 'date'=>'12/12/2112' ) ); 和另一个数组: $post_formation
$A = array(
0=> array(
'title'=>'HTML5+CSS3',
'teacher'=>'jonh',
'id_post'=>1,/* link to $post_formation */
'formation'=>1,
'date'=>'12/12/2112'
)
);
和另一个数组:
$post_formation = array(
0=>array(1,2,3),
1=>array(3,4,5)
)
查看$A
以操作新的result(在本例中id\u post=1
1=>数组(3,4,5)
因此将包含更多3个元素
)
有人能告诉我怎么做吗?虽然有点奇怪
$A=array(array("title"=>"HTML5+CSS3","teacher"=>"john","id_post"=>1,"formation"=>1,"date"=>"12/12/2012"));
$post_information=array(0=>array(1,2,3),1=>array(3,4,5));
print_r($A);
if(isset($post_information[$A[0]["id_post"]]))
{
foreach($post_information[$A[0]["id_post"]] as $idx)
{
$cache=$A[0];
$cache["title"]="(A)".$cache["title"];
$cache["formation"]=$idx;
$cache["date"]="--/--/----";
$A[]=$cache;
}
}
print_r($A);
结果:
第一个打印\r
:
Array
(
[0] => Array
(
[title] => HTML+CSS3
[teacher] => john
[id_post] => 1
[formation] => 1
[date] => 12/12/2012
)
)
Array
(
[0] => Array
(
[title] => HTML+CSS3
[teacher] => john
[id_post] => 1
[formation] => 1
[date] => 12/12/2012
)
[1] => Array
(
[title] => (A)HTML+CSS3
[teacher] => john
[id_post] => 1
[formation] => 3
[date] => --/--/----
)
[2] => Array
(
[title] => (A)HTML+CSS3
[teacher] => john
[id_post] => 1
[formation] => 4
[date] => --/--/----
)
[3] => Array
(
[title] => (A)HTML+CSS3
[teacher] => john
[id_post] => 1
[formation] => 5
[date] => --/--/----
)
)
第二个打印\u r
:
Array
(
[0] => Array
(
[title] => HTML+CSS3
[teacher] => john
[id_post] => 1
[formation] => 1
[date] => 12/12/2012
)
)
Array
(
[0] => Array
(
[title] => HTML+CSS3
[teacher] => john
[id_post] => 1
[formation] => 1
[date] => 12/12/2012
)
[1] => Array
(
[title] => (A)HTML+CSS3
[teacher] => john
[id_post] => 1
[formation] => 3
[date] => --/--/----
)
[2] => Array
(
[title] => (A)HTML+CSS3
[teacher] => john
[id_post] => 1
[formation] => 4
[date] => --/--/----
)
[3] => Array
(
[title] => (A)HTML+CSS3
[teacher] => john
[id_post] => 1
[formation] => 5
[date] => --/--/----
)
)
当然,您可以将逻辑封装在函数中,而不是简单地修改源代码
$a
,尽管有点奇怪
$A=array(array("title"=>"HTML5+CSS3","teacher"=>"john","id_post"=>1,"formation"=>1,"date"=>"12/12/2012"));
$post_information=array(0=>array(1,2,3),1=>array(3,4,5));
print_r($A);
if(isset($post_information[$A[0]["id_post"]]))
{
foreach($post_information[$A[0]["id_post"]] as $idx)
{
$cache=$A[0];
$cache["title"]="(A)".$cache["title"];
$cache["formation"]=$idx;
$cache["date"]="--/--/----";
$A[]=$cache;
}
}
print_r($A);
结果:
第一个打印\r
:
Array
(
[0] => Array
(
[title] => HTML+CSS3
[teacher] => john
[id_post] => 1
[formation] => 1
[date] => 12/12/2012
)
)
Array
(
[0] => Array
(
[title] => HTML+CSS3
[teacher] => john
[id_post] => 1
[formation] => 1
[date] => 12/12/2012
)
[1] => Array
(
[title] => (A)HTML+CSS3
[teacher] => john
[id_post] => 1
[formation] => 3
[date] => --/--/----
)
[2] => Array
(
[title] => (A)HTML+CSS3
[teacher] => john
[id_post] => 1
[formation] => 4
[date] => --/--/----
)
[3] => Array
(
[title] => (A)HTML+CSS3
[teacher] => john
[id_post] => 1
[formation] => 5
[date] => --/--/----
)
)
第二个打印\u r
:
Array
(
[0] => Array
(
[title] => HTML+CSS3
[teacher] => john
[id_post] => 1
[formation] => 1
[date] => 12/12/2012
)
)
Array
(
[0] => Array
(
[title] => HTML+CSS3
[teacher] => john
[id_post] => 1
[formation] => 1
[date] => 12/12/2012
)
[1] => Array
(
[title] => (A)HTML+CSS3
[teacher] => john
[id_post] => 1
[formation] => 3
[date] => --/--/----
)
[2] => Array
(
[title] => (A)HTML+CSS3
[teacher] => john
[id_post] => 1
[formation] => 4
[date] => --/--/----
)
[3] => Array
(
[title] => (A)HTML+CSS3
[teacher] => john
[id_post] => 1
[formation] => 5
[date] => --/--/----
)
)
当然,您可以将逻辑包装到函数中,而不是以另一种方式简单地修改源代码
$a
$ArrayPayChannel=array();
function array_push_assoc($array, $key, $value){
$array[$key] = $value;
return $array;
}
$ArrayPayChannel = array_push_assoc($ArrayPayChannel, 'Key', 'Value');
如果你在数组中有更多的深度,而不是值,你可以放入另一个数组(ArrayPayChannel,'Key,'value');而且你有更多你想要的深度另一种方式,可能更简单
$ArrayPayChannel=array();
function array_push_assoc($array, $key, $value){
$array[$key] = $value;
return $array;
}
$ArrayPayChannel = array_push_assoc($ArrayPayChannel, 'Key', 'Value');
如果你在数组中有更多的深度,而不是值,你可以放入另一个数组(ArrayPayChannel,'Key,'value');而且你有更多你想要的深度我不太明白其中的逻辑。你尝试了什么,你到底被困在哪里了?我不太明白其中的逻辑。你尝试了什么,你到底被困在哪里?如果数组
A
可以应用更多的元素吗?@BandOfBrothers这取决于源在“更多元素”中的样子,以及你期望的结果是什么。用一个“更多元素”的例子更新你的问题(以及预期的结果),我会看看我是否能做到。如果数组a
可以应用更多元素?@BandOfBrothers,这取决于源在“更多元素”中的外观以及你预期的结果。用一个“更多元素”的例子(和预期结果)更新你的问题,我会看看我是否能做到。