Reply to Re: [PHP] Problem with arrays

Your name:

Reply:


Posted by Bob Winter on 06/24/05 18:49

Mike Johnson wrote:
> From: virtualsoftware@gmail.com [mailto:virtualsoftware@gmail.com]
>
>
>>Hi,
>>I have 2 arrays:
>>
>>Array (
>> [0] => Array (
>> [0] => 28
>> [1] => Music
>> )
>> [1] => Array (
>> [0] => 5
>> [1] => Books
>> )
>> )
>>
>>and
>>
>>Array (
>> [0] => aaa
>> [1] => bbb
>> )
>>
>>I want to join this two array and the result must be loke this:
>>
>>Array (
>> [0] => Array (
>> [0] => 28
>> [1] => Music
>> [2] => aaa
>> )
>> [1] => Array (
>> [0] => 5
>> [1] => Books
>> [2] => bbb
>> )
>> )
>>
>>Thanks in advance for your help
>
>
> In this specific example, I think this would work:
>
> <?
> for ($i = 0; $i < count($second_array); $i++) {
> array_push($first_array[$i], $second_array[$i]);
> }
> ?>
>
> That's not terribly flexible, though. Is this used in a more generalized
> sense, or is it just this specific instance?
>

This variation of Mike's solution will allow the array keys to be non-numeric and/or non-incrementing.

<?php

foreach($second_array as $key=>$value) {
if(array_key_exists($key, $first_array)) {
array_push($first_array[$key], $value);
} else {
print 'ERROR: Key '.$key.' does not exist in array $first_array.\n';
// or alternatively to add a new sub-array to $first_array
// $first_array[$key] = array($value);
}
}

?>

--Bob

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация