array_push adds another empty array after adding my array - php

I was working on a way to populate an empty array.
I have this code:
$array = array();
$month = 'enero';
array_push($array, $array[$month] = array('01'));
array_push($array['enero'], '02');
print_r($array);
This returns:
Array
(
[enero] => Array
(
[0] => 01
[1] => 02
)
[0] => Array
(
[0] => 01
)
)
The array [0] appears from nowhere and I don't know what to do. I have tried
array_push($array['enero'], '02');
But it does not work. How can I get the expected result:
Array
(
[enero] => Array
(
[0] => 01
[1] => 02
)
)

When in doubt, avoid array_push and just use [] notation. It has the advantage of automatically creating sub-arrays that don't exist (so no need to use $array[$month] = array();):
$array = array();
$month = 'enero';
$array[$month][] = '01';
$array[$month][] = '02';
print_r($array);
If you want to use array_push, you need to create the enero element first before attempting to push into it:
$array = array();
$month = 'enero';
$array[$month] = array();
array_push($array[$month], '01');
array_push($array[$month], '02');
print_r($array);
Output (for both pieces of code):
Array
(
[enero] => Array
(
[0] => 01
[1] => 02
)
)
Demo on 3v4l.org

Related

Remove duplicate rows from array even if their element ordering is different

in PHP how to get this unique of array ?
$originalArray = [[2,48],[48,2],[19,31],[31,19]];
I want to get
Array
(
[0] => Array
(
[0] => 2
[1] => 48
)
[2] => Array
(
[0] => 19
[1] => 31
)
)
I tried with array_unique($array,SORT_REGULAR) but still won't work.
Is there other function to achieve this ?
Similiar to this technique. Use sorted, stringified keys to build the filtered result.
Code: (Demo)
$array = [[2,48],[48,2],[19,31],[31,19]];
$result = [];
foreach ($array as $row) {
sort($row);
$result[json_encode($row)] ??= $row; // just = will also work
}
var_export(array_values($result));

Associative Array From Multidimensional Array

I am new to PHP an needed little help. It may be easy for some but giving me a tough time.
I have an array
Array ( [0] => page-18 [1] => page-20 )
Which I would like to explode further by '-':
$mainStringBrk = array('page-18', 'page-20');
$finalArray = array();
foreach($mainStringBrk as $bString){
$mainStringBrkBrk = explode('-', $bString);
$finalArray[$mainStringBrkBrk[0]] = $mainStringBrkBrk[1];
}
echo '<pre>'; print_r($finalArray);
When I do, it outputs only the last key and value of array.
Array ( page => 20 )
My desired output is:
Array ( page => 18, page => 20 )
I am wondering if anyone can guide me in right direction.
You can't achieve the result you want as it is not possible to have an array with identical keys; this is why you only have one result in your output. You could change your output structure to a 2-dimensional array to work around this e.g.
$mainStringBrk = array('page-18', 'page-20');
$finalArray = array();
foreach($mainStringBrk as $bString){
$mainStringBrkBrk = explode('-', $bString);
$finalArray[$mainStringBrkBrk[0]][] = $mainStringBrkBrk[1];
}
print_r($finalArray);
Output:
Array
(
[page] => Array
(
[0] => 18
[1] => 20
)
)
Or you can adopt this structure if it is better suited to your needs:
$finalArray = array();
foreach($mainStringBrk as $bString){
$mainStringBrkBrk = explode('-', $bString);
$finalArray[] = array($mainStringBrkBrk[0] => $mainStringBrkBrk[1]);
}
print_r($finalArray);
Output:
Array
(
[0] => Array
(
[page] => 18
)
[1] => Array
(
[page] => 20
)
)
Demo on 3v4l.org

Difference between two JSON strings in php and remove duplicate?

$json_str1 = '[{"13:00":"1"},{"14:00":"1"},{"15:30":"1"},{"16:30":"1"},{"1:00":"1"}]';
$json_str2 = '[{"13:00":"1"},{"14:00":"1"},{"15:30":"1"},{"12:30":"1"}]';
These are two JSON strings and I want the result like difference between them like {"1:00":"1"} and one more {"12:30":"1"}
The solution of this has a many aspects, cause there's 3 different values:
{"16:30":"1"},{"1:00":"1"}
{"12:30":"1"}
Firstly, you can convert your JSON string into array with json_decode($str,true):
json_decode($json_str1, true);
json_decode($json_str2, true);
Outputs like:
Array
(
[0] => Array
(
[13:00] => 1
)
...
Then create combined array with values like JSON object elements with foreach loop:
$str1 = [];
foreach($data1 as $row){
$str1[] = json_encode($row);
}
Outputs like:
Array
(
[0] => {"13:00":"1"}
[1] => {"14:00":"1"}
[2] => {"15:30":"1"}
[3] => {"16:30":"1"}
[4] => {"1:00":"1"}
)
...
Then you can find the difference between this two arrays, but, you need to do it in both ways (compare $array1 with $array2 and $array2 with $array1):
$dif1 = array_diff($str1, $str2);
$dif2 = array_diff($str2, $str1);
Outputs:
Array
(
[3] => {"16:30":"1"}
[4] => {"1:00":"1"}
)
Array
(
[3] => {"12:30":"1"}
)
Finally you can merge the result with:
$fin = array_merge($dif1, $dif2);
Outputs:
Array
(
[0] => {"16:30":"1"}
[1] => {"1:00":"1"}
[2] => {"12:30":"1"}
)
All of this you can put in a separate function like:
function getDifference2JSONstrings($jsonStr1, $jsonStr2){
$data1 = json_decode($jsonStr1, true);
$data2 = json_decode($jsonStr2, true);
$str1 = [];
$str2 = [];
foreach($data1 as $row){
$str1[] = json_encode($row);
}
foreach($data2 as $row){
$str2[] = json_encode($row);
}
return array_merge(array_diff($str1, $str2), array_diff($str2, $str1));
}
$res = getDifference2JSONstrings($json_str1, $json_str2);
print_r($res);
Outputs an array:
Array
(
[0] => {"16:30":"1"}
[1] => {"1:00":"1"}
[2] => {"12:30":"1"}
)
Demo

Merger Array Table

I have two array, I would like to merge them without duplicating in "name",
$array1[]= array(name['udi','ari'],id['1','2'])
$array2[]= array(name['udi','ari'],age['22','18'])
result
$arrayresult[]= array(name['udi','ari'],id['1','2'],age['22','18'])
Just simply use array_merge for merging both array as:
Example:
<?php
$array1 = array(
'name'=>array('udi','ari'),
'id'=>array('1','2'),
);
$array2 = array(
'name'=>array('udi','ari'),
'age'=>array('22','18'),
);
$newArr = array_merge($array1,$array2);
echo "<pre>";
print_r($newArr);
?>
Result:
Array
(
[name] => Array
(
[0] => udi
[1] => ari
)
[id] => Array
(
[0] => 1
[1] => 2
)
[age] => Array
(
[0] => 22
[1] => 18
)
)
You can use first $result=array_merge($array1,$array2) and then use $result=array_unique($result) its remove duplicate value.
I think what you're looking for is array_merge:
http://php.net/manual/en/function.array-merge.php
$arrayresult = array_merge($array1,$array2);
should give you:
$arrayresult = array(name('udi','ari'),id('1','2'),age('22','18'))

Formatting an Array in PHP

I'm pulling an array from the database and it looks like so:
Array
(
[0] => Array
(
[tracker_id] => 28
[tracking_numbers] => hdkshwuy47937892hd
)
[1] => Array
(
[tracker_id] => 28
[tracking_numbers] => 797825464411
)
)
I need to reformat it to look like this:
Array
(
[0] => Array
(
[tracker_id] => 28
[tracking_numbers] => Array
(
[0] => hdkshwuy47937892hd
[1] => 797825464411
)
)
)
I can't seem find the right search in the array or keys to create an array of numbers for the single tracker id.
Use array_column() for < php V5.5
<?php
$a=array
( array
('tracker_id' => 28,
'tracking_numbers'=> "hdkshwuy47937892hd"
),
array('tracker_id' => 28,
'tracking_numbers' => "797825464411",
) );
$a[0]['tracking_numbers']=array_column($a,"tracking_numbers");
unset($a[1]);
print_r($a);
Demo
try this
$arr_output = array();
foreach($arr_input as $arr)
{
$tracker_id = $arr['tracker_id'];
$tracking_numbers = $arr['tracking_numbers'];
$arr_output[$traker_id][] = $tracking_numbers;
}
print_r($arr_output);
UPDATE 2:
$arr_output = array();
$arr_output1 = array();
foreach($arr_input as $arr)
{
$tracker_id = $arr['tracker_id'];
$tracking_numbers = $arr['tracking_numbers'];
$arr_output[$traker_id][] = $tracking_numbers;
}
$i=0;
foreach($arr_output as $key=>$value)
{
$arr_output1[$i]['tracker_id']=$key
$arr_output1[$i]['tracking_numbers']=$value
$i+=1;
}
print_r($arr_output1);

Categories