Log in

View Full Version : Extract Values From PHP Array



hemi519
08-22-2012, 12:20 PM
Hi All,

Anyone can help me to get the values from the following array




Array ( [0] => 2644 [1] => 10 [2] => 2644 [3] => 50 [4] => 2644 [5] => 10 [6] => 5172 [7] => 70 [8] => 6546 [9] => 100 )




In the above array first one customer-id(2644) and his amount is (10). Now can i extract the amount of all three customer-id's (2644) and sum their amount. I am trying to do it dynamically. Is this possible? Can anyone suggest me how to do this?

traq
08-22-2012, 02:22 PM
so, odd values are amounts? put the array through a loop:
<?php
$array = Array( 2644,10,2644,50,2644,10,5172,70,6546,100 );
foreach( $array as $key=>$value ){
// use the % (modulus (remainder after division)) to determine if the key is odd or even
if( $key%2 === 0 ){
// it's even (or 0) --a customer number
$cust_no[] = $value;
}else{
// it's odd --an amount
$amount[] = $value;
}
}
// now you have two arrays: one with customer numbers, one with amounts.
// want to combine them? (first arg will be keys; second arg will be values)
$assoc = array_combine( $cust_no,$amount );

// view results:
foreach( $assoc as $k=>$v ){
print "customer #$k has amount $v<br>";
}
print "Sum of all amounts: ".array_sum( $assoc );

hemi519
08-22-2012, 03:10 PM
so, odd values are amounts? put the array through a loop:
<?php
$array = Array( 2644,10,2644,50,2644,10,5172,70,6546,100 );
foreach( $array as $key=>$value ){
// use the % (modulus (remainder after division)) to determine if the key is odd or even
if( $key%2 === 0 ){
// it's even (or 0) --a customer number
$cust_no[] = $value;
}else{
// it's odd --an amount
$amount[] = $value;
}
}
// now you have two arrays: one with customer numbers, one with amounts.
// want to combine them? (first arg will be keys; second arg will be values)
$assoc = array_combine( $cust_no,$amount );

// view results:
foreach( $assoc as $k=>$v ){
print "customer #$k has amount $v<br>";
}
print "Sum of all amounts: ".array_sum( $assoc );



Thanks for your time and suggestion. In the above code i am having one doubt,

Now For Customer-Id (2644) there are three amounts which should give a total amount of 70, but according to your code the value for 2644 is 10. How can i make it to 70

jscheuer1
08-22-2012, 04:03 PM
<?php
$array = Array( 2644,10,2644,50,2644,10,5172,70,6546,100 );

foreach( $array as $key => $value ){
// use the % (modulus (remainder after division)) to determine if the key is odd or even
if( $key%2 === 0 ){
// it's even (or 0) --a customer number
$cust_no[$value] = isset($cust_no[$value])? $cust_no[$value] + $array[$key + 1] : $array[$key + 1];
}
}

// view results:
foreach( $cust_no as $k => $v ){
print "customer #$k has amount $v<br>";
$assoc[] = $v;
}
print "Sum of all amounts: " . array_sum( $assoc );
?>

hemi519
08-22-2012, 04:35 PM
Thanks for both of you, it is working perfectly. But one last help

I am getting array in this format



Array ( [0] => 2644 [1] => 10 [2] => 2644 [3] => 50 [4] => 2644 [5] => 10 [6] => 5172 [7] => 70 [8] => 6546 [9] => 100 )


how to convert that according to array taken by you in the above example



Array ( 2644 ,10,2644,50,2644,10,5172,70,6546,100 )

jscheuer1
08-22-2012, 04:50 PM
Aside from a typo you made in copying our version, they are equivalent. Doing:


$array = Array( 2644,10,2644,50,2644,10,5172,70,6546,100 );
print_r($array);

will get you:


Array ( [0] => 2644 [1] => 10 [2] => 2644 [3] => 50 [4] => 2644 [5] => 10 [6] => 5172 [7] => 70 [8] => 6546 [9] => 100 )

You cannot do this though:


$array = Array ( [0] => 2644 [1] => 10 [2] => 2644 [3] => 50 [4] => 2644 [5] => 10 [6] => 5172 [7] => 70 [8] => 6546 [9] => 100 );

It's invalid PHP.

You can do this:


$array = Array ( 0 => 2644, 1 => 10, 2 => 2644, 3 => 50, 4 => 2644, 5 => 10, 6 => 5172, 7 => 70, 8 => 6546, 9 => 100 );

and that's equivalent.

If you're still having problems, where are you getting your array from? Show us the code that produces it.

hemi519
08-22-2012, 05:12 PM
In my coding there are two while loops which will produce customer-id and their amount



$array []= $customerid;
$array []= $amount;

// following is outside of two while loops
showresult($array); // calling a function

function showresult($array) // code given by you both
{

print_r($array) // this will give $array = Array ( 0 => 2644, 1 => 10, 2 => 2644, 3 => 50, 4 => 2644, 5 => 10, 6 => 5172, 7 => 70, 8 => 6546, 9 => 100 );
// the above array will produce incorrect results

$array = Array( 2644,10,2644,50,2644,10,5172,70,6546,100 ); // array taken by you
// the above array will produce correct results

// So i want my array to be taken as Array( 2644,10,2644,50,2644,10,5172,70,6546,100 );

foreach( $array as $key => $value ){
// use the % (modulus (remainder after division)) to determine if the key is odd or even
if( $key%2 === 0 ){
// it's even (or 0) --a customer number
$cust_no[$value] = isset($cust_no[$value])? $cust_no[$value] + $array[$key + 1] : $array[$key + 1];
}
}

// view results:
foreach( $cust_no as $k => $v ){
print "customer #$k has amount $v<br>";
$assoc[] = $v;
}
print "Sum of all amounts: " . array_sum( $assoc );
}

jscheuer1
08-22-2012, 05:43 PM
print_r($array) // this will give $array = Array ( 0 => 2644, 1 => 10, 2 => 2644, 3 => 50, 4 => 2644, 5 => 10, 6 => 5172, 7 => 70, 8 => 6546, 9 => 100 );
// the above array will produce incorrect results


print_r($array)

will never give that result. For one thing it has no ; terminus, for another print_r doesn't output in the format you indicate in the comment.

I don't see any while loops.

What you have here suggests one:



$array []= $customerid;
$array []= $amount;

And could if it were a while loop output an array like, when print_r is used on it:


Array ( [0] => 2644 [1] => 10 [2] => 2644 [3] => 50 [4] => 2644 [5] => 10 [6] => 5172 [7] => 70 [8] => 6546 [9] => 100 )

And that would work with the function showresult you have there once you clean it up (add the missing terminus and remove the line declaring a new array for $array).

hemi519
08-22-2012, 05:45 PM
I am sry, i was having issue in my coding, everything is working fine, onceagain thanks

djr33
08-23-2012, 02:54 AM
Just in case anyone is wondering, it is possible to convert between the print_r() output format and the array() format. It is relatively simple and works using str_replace(). (It's easier if you're using just numerical data or something else that doesn't involve the comma or quotes symbols in the array data, but possible even if you're doing that, probably using regex.)

Or, what I have done sometimes is to create my own equivalent function to print_r() that just immediately prints it in the array() format and then you can save that into a .php file and read it directly. (Of course be very careful about what could get into that .php file and don't run it on your server if you're not certain the code is safe!)