array combine is not working in php? - php

my code is:
array set 1:
Array
(
[0] => 15-3
[1] => 16-3
[2] => 15-4
[3] => 16-4
[4] => 15-3
[5] => 16-3
[6] => 15-4
[7] => 16-4
[8] => 15-3
[9] => 16-3
[10] => 15-4
[11] => 16-4
)
My second array set is:
Array
(
[0] => 1
[1] => 1
[2] => 1
[3] => 1
[4] => 2
[5] => 2
[6] => 2
[7] => 2
[8] => 3
[9] => 3
[10] => 3
[11] => 3
)
i just combine the above both the two array into one like below
$data1=array_combine($store_attri_ids, $store_ids);
but its shows like that
Array
(
[15-3] => 3
[16-3] => 3
[15-4] => 3
[16-4] => 3
)
The remaining values are not combined, wt we do nw????

You can try this by creating a sub-array -
$data1 = array();
foreach($store_attri_ids as $key => $id) {
$data1[$id][] = $store_ids[$key];
}
The output would be like -
Array
(
[15-3] => array(1, 2, 3),
[16-3] => array(...),
[15-4] => array(...),
[16-4] => array(...)
)

If you use array_combine the result is totally correct, like putting here: http://php.net/manual/en/function.array-combine.php
You need use array_merge, take a look at the documentation :http://php.net/manual/en/function.array-merge.php

Use like this:
$data1=array_merge($store_attri_ids, $store_ids);
print_r($data1);

Related

PHP Replace Array with multiple Array by key

i have Main Array and i want to replace
Array
(
[0] => {title-1}
[1] => zebra
[2] => {title-1}
[3] => fruit
[4] => {title-2}
[5] => cars
[6] => {title-3}
[7] => city
[8] => {title-3}
[9] => amazing
[10] => gold
[11] => {title-2}
)
and then i have 3 array like that
Array
(
[0] => Blue
[2] => Red
)
Array
(
[6] => lamborghini
[8] => bugati
)
Array (
[4] => Yellow
[11] => dodge
)
i want to output like that
(
[0] => blue
[1] => zebra
[2] => red
[3] => fruit
[4] => yellow
[5] => cars
[6] => lamborghini
[7] => city
[8] => bugati
[9] => amazing
[10] => gold
[11] => dodge
)
i try used array_replace_recursive() but only work if one array
can any body help me
As you stated you have 3 array wich do contain values for your main array. There has nothing recoursive to be done.
You can simply use "array-replace":
$arrayMain = array(0 => "{title-1}", 1 => "zebra", 2 => "{title-1}");
$arrayReplace = array(0 => "Blue", 2 => "Red");
$finishedArray = array_replace($arrayMain, $arrayReplace);
This will give you:
array(
0 => "Blue",
1 => "zebra",
2 => "Red",
);
You can actually use this to replace multiple array_keys from multiple arrays like array_replace($mainArray, $replaceArray1, $replaceArray2); and so on.
Check the PHP Docs:
http://php.net/manual/en/function.array-replace.php
To use array_replace_recursive both array keys should be same. refer to below code.
$base = array('0' => '{title-1}', '1' => 'zebra','2'=>'{title-1}','3'=> 'fruit','4' => '{title-2}',5 => 'cars',6 => '{title-3}',7 => 'city');
$tem=array ( 0 => 'Blue', 2 => 'Red' ) ;
$tem1=array (4 => 'Yellow',6 => 'dodge');
$basket = array_replace_recursive($base, $tem);
$basket1 = array_replace($basket, $tem1);
print_r($basket1);
output:
Array
(
[0] => Blue
[1] => zebra
[2] => Red
[3] => fruit
[4] => Yellow
[5] => cars
[6] => dodge
[7] => city
)

how we remove index in the array and merge

Bellow is my Array in this array
****i want to remove second level index 1 and 2 and merge two arrays in one array****
$array1 = Array
(
[1] => Array
(
[1] => Array
(
[2] => 4
[3] => 2
[4] => 3
[5] =>
)
[2] => Array
(
[9] => 4
[10] =>
[11] => 4
[12] => 4
)
)
[2] => Array
(
[1] => Array
(
[2] => 3
[3] =>
[4] => 4.1428571428571
[5] => 5
)
[2] => Array
(
[9] => 3
[10] => 5
[11] => 5
[12] => 3
)
)
[3] => Array
(
[1] => Array
(
[2] => 5
[3] => 5
[4] =>
[5] => 4.1428571428571
)
[2] => Array
(
[9] => 3
[10] => 4.4285714285714
[11] => 4.4285714285714
[12] => 5
)
)
my aspected output is bellow where second level index is removed and arrays are merged
$array1= Array
(
[1] => Array
(
[2] => 4
[3] => 2
[4] => 3
[5] =>
[9] => 4
[10] =>
[11] => 4
[12] => 4
)
[2] => Array
(
[2] => 3
[3] =>
[4] => 4.1428571428571
[5] => 5
[9] => 3
[10] => 5
[11] => 5
[12] => 3
)
)
[3] => Array
(
[2] => 5
[3] => 5
[4] =>
[5] => 4.1428571428571
[9] => 3
[10] => 4.4285714285714
[11] => 4.4285714285714
[12] => 5
)
And i tried :
foreach($array1 as $arrkey => $arrvalue)
{
foreach($arrvalue as $innerkey => $innervalue){
//unset($cutvalu[$cutck]);
foreach($innervalue as $subkey => $subvalue){
#$array1[$arrkey][$subkey];
}
}
}
echo "<pre>"; print_r($array1);echo "<pre>";
Try this:
foreach($array1 as $arrkey => $arrvalue)
{
foreach($arrvalue as $innerkey => $innervalue){
foreach($innervalue as $subkey => $subvalue){
$new_array[$arrkey][$subkey] = $subvalue; // Change this line
}
}
}
echo "<pre>"; print_r($new_array);echo "<pre>";
This will give you what you're looking for.

Get sum of multi-dimensional array in PHP

I need to get the sum of all numeric values in my array for each designated month. Ideally, it would return the following format.
April
total = 22
March
total = 'sum'
Array
(
[April] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] =>
[4] => 3
[5] =>
[6] => 2
[7] => 6
[8] => 3
[9] => 2
)
[March] => Array
(
[0] => 3.19198
[1] => 2.52219
[2] => 3.40053
[3] => 2.42639
[4] => 3.92301
[5] => 3.23758
[6] => 3.22457
[7] => 2.62855
)
Apply array_sum() on each of the sub-arrays using array_map():
$result = array_map('array_sum', $data);
Output:
Array
(
[April] => 22
[March] => 24.5548
)
Demo

Explode String using PHP

How to explode this array value:
$row = array(
[0] = 1,1,Carlyle,Rogers,1,"Carlyle, Rogers",0000-00-00,,carlyle.rogers#stafford-trust.com,,non premium)
I tried this code
$values = explode(',', $row[0]);
and give me this wrong output:
Array (
[0] => 1
[1] => 1
[2] => Carlyle
[3] => Rogers
[4] => 1
[5] => "Carlyle
[6] => Rogers"
[7] => 0000-00-00
[8] =>
[9] => carlyle.rogers#stafford-trust.com
[10] =>
[11] => non premium
)
What I want is to Output must be like this one:
Array (
[0] => 1
[1] => 1
[2] => Carlyle
[3] => Rogers
[4] => 1
[5] => "Carlyle, Rogers"
[6] => 0000-00-00
[7] =>
[8] => carlyle.rogers#stafford-trust.com
[9] =>
[10] => non premium
)
You can't use explode like that because your input seems to be CSV-formatted and explode knows nothing about that "format". Use str_getcsv instead:
$values = str_getcsv($row[0]);

array manipulation

I have a two column array (array1), for each row of that array I need to compare the value in the 2nd column with a column value in each row of another array(array1) , when they equal I want to append another column value (from array2) to the first array.
in english:
if array1[x][1] = array2[y][0]
then array1[x][2] = array2[y][2]
screen dumps of both arrays
array1 (
[1] => Array (
[0] => 1 [1] => 2
)
[2] => Array (
[0] => 2 [1] => 3
)
[3] => Array (
[0] => 3 [1] =>
) [7] => Array (
[0] => 7 [1] => 1
)
[8] => Array (
[0] => 8 [1] => 1
)
[9] => Array (
[0] => 9 [1] => 10
)
[10] => Array (
[0] => 10 [1] => 2
)
)
array2 (
[0] => Array (
[0] => 1
[1] => 2
[2] => 2
[3] => Jane
[4] => Smith
[5] => jsmith#internet.com
[6] => jsmith
[7] => 12345
[8] => 1
[9] => no
)
[1] => Array (
[0] => 2
[1] => 2
[2] => 3
[3] => James
[4] => Beard
[5] => jasb#bellsouth.net
[6] => jbeard03
[7] => keeper
[8] => 1
[9] => no
)
[2] => Array (
[0] => 3
[1] => 2
[2] =>
[3] => Peter
[4] => Allen
[5] => pallen#rfgg.com
[6] => pallen
[7] => pallen
[8] => 1
[9] => no
)
[3] => Array (
[0] => 7
[1] => 2
[2] => 1
[3] => Joe
[4] => Blow
[5] => jasb#bellsouth.net
[6] => jblow
[7] => blow123
[8] => 5
[9] => yes
)
[4] => Array (
[0] => 8
[1] => 2
[2] => 1
[3] => John
[4] => Smith
[5] => logtest#bellsouth.net
[6] => jnsmith
[7] => jsmith123
[8] => 4
[9] => yes
)
[5] => Array (
[0] => 9
[1] => 2
[2] => 10
[3] => Frank
[4] => Smith
[5] => pallen#test.com
[6] => fsmith
[7] => fsmith123
[8] => 4
[9] => yes
)
[6] => Array (
[0] => 10
[1] => 2
[2] => 2
[3] => Loretta
[4] => Beard
[5] => lbeard#me.net
[6] => lbeard
[7] => lbeard123
[8] => 1
[9] => no
)
)
Does this work? I'm not sure I'm entirely clear on what you're looking for.
foreach($array1 as $x => $xarray) {
foreach($array2 as $y => $yarray) {
if($xarray[1] == $yarray[0]) {
$array1[$x][2] = $array[$y][2];
}
}
}
foreach ($array1 as &$a1) {
foreach ($array2 as $a2) {
if ($a1[1] == $a2[0]) {
$a1[] = $a2[2];
continue 2;
}
}
}
BTW, you should try to use explicit keys that actually mean something, e.g. $a1['id'] instead of $a1[1]. You'll thank yourself when you look at the code again two months down the road.
And because I threatened to do so:
btwyoushouldtrytouseexplicitkeysthatactuallymeansomethingeg$a1['id']insteadof$a1[1]youllthankyourselfwhenyoulookatthecodeagaintwomonthsdowntheroad ;-P

Categories