Welcome Guest, Not a member yet? Register   Sign In
Problem With Sorting array
#1

I want result like
array([10]=>12,7
[4]=>7)

If Array Keys Are Same . then Values added one by one based on comma .
How .. Each and Every time Array keys are Generated Dynamically .. Help Me .. Thanks .See the Below Image I am Getting Array Leke This . Based On that Array I want Out PUT Like This ...
NOTE : Keys Are Not static ... It generated Dynamically
array([10]=>12,7
[4]=>7)

Attached Files Thumbnail(s)
   
Manikanta
Reply
#2

If you assign key/value pairs to an array, the comma is used to separate the pairs. The keynames must be strings. If you need numeric values with decimals, use a dot, not a comma.

E.g.:
$myArray = array(
'10'=>12.7,
'4'=>7
);

Will result in :
$myArray[10] => 12.7
$myArray[4] => 7
Reply
#3

(This post was last modified: 02-09-2016, 02:34 PM by siburny.)

(02-09-2016, 06:40 AM)Chandini Wrote: If Array Keys Are Same . then Values added one by one based on comma  .
It sounds like you mean to "merge" arrays, not to sort.

PHP Code:
$a1 = array('10' => 12);
$a2 = array('10' => 7'4' => 7);

$out $a1;
foreach(
$a2 as $key => $value)
{
    if(
array_key_exists($key$out))
    {
        
$out[$key] .= ",".$value;
    }
    else
    {
        
$out[$key] = $value;
    }
}

print_r($out); 
Reply
#4

(02-09-2016, 02:34 PM)siburny Wrote:
(02-09-2016, 06:40 AM)Chandini Wrote: If Array Keys Are Same . then Values added one by one based on comma  .
It sounds like you mean to "merge" arrays, not to sort.

PHP Code:
$a1 = array('10' => 12);
$a2 = array('10' => 7'4' => 7);

$out $a1;
foreach(
$a2 as $key => $value)
{
 if(
array_key_exists($key$out))
 {
 
$out[$key] .= ",".$value;
 }
 else
 {
 
$out[$key] = $value;
 }
}

print_r($out); 
Thanks For U r Answer ..
But each every time Arrays changed .. Some times I am Getting Only One Array . Some Times Two , Some Times Four .. These arrays Dynamically generated .. On that Time...?
Manikanta
Reply
#5

It still isn't very clear what you try to do and what you want to achieve. Please, share some code with us.
Generally speaking, an array can't contain multiple keys with the same name.
If you created an array like this (no matter if it is done dynamically or by hard coding):
PHP Code:
$myArray = array(
 
 10 => 12,
 
 4 => 7
); 
And you assign a value like:
PHP Code:
$myArray[10] = 7
The value of key 10 simply changes from 12 to 7.
Reply
#6

(02-09-2016, 11:16 PM)Wouter60 Wrote: It still isn't very clear what you try to do and what you want to achieve. Please, share some code with us.
Generally speaking, an array can't contain multiple keys with the same name.
If you created an array like this (no matter if it is done dynamically or by hard coding):
PHP Code:
$myArray = array(
 
 10 => 12,
 
 4 => 7
); 
And you assign a value like:
PHP Code:
$myArray[10] = 7
The value of key 10 simply changes from 12 to 7.

array([10]=>12,[4]=>7);
array([5]=>1,[10]=>9);
array([6]=>2,[4]=>15);
Thing is Very Clear .. In Above I am Getting 3 different arrays. In that arrays I am Getting Same Keys ... Now In this Condition I want To output Like This :
BAsed On above 3 arrays ...
array([10]=>12_9,[4]=>7_15,[5]=>1,[6]=>2);
Like This ..
Thanks For U r support ..
Manikanta
Reply
#7

PHP Code:
<?php

$array1 
= array(
 
   10=>12,
 
   4 =>7
    
);
$array2 = array(
 
   5=>1,
 
   10=>9
    
);
$array3 = array(
 
   6=>2,
 
   4=>15
    
);

$output = array();

foreach(
$array1 as $key => $value) {
 
   if (!array_key_exists($key$output)) {
 
       $output[$key] = $value;
 
   } else {
 
       $output[$key] .= '_' $value  ;
 
   }
}

foreach(
$array2 as $key => $value) {
 
   if (!array_key_exists($key$output)) {
 
       $output[$key] = $value;
 
   } else {
 
       $output[$key] .= '_' $value  ;
 
   }
}

foreach(
$array3 as $key => $value) {
 
   if (!array_key_exists($key$output)) {
 
       $output[$key] = $value;
 
   } else {
 
       $output[$key] .= '_' $value  ;
 
   }
}

var_dump($output);

array(
4) {
 
 [10]=>
 
 string(4"12_9"
 
 [4]=>
 
 string(4"7_15"
 
 [5]=>
 
 int(1)
 
 [6]=>
 
 int(2)

Reply
#8

(02-09-2016, 09:53 PM)Chandini Wrote: But each every time Arrays changed .. Some times I am Getting Only One Array . Some Times Two , Some Times Four .. These arrays Dynamically generated .. On that Time...?
So you don't the number of arrays in advance? How do you get them and how do you store them?
Reply




Theme © iAndrew 2016 - Forum software by © MyBB