/    Sign up×
Community /Pin to ProfileBookmark

creating one array from two previous arrays without array_combine

Hi

I can not use array_combine to combine two arrays, (array combine kill one element if it have the same value).

array
0 => string ‘4443X-06148101’ (length=14)
1 => string ‘4440X-04590101’ (length=14)
array
0 => string ‘148’ (length=3)
1 => string ‘123’ (length=3)

i need a way to create another array from the two previous arrays that at the end have this result :

array
‘4443X-06148101’ => string ‘148’ (length=3)
‘4440X-04590101’ => string ‘123’ (length=3)

keys->values in the same order they are presented in the first array

How can it be done ?

Thank’ s in advance

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@aj_nscAug 26.2010 — [code=php]
$firstArr = array('4443X-06148101','4440X-04590101');
$secondArr = array('148','123');
$firstArrLength = count($firstArr);

$newArr = array();
for($i=0;i<$firstArrLength;$i++) {
$newArr[$firstArr[$i]] = $secondArr[$i];
}

print_r($newArr);
[/code]


Are both arrays always going to be the same length? If not, what do you want to happen when the one is longer than the other?

I still don't know why you can't use array_combine....what do you mean it will kill one element if it has the same value? Of course you cannot have two identical keys in an array, that kind of defeats the purpose of an array...
Copy linkTweet thisAlerts:
@amrigoauthorAug 26.2010 — Hi

I have a situation where i really can have one array with the same key and only the value cannot be the same for eg. :

array

0 => string '4443X-06148101' (length=14)

1 => string '4443X-06148101' (length=14)

array

0 => string '148' (length=3)

1 => string '123' (length=3

This would be acceptable and using array_combine i would have just one item in the array.
Copy linkTweet thisAlerts:
@aj_nscAug 26.2010 — You've got bad logic. An array is required to have unique keys. Think about your problem a little more and come up with a different solution. Basically what you have now is that what you want to have as keys, can be associated with more than one value....so why not format it like this:

Example, why not use a numerically indexed array like this:
<i>
</i>Array(
[4443X-06148101] =&gt; array('148','123')
)
Copy linkTweet thisAlerts:
@amrigoauthorAug 26.2010 — Agree!

In my case i have to find a substitute to a foreach that iterates over one array that have 8digits+3digits+3digits

This array comes from this previous adjustment of data :

foreach($results as $resultkey=>$resultvalue) {

if(substr($resultkey,-6,3)!= $resultvalue) {

$finalresult[] = substr($resultkey,0,8).$resultvalue.substr($resultkey,-3);

} else {

$finalresult[] = $resultkey;

}


}

So now here i have just the finalresult array :8digits+3digits+3digits and have to deal with the case where i can have the 8 digits being the same but the next 3 digits can not be the same
×

Success!

Help @amrigo spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...