I want to expand my city array with post code value.
If the city_postcode array contain city array name record then push postcode value into city array. That's what i want to achive somehow.
city array:
Array
(
[0] => Array
(
[id] => 1
[city] => Budapest
[population] => 1700000
)
[1] => Array
(
[id] => 2
[city] => Szeged
[population] => 160000
)
)
city_postcode array:
Array
(
[0] => Array
(
[name] => Budapest
[post_code] => 12345
)
[1] => Array
(
[name] => Szeged
[post_code] => 33356
)
)
The result I want:
Array
(
[0] => Array
(
[id] => 1
[city] => Budapest
[population] => 1700000
[post_code] => 12345
)
[1] => Array
(
[id] => 2
[city] => Szeged
[population] => 160000
[post_code] => 33356
)
)
If you can rely on the cities and post codes to be equal in length and sorted, you could just do something like this:
<?php
$cities = array(
array("id"=>1,"city"=>"Budapest","Population"=>"1700000"),
array("id"=>2,"city"=>"Szeged","Population"=>"160000")
);
$cityPostCode = array(
array("name"=>"Budapest","post_code"=>12345),
array("name"=>"Szeged","post_code"=>33356)
);
for($i = 0; $i < count($cities); $i++){
$cities[$i]['post_code'] = $cityPostCode[$i]['post_code'];
}
print_r($cities);
Other wise you could do something more dyanmic like:
function _parsePostCode($targetName,$targetArr){
foreach($targetArr as $el){
if($el['name'] == $targetName){
return $el['post_code'];
}
}
return false;
}
for($i = 0; $i < count($cities); $i++){
$cities[$i]['post_code'] = _parsePostCode($cities[$i]['city'],$cityPostCode);
}
print_r($cities);
As an alternative you can use 'reference' PHP in foreach loop as follows
$city = array(
0 => array(
'id' => 1,
'city' => "Budapest",
'population' => 1700000
),
1 => array(
'id' => 2,
'city' => "Szeged",
'population' => 160000
)
);
$city_postcode = array(
0 =>array(
'name' => 'Budapest',
'post_code' => 12345
),
1 => array(
'name' => 'Szeged',
'post_code' => 33356
)
);
foreach ($city as $ckey => &$cval) {
$cval['post_code'] = $city_postcode[$ckey]['post_code'];
}
unset($cval);
var_dump($city);
You can use this simple approach :
<?php
$city = array(
0 => array(
'id' => 1,
'city' => "Budapest",
'population' => 1700000
),
1 => array(
'id' => 2,
'city' => "Szeged",
'population' => 160000
)
);
$city_postcode = array(
0 => array(
'name' => 'Budapest',
'post_code' => 12345
),
1 => array(
'name' => 'Szeged',
'post_code' => 33356
)
);
function apply_post_code(&$item, $key, $postcode)
{
$item['post_code'] = $postcode[ $key ][ 'post_code' ];
}
array_walk($city, 'apply_post_code', $city_postcode);
var_dump($city);
Note that the $city variable has been passes by reference
Related
I need to convert the below 2d array in to specified 2d array format. Array contains multiple parent and multiple child array. Also, have tried to convert the code, but am not getting the expected output.
This is the code what i have tried,
$a1 = array(
'0' =>
array(
'banner_details' =>
array(
'id' => 2,
'section_id' => 24
),
'slide_details' =>
array(
0 => array(
'id' => 74,
'name' => 'Ads1'
),
1 => array(
'id' => 2,
'name' => 'Ads2'
)
)
),
'1' =>
array(
'banner_details' =>
array(
'id' => 106,
'section_id' => 92
),
'slide_details' =>
array(
0 => array(
'id' => 2001,
'name' => 'Adv1'
),
1 => array(
'id' => 2002,
'name' => 'Adv2'
)
)
)
);
$s = [];
for($i = 0; $i<2; $i++) {
foreach($a1[$i]['slide_details'] as $vs){
$s[] = $vs;
}
}
My output:
Array
(
[0] => Array
(
[id] => 74
[name] => Ads1
)
[1] => Array
(
[id] => 2
[name] => Ads2
)
[2] => Array
(
[id] => 2001
[name] => Adv1
)
[3] => Array
(
[id] => 2002
[name] => Adv2
)
)
Expected output:
Array
(
[24] => Array
(
[0] => 74
[1] => 2
)
[92] => Array
(
[0] => 2001
[1] => 2002
)
)
please check the above code and let me know.
Thanks,
You can apply next simple foreach loop with help of isset() function:
foreach($a1 as $data){
if (isset($data['banner_details']['section_id'])){
$s[$data['banner_details']['section_id']] = [];
if (isset($data['slide_details'])){
foreach($data['slide_details'] as $row){
$s[$data['banner_details']['section_id']][] = $row['id'];
}
}
}
}
Demo
If you know that indexes like banner_details or slide_details or section_id will be there always then you can skip isset() in if statements.
You can use array_column function for simple solution:
$result = [];
foreach ($a1 as $item)
{
$result[$item['banner_details']['section_id']] = array_column($item['slide_details'], 'id');
}
var_dump($result);
I have this type of array
$arr = array(
0 => array(
0 => array(
'name' => 'test1',
'country' => 'abc'
)
1 => array(
'name' => 'test2',
'country' => 'xyz'
)
)
1 => array(
'name' => 'test3',
'country' => 'pqr'
)
);
How can I make all arrays as parallel arrays. So that all sub arrays are parallel to each other without using any loop.
Like this
$arr = array(
0 => array(
'name' => 'test1',
'country' => 'abc'
)
1 => array(
'name' => 'test2',
'country' => 'xyz'
)
2 => array(
'name' => 'test3',
'country' => 'pqr'
)
);
Any help is much appreciated. !
A dynamic version of Nigel's code would be to loop the array and merge each subarray.
$new = [];
foreach($arr as $subarr){
$new = array_merge($new, $subarr);
}
var_dump($new);
https://3v4l.org/np2ZD
You could simply merge the arrays...
$out = array_merge($arr[0], [$arr[1]]);
print_r($out);
Which gives...
Array
(
[0] => Array
(
[name] => test1
[country] => abc
)
[1] => Array
(
[name] => test2
[country] => xyz
)
[2] => Array
(
[name] => test3
[country] => pqr
)
)
I have one array and try to change some key and value for example if sku are same than i need to merge image. Below array i have
Array
(
[0] => Array
(
[sku] => h-eldora
[name] => H ELDORA
[image] => s/files/1/1282/4221/products/h_eldora_01.jpg?v=1476667054
)
[1] => Array
(
[sku] => h-eldora
[name] =>
[image] => s/files/1/1282/4221/products/h_eldora_02.jpg?v=1475116221
)
[2] => Array
(
[sku] => h-eldora
[name] =>
[image] => s/files/1/1282/4221/products/20100707164858197_1_88da6866-701a-42b9-b523-5e454cbcce70.jpg?v=1475717598
)
[3] => Array
(
[sku] => hl-dracy
[name] => HL DRACY
[image] => s/files/1/1282/4221/products/h_dracy_01.jpg?v=1475115222
)
[4] => Array
(
[sku] => hl-dracy
[name] =>
[image] => s/files/1/1282/4221/products/h_dracy_02.jpg?v=1475115223
)
[5] => Array
(
[sku] => hl-dracy
[name] =>
[image] =>s/files/1/1282/4221/products/20100707164858197_1_633237aa-36ec-441b-a074-03844f6a0858.jpg?v=1475719793
)
)
I need to merge array like this
Array
(
[0] => Array
(
[sku] => h-eldora
[name] =>
[image1] => s/files/1/1282/4221/products/h_eldora_02.jpg?v=1475116221
[image2] => s/files/1/1282/4221/products/20100707164858197_1_88da6866-701a-42b9-b523-5e454cbcce70.jpg?v=1475717598
[image3] => s/files/1/1282/4221/products/20100707164858197_1_88da6866-701a-42b9-b523-5e454cbcce70.jpg?v=1475717598
)
[1] => Array
(
[sku] => hl-dracy
[name] => HL DRACY
[image1] => s/files/1/1282/4221/products/h_dracy_01.jpg?v=1475115222
[image2] => s/files/1/1282/4221/products/h_dracy_02.jpg?v=1475115223
[image3] => s/files/1/1282/4221/products/20100707164858197_1_633237aa-36ec-441b-a074-03844f6a0858.jpg?v=1475719793
)
)
If any php function is there than please let me know or any code suggestion
Using simple PHP:
<?php
$arr1 = array(
0 => array(
'sku' => 'h-eldora',
'name' => 'H ELDORA',
'image' => 's/files/1/1282/4221/products/h_eldora_01.jpg?v=1476667054'
),
1 => array(
'sku' => 'h-eldora',
'name' => '',
'image' => 's/files/1/1282/4221/products/h_eldora_02.jpg?v=1475116221'
),
2 => array(
'sku' => 'h-eldora',
'name' => '',
'image' => 's/files/1/1282/4221/products/20100707164858197_1_88da6866-701a-42b9-b523-5e454cbcce70.jpg?v=1475717598'
),
3 => array(
'sku' => 'hl-dracy',
'name' => 'HL DRACY',
'image' => 's/files/1/1282/4221/products/h_dracy_01.jpg?v=1475115222'
),
4 => array(
'sku' => 'hl-dracy',
'name' => 'H ELDORA',
'image' => 's/files/1/1282/4221/products/h_dracy_02.jpg?v=1475115223'
),
5 => array(
'sku' => 'hl-dracy',
'name' => 'H ELDORA',
'image' => 's/files/1/1282/4221/products/20100707164858197_1_633237aa-36ec-441b-a074-03844f6a0858.jpg?v=1475719793'
)
);
$newArr = $imgIndex = array();
foreach($arr1 as $a){
if( !array_key_exists($a['sku'],$newArr) ){
$newArr[$a['sku']] = array(
'sku' => $a['sku'],
'name' => $a['name'],
'image1' => $a['image']
);
$imgFound[$a['sku']] = 1;
}else{
$imgFound[$a['sku']]++;
$newArr[$a['sku']]['image'.$imgFound[$a['sku']]] = $a['image'];
}
}
unset($imgFound);
echo '<pre>'; print_r($newArr); echo '</pre>';
?>
This could work for you:
$data = // your input array
$uniqueSKUs = Array();
$newArray = Array();
$currentIndex = -1;
foreach ($data as $item) {
if (!in_array($item['sku'], $uniqueSKUs)) {
$currentIndex++;
$uniqueSKUs[] = $item['sku'];
$newArray[$currentIndex] = Array(
'sku' => $item['sku'],
'name' => $item['name']
);
}
$newArray[$currentIndex]['images'][] = $item['image'];
}
echo "<pre>";
var_dump($newArray);
echo "</pre>";
I am trying to update the below array to set "duplicate" => true when both "name" and "date" are the same. In the below example 'array[1][duplicate]=>true' as both
array[0] & array[1] have the same "name"=john & "date"=2015-7-24
Array
(
[0] => Array
(
[id] => 1
[name] => john
[date] => 2015-07-24
[duplicate] => false
)
[1] => Array
(
[id] => 1
[name] => john
[date] => 2015-07-24
[duplicate] => false
)
[2] => Array
(
[id] => 1
[name] => jane
[date] => 2015-07-24
[duplicate] => false
)
[3] => Array
(
[id] => 1
[name] => notJaneORJohn
[date] => 2015-07-24
[duplicate] => false
)
[4] => Array
(
[id] => 1
[name] => jane
[date] => 2099-07-24
[duplicate] => false
)
)
Try this,
$array = Array
(
0 => Array
(
'id' => '1',
'name' => 'john',
'date' => '2015-07-24',
'duplicate' => 'false',
),
1 => Array
(
'id' => 1,
'name' => 'john',
'date' => '2015-07-24',
'duplicate' => 'false'
),
2 => Array
(
'id' => 1,
'name' => 'jane',
'date' => '2015-07-24',
'duplicate' => 'false'
),
3 => Array
(
'id' => 1,
'name' => 'notJaneORJohn',
'date' => '2015-07-24',
'duplicate' => 'false'
),
4 => Array
(
'id' => 1,
'name' => 'jane',
'date' => '2099-07-24',
'duplicate' => 'false'
)
);
foreach ($array as $key => $value) {
for ($i = $key + 1 ; $i < sizeof($array); $i++) {
if ($value['name'] === $array[$i]['name'] && $value['date'] === $array[$i]['date']) {
$array[$key]['duplicate'] = 'TRUE';
$array[$i]['duplicate'] = 'TRUE';
}
}
}
This would work:
$Arr = Array(
['id'=>1, 'name'=>'john', 'date'=>'2015-07-24', 'duplicate'=>0],
['id'=>1, 'name'=>'john', 'date'=>'2015-07-24', 'duplicate'=>0],
['id'=>1, 'name'=>'Jane', 'date'=>'2015-07-24', 'duplicate'=>0]
);
foreach($Arr as $i1 => $v1){
$Str1 = $v1['name'].$v1['date'];
foreach($Arr as $i2 => $v2){
if( $i1 !== $i2 && $Str1 === $v2['name'].$v2['date'] ){
$Arr[$i1]['duplicate'] = 1;
}
}
}
echo '<pre>',print_r($Arr),'</pre>'; die();
... outputs:
Array(
[0] => Array
(
[id] => 1
[name] => john
[date] => 2015-07-24
[duplicate] => 1
)
[1] => Array
(
[id] => 1
[name] => john
[date] => 2015-07-24
[duplicate] => 1
)
[2] => Array
(
[id] => 1
[name] => Jane
[date] => 2015-07-24
[duplicate] => 0
)
)
Have a look at this shortcut method ;)
<?php $testarr = array(
array("id" => 1,"name" => "john","date" => "2015-07-24","duplicate" => "false"),
array("id" => 1,"name" => "john","date" => "2015-07-24","duplicate" => "false"),
array("id" => 1,"name" => "jane","date" => "2015-07-24","duplicate" => "false"),
array("id" => 1,"name" => "notJaneORJohn","date" => "2015-07-24","duplicate" => "false"),
array("id" => 1,"name" => "jane","date" => "2099-07-24","duplicate" => "false")
);
$tempArray = array();
function checkDuplicate(&$arr) {
global $tempArray;
if (count($tempArray) > 0 && in_array($arr['name'], $tempArray) && in_array($arr['date'], $tempArray)) {
$arr['duplicate'] = "true";
} else {
$tempArray[] = $arr['name'];
$tempArray[] = $arr['date'];
}
}
array_walk($testarr, 'checkDuplicate');
print_r($testarr);
I'm trying to compare one array, against another array.
$dropship_array = array();
$dropship_query = tep_db_query("select id, email from drop_shippers");
while ($dropship = tep_db_fetch_array($dropship_query)) {
$dropship_array[] = array('id' => $dropship['id'],
'email' => $dropship['email']);
}
Now, $dropship_array[] contains:
Array (
[0] => Array (
[id] => 0
[email] => none
)
[1] => Array (
[id] => 2
[email] => dropshipper1#gmail.com
)
[2] => Array (
[id] => 5
[email] => dropshipper2#gmail.com
)
[2] => Array (
[id] => 10
[email] => dropshipper3#gmail.com
)
)
Now, I need to compare the array above (dropship_array['id']) against the array bellow (products_array['dsid']). The products_array[] array has been split so that each individual array is grouped together according to each ones [dsid]. So every time a match is found between a group of products and the drop ship id, a function needs to be performed.
$products_array = array();
$products_query = tep_db_query("select products_id, products_name, drop_ship_id from " . TABLE_ORDERS_PRODUCTS . " where orders_id = '" . (int)$orders['orders_id'] . "' order by products_name");
while ($products = tep_db_fetch_array($products_query)) {
$products_array[] = array('id' => $products['products_id'],
'text' => $products['products_name'],
'dsid' => $products['drop_ship_id']);
}
Array (
[0] => Array (
[0] => Array (
[id] => 793
[text] => Gun Dog Training Book
[dsid] => 8
)
)
[1] => Array (
[0] => Array (
[id] => 789
[text] => Top Dog II Training DVD Video
[dsid] => 5
)
[1] => Array (
[id] => 237
[text] => Tri-Tronics Retriever Training Book
[dsid] => 5
)
)
)
Would this require a foreach function?
<?php
$one = array(
'0' => array(
'id' => '0',
'email' => 'none'
),
'1' => array(
'id' => '1',
'email' => 'none'
),
'2' => array(
'id' => '2',
'email' => 'none'
),
'3' => array(
'id' => '3',
'email' => 'none'
)
);
$two = array(
'0' => array(
'0' => array(
'id' => '793',
'text' => 'derp',
'dsid' => '8'
)
),
'1' => array(
'0' => array(
'id' => '793',
'text' => 'derp',
'dsid' => '8'
),
'1' => array(
'id' => '793',
'text' => 'derp',
'dsid' => '3'
)
),
);
foreach($one as $item) {
foreach($two as $compare) {
if(is_array($compare)) {
foreach($compare as $multicompare) {
if($multicompare['dsid'] == $item['id']) {
// Perform function
}
}
}
}
}
}
?>
Breaking it down:
First foreach() loops through the very first array from your drop_shippers table.
Second foreach() loops through your next array and checks if the item is an array, if so it compares the dsid with the id from the first array.