I have the array about:
Array
(
[id] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[product] => Array
(
[0] => t-shirt
[1] => earing
[2] => clock
)
[price] => Array
(
[0] => 100.00
[1] => 32.00
[2] => 898.00
)
)
I want to do this:
Array
(
[0] => Array
(
[0] => 1
[1] => t-shirt
[2] => 100.00
)
[1] => Array
(
[0] => 2
[1] => earing
[2] => 32.00
)
[2] => Array
(
[0] => 3
[1] => clock
[2] => 898.00
)
)
You can try with:
$input = array( /* your input array */ );
$output = array();
foreach ($input as $data) {
for ($i = 0; $i < count($data); $i++) {
if (!isset($output[$i])) {
$output[$i] = array();
}
$output[$i][] = $data[$i];
}
}
Well or like this.
$test = array(
'id' => array(
1,2,3
),
'product' => array(
'tshirt', 'ewew', 'shorts'
),
'price' => array(
'10.00', '20.00', '30.00'
)
);
$newarray = array();
foreach($test['id'] as $k => $v){
$newarray[$k] = array(
$v, $test['product'][$k], $test['price'][$k]
);
}
echo '<pre>';
print_r($newarray);
Example live
Related
I have 2 Array:
$arr1 = Array (
[2] => Array ( [0] => 41000 [1] => 31079 )
[3] => Array ( [0] => 42963 [1] => 41189 )
)
$arr2 = Array (
[2] => Array ( [0] => 40213 [1] => 42054 )
[3] => Array ( [0] => 42998 [1] => 34567 )
)
I want to combine these two arrays to this array with same key:
$arr3 = Array (
[2] => Array ( [0] => 40213 [1] => 42054 [2] => 41000 [3] => 31079 )
[3] => Array ( [0] => 42998 [1] => 34567 [2] => 42963 [3] => 41189 )
)
I tried almost anything (merge, combine, join) but I can't figure it out. Any suggestions?
This is a nice and easy thing to do.
$arr1 = [2 => [0 => 41000, 1 => 31079], 3 => [0 => 42963, 1 => 41189]];
$arr2 = [2 => [0 => 40213, 1 => 42054], 3 => [0 => 42998, 1 => 34567]];
function addArray(&$bucket, $water)
{
foreach ($water as $key => $drop) {
$bucket[$key] = array_merge($bucket[$key] ?? [], $drop);
}
}
$combined = [];
addArray($combined, $arr1);
addArray($combined, $arr2);
var_export($combined);
See a PHP fiddle here.
The output is:
array (
2 =>
array (
0 => 41000,
1 => 31079,
2 => 40213,
3 => 42054,
),
3 =>
array (
0 => 42963,
1 => 41189,
2 => 42998,
3 => 34567,
),
)
How does it work? I wrote a little function that would add the input arrays to an array that combines all arrays.
$arr1 = array(
2 => array(41000, 31079),
3 => array(42963, 41189)
);
$arr2 = array(
2 => array(40213, 42054),
3 => array(42998, 34567)
);
$arr3 = array();
foreach($arr1 as $key => $values){
if(!isset($arr3[$key])){
$arr3[$key] = array();
}
$arr3[$key] = array_merge($arr3[$key], $values);
}
foreach($arr2 as $key => $values){
if(!isset($arr3[$key])){
$arr3[$key] = array();
}
$arr3[$key] = array_merge($arr3[$key], $values);
}
echo '<pre>';
print_r($arr3);
Prints:
Array
(
[2] => Array
(
[0] => 41000
[1] => 31079
[2] => 40213
[3] => 42054
)
[3] => Array
(
[0] => 42963
[1] => 41189
[2] => 42998
[3] => 34567
)
)
I have an array like this-
Array
(
[sku] => Array
(
[0] => SKU125
[1] => SKU121
[2] => SKU122
[3] => SKU124
)
[variation_description] => Array
(
[0] => test another
[1] => test
[2] => test
[3] => test
)
[price_html] => Array
(
[0] => 400,200
[1] => 500
[2] => 600,300
[3] => 700
)
)
Is it possible to covert the array to like this table-
Any kind of help will be appreciated.
Thanks In Advance
As every key (sku , price_html etc) has same amount of data , so just push the corresponding key data to a new array.
$data = [
'sku' => ['SKU125', 'SKU121', 'SKU122', 'SKU124'],
'variation_description' => ['test another', 'test', 'test', 'test'],
'price_html' => ['400,200', '500', '600,300', '700']
];
$re_structured = [];
foreach ($data as $each_key_data ) {
foreach ($each_key_data as $key => $value2 ) {
$re_structured[$key][] = $value2;
}
}
var_dump($re_structured);
You can simply iterate through data and create a new array.
$data = [
'sku' => ['SKU125', 'SKU121', 'SKU122', 'SKU124'],
'variation_description' => ['test another', 'test', 'test', 'test'],
'price_html' => ['400,200', '500', '600,300', '700']
];
$mapped = [];
$keys = array_keys($data);
$rows = count($data[reset($keys)]);
for ($i = 0; $i < $rows; $i++) {
$row = [];
foreach ($keys as $key)
$row[] = $data[$key][$i];
$mapped[] = $row;
}
This will result in
print_r($mapped);
Array
(
[0] => Array
(
[0] => SKU125
[1] => test another
[2] => 400,200
)
[1] => Array
(
[0] => SKU121
[1] => test
[2] => 500
)
[2] => Array
(
[0] => SKU122
[1] => test
[2] => 600,300
)
[3] => Array
(
[0] => SKU124
[1] => test
[2] => 700
)
)
Array
(
[681074CRPAK4] => Array
(
[0] => 681074
[1] => 681074CRPAK4
[2] => 5602385431605
)
[681520XXXP6L] => Array
(
[0] => 681520
[1] => 681520XXXP6L
[2] => 5602385667394
)
[681530XXXP6V] => Array
(
[0] => 681530
[1] => 681530XXXP6V
[2] => 5602385667417
)
[681530XXXP6W] => Array
(
[0] => 681530
[1] => 681530XXXP6W
[2] => 5602385667424
)
[681530XXXP6X] => Array
(
[0] => 681530
[1] => 681530XXXP6X
[2] => 5602385667400
)
)
I want to compare the value of key[0] of each array.
If they are the same then I would like to add a new key[3] to each array with an id.
This is an array of variable products if the product has the same key[0] then its the same product with different variations.
If the key[0] is different from the previous then add id+1, in this example, I would like to end up with:
Array
(
[681074CRPAK4] => Array
(
[0] => 681074
[1] => 681074CRPAK4
[2] => 5602385431605
[3] => 1
)
[681520XXXP6L] => Array
(
[0] => 681520
[1] => 681520XXXP6L
[2] => 5602385667394
[3] => 2
)
[681530XXXP6V] => Array
(
[0] => 681530
[1] => 681530XXXP6V
[2] => 5602385667417
[3] => 3
)
[681530XXXP6W] => Array
(
[0] => 681530
[1] => 681530XXXP6W
[2] => 5602385667424
[3] => 3
)
[681530XXXP6X] => Array
(
[0] => 681530
[1] => 681530XXXP6X
[2] => 5602385667400
[3] => 3
)
)
can you guys help me with this?
I tried this:
but does not work
foreach ($new as $current_key => $current_array) {
foreach ($new as $search_key => $search_array) {
$ref1 = $current_array[0];
$ref2 = $search_array[0];
if (($search_key != $current_key) and ($ref1 == $ref2)) {
$current_array[3] = $p_id_product;
}
else{
$current_array[3] = $p_id_product++;
}
}
}
Assuming you have already sorted the array by the initial index, so at least they are grouped:
<?php
$data =
[
[
'foo',
'spam',
'bar',
],
[
'foo',
'eggs',
],
[
'bar',
'ham'
],
];
$output = [];
$counter = 0;
$last = null;
foreach($data as $k => $v) {
if($last !== $v[0])
$counter++;
$v[3] = $counter;
$output[$k] = $v;
$last = $v[0];
}
var_export($output);
Output:
array (
0 =>
array (
0 => 'foo',
1 => 'spam',
2 => 'bar',
3 => 1,
),
1 =>
array (
0 => 'foo',
1 => 'eggs',
3 => 1,
),
2 =>
array (
0 => 'bar',
1 => 'ham',
3 => 2,
),
)
I have build this array structure from dig query data.
[10] => Array
(
[id] => 150
[0] => 200.201.202.23
[1] => dns.name1.com
[2] => 200.201.202.24
[3] => dns.name2.com
[4] => 200.201.202.25
[5] => dns.name3.com
) `
I need something like:
[10] => Array
(
[0] => array ( [0] => 200.201.202.23
[1] => dns.name1.com
[id] => 150
)
[1] => array ( [0] => 200.201.202.24
[1] => dns.name2.com
[id] => 150
)
[2] => array ( [0] => 200.201.202.25
[1] => dns.name3.com
[id] => 150
)
) `
I'm not sure if this is possible?
Heres the code where i create the array:
At the first time from the dig i use array_push() to add content to it.
$temp = array();
$i = 0;
foreach ($digResult as $single){
if (preg_match('/(?:^|\s+)(\d+)(?:\s+|\n+|$)/', $single )){
$temp []["id"]= $single;
$i++;
}else {
$temp[$i][] = $single;
}
}
This will work for you :
<?php
$dataArray = array(10 => array
(
'id' => 150 ,
0 => '200.201.202.23' ,
1 => 'dns.name1.com',
2 => '200.201.202.24',
3 => 'dns.name2.com',
4 => '200.201.202.25',
5 => 'dns.name3.com',
)
);
$newArray = array();
$id = $dataArray[10]['id'];
for($i=0; $i< 6; $i++)
{
$newArray[10][] = array(0=>$dataArray[10][$i],1=>$dataArray[10][$i+1],'id'=> $id);
$i+=1;
}
print_r($newArray);
?>
This will output
Array
(
[10] => Array
(
[0] => Array
(
[0] => 200.201.202.23
[1] => dns.name1.com
[id] => 150
)
[1] => Array
(
[0] => 200.201.202.24
[1] => dns.name2.com
[id] => 150
)
[2] => Array
(
[0] => 200.201.202.25
[1] => dns.name3.com
[id] => 150
)
)
)
LIVE EXAMPLE : CLICK HERE
Try this:-
<?php
$array = array(
'id' => '150',
'0' => '200.201.202.23',
'1' => 'dns.name1.com',
'2' => '200.201.202.24',
'3' => 'dns.name2.com',
'4' => '200.201.202.25',
'5' => 'dns.name3.com'
);
$i = 0;
$arrayLenght = (count($array)-2);
$newArray = array();
while ($i <= $arrayLenght) {
$newArray[] = array(
"0" => $array[$i++],
"1" => $array[$i++],
"id" => $array['id']
);
}
echo '<pre>';
print_r($newArray);
echo '</pre>';
?>
Output:-
Array
(
[0] => Array
(
[0] => 200.201.202.23
[1] => dns.name1.com
[id] => 150
)
[1] => Array
(
[0] => 200.201.202.24
[1] => dns.name2.com
[id] => 150
)
[2] => Array
(
[0] => 200.201.202.25
[1] => dns.name3.com
[id] => 150
)
)
This is my array of timestamps. I would like to eliminate values within 30 seconds of each other, only keeping the value if there is not another value within 30 sec. Any help would be greatly appreciated!
Array
(
[99999] => Array
(
[0] => 1356399000
[1] => 1356398971
[2] => 1356399005
[3] => 1356413406
)
[99997] => Array
(
[0] => 1356399002
[1] => 1356399007
[2] => 1356398871
[3] => 1356398876
)
[99996] => Array
(
[0] => 1356399003
[1] => 1356399004
[2] => 1356399008
)
[99995] => Array
(
[0] => 1356399009
)
)
My expected output:
Array
(
[99999] => Array
(
[0] => 1356399000
[1] => 1356398971
[2] => 1356413406
)
[99997] => Array
(
[0] => 1356399002
[1] => 1356398871
)
[99996] => Array
(
[0] => 1356399003
)
[99995] => Array
(
[0] => 1356399009
)
)
Any solutions/advice would be greatly appreciated! Thanks!
Your output is wrong .. because 1356398971 + 30 = 1356399001 which is grater than 1356399000 if i understand you clearly this i what it should look like
$data = array(
99999 => array(
0 => 1356399000,
1 => 1356398971,
2 => 1356399005,
3 => 1356413406,
),
99997 => array(
0 => 1356399002,
1 => 1356399007,
2 => 1356398871,
3 => 1356398876,
),
99996 => array(
0 => 1356399003,
1 => 1356399004,
2 => 1356399008,
),
99995 => array(
0 => 1356399009,
),
);
echo "<pre>";
$data = array_map(function ($values) {
rsort($values);
$ci = new CachingIterator(new ArrayIterator($values));
$values = array();
foreach ( $ci as $ts ) {
if ($ci->hasNext()) {
abs($ci->current() - $ci->getInnerIterator()->current()) > 30 and $values[] = $ts;
} else {
$values[] = $ts;
}
}
sort($values);
return $values;
}, $data);
print_r($data);
Output
Array
(
[99999] => Array
(
[0] => 1356398971
[1] => 1356413406
)
[99997] => Array
(
[0] => 1356398871
[1] => 1356399002
)
[99996] => Array
(
[0] => 1356399003
)
[99995] => Array
(
[0] => 1356399009
)
)