So this is how my Array ($dataArray) looks oks like:
Array
(
[0] => Array
(
[0] => Date
[1] => Time
[2] => Duration
[3] => Info
[4] => Client
)
[1] => Array
(
[0] => 2021-12-01
[1] => 10:45:43
[2] => 237
[3] => Some text from
[4] => Client 1
)
[2] => Array
(
[0] => 2021-12-01
[1] => 11:29:13
[2] => 77
[3] => Nothing important
[4] => Client 2
)
[3] => Array
(
[0] => 2021-12-01
[1] => 11:53:03
[2] => 44
[3] => anonymous
[4] => Client 1
)
I need to Loop trough it to search the Client Names, and if i find the matching name in the Element 4 then delete the entire Array.
$ExportKDname = "Client 1"
foreach($dataArray as $key => $sub_array) {
if($sub_array[4] == $ExportKDname) {
unset($dataArray[$key]);
break;
}
}
print_r($dataArray);
But with this code none of the arrays will be deleted. And I just can not find the right way to do it.
The Final array what I need to look like if we find the "Client 1" in the array would be like this:
Array
(
[0] => Array
(
[0] => Date
[1] => Time
[2] => Duration
[3] => Info
[4] => Client
)
[1] => Array
(
[0] => 2021-12-01
[1] => 11:29:13
[2] => 77
[3] => Nothing important
[4] => Client 2
)
In the if condition you are saying "if u match with $sub_arr[4] == $ExportKDname unset it and stop the loop". the machine doing that. when it matched first time it removes and stoping. If u wanna delete all match do not write break; let it continue. So delete or make it comment break; line.
You can array_filter your variable and check if value is in_array.
With PHP 7.4+ syntax it should look like this:
$result = array_filter($dataArray, fn ($innerArray) => !in_array('Client 1', $innerArray));
Related
I have an arbitrary number of arrays all containing the same format of data. There are 2 separate for loops looping through two separate SQL query results and adding them to 2 separate arrays.
Once I have all the information in both arrays, I am walking through them and joining them together to make a longer array.
However, as I am writing this array to a csv file, The information needs to be in order in the array so it writes it in order to the csv file. How can I do this?
Array 1
[1] => Array
(
[0] => 2017-07-21 00:00:00
[1] => Foo
[2] => Bar
[3] => 32.63
[4] => 18.36
[5] => 98.46
)
[2] => Array
(
[0] => 2017-07-21 00:00:00
[1] => Foo
[2] => Bar
[3] => 29.74
[4] => 148.68
[5] => 178.42
)
//etc
Array 2
[1] => Array
(
[0] => RTGH707321222
[1] => THIS
[2] => IS
[3] => TEXT
)
[2] => Array
(
[0] => RTGH707321220
[1] => SOME
[2] => WORDS
[3] => HERE
)
//etc
Joining the arrays together
array_walk($array2, function($values, $key) use (&$array1) {
$array1[$key] = array_merge($array1[$key], $values);
} );
After The array Merge - print_r($array1)
[1] => Array
(
[0] => 2017-07-21 00:00:00
[1] => Foo
[2] => Bar
[3] => 32.63
[4] => 18.36
[5] => 98.46
[6] => RTGH707321222
[7] => THIS
[8] => IS
[9] => TEXT
)
[2] => Array
(
[0] => 2017-07-21 00:00:00
[1] => Foo
[2] => Bar
[3] => 29.74
[4] => 148.68
[5] => 178.42
[6] => RTGH707321220
[7] => SOME
[8] => WORDS
[9] => HERE
)
//etc
So this is working fine. However, I would like to move some of these indexes around so that they are in a different order. I have looked into array_splice() but I am not sure if this is the correct method to use.
What I want it to look like
[1] => Array
(
[0] => 2017-07-21 00:00:00
[1] => RTGH707321222
[2] => TEXT
[3] => THIS
[4] => 18.36
[5] => 98.46
[6] => Foo
[7] => 32.63
[8] => IS
[9] => Bar
)
//etc
As you can see, all the information is still the same. The values have just been moved to different indexes. How can I sort the array so that it looks like the above. Can anyone point me in the right direction? Thanks.
This is a simpler method using array_replace() and an ordering array.
No extra loop, no temporary swapping variables.
Code: (Demo)
$array1=[
1=>['2017-07-21 00:00:00','Foo','Bar',32.63,18.36,98.46],
2=>['2017-07-21 00:00:00','Foo','Bar',29.74,148.68,178.42]
];
$array2=[
1=>['RTGH707321222','THIS','IS','TEXT'],
2=>['RTGH707321220','SOME','WORDS','HERE']
];
$order=[0=>'',6=>'',9=>'',7=>'',4=>'',5=>'',1=>'',3=>'',8=>'',2=>''];
array_walk($array2, function($values, $key) use (&$array1,$order) {
$array1[$key] = array_replace($order,array_merge($array1[$key], $values));
});
var_export($array1);
we can use swap technice here like,
<?php
foreach ($arr as $key => $value) {
$swap = $value[1];
$arr[$key][1] = $value[6];
$arr[$key][6] = $swap;
$swap = $value[9];
$arr[$key][9] = $value[2];
$arr[$key][2] = $swap;
$swap = $value[7];
$arr[$key][7] = $value[3];
$arr[$key][3] = $swap;
}
print_r($arr);
?>
$arr is your array.
This is my array () ...not much English'm using google translator.
I'm printing this array with print_r (). but what I deceo is sort of form this down
Array
(
[0] => Array
(
[0] => 606125999550609
[1] => Patricia
[2] => Michelle
)
[1] => Array
(
[0] => 724417787635260
[1] => Nasshy
[2] => Green
)
[2] => Array
(
[0] => 1121064174618668
[1] => Luisanna
[2] => Rodriguez
)
[3] => Array
(
[0] => 1057585894278115
[1] => Libane
[2] => Heredia
)
)
Basically what I need is to sort this array as follows......
So I do not know how to sort follows in PHP...
Array
(
[0] => 606125999550609
[1] => 724417787635260
[2] => 1121064174618668
[3] => 1057585894278115
[4] => Patricia
[5] => Nasshy
[6] => Luisanna
[7] => Libane
[8] => Michelle
[9] => Green
[10] => Rodriguez
[11] => Heredia
)
This isn't so much "sorting", its' more of a manipulation/restructure. Using a loop to regenerate your array would be the option, but if you can modify the data from where it comes from, then that's always recommended.
$new = array();
array_map(function($obj) use(&$new) {
foreach($obj as $i => $elem) {
$new[$i][] = $elem;
}
}, $array);
In the example above, we're using array_map() to apply our function() {... that runs the loop of each element, applying it to our $new array.
All you need to do is pass your $array in as you see above.
Example/Demo
I need help figuring out what I am doing wrong and/or if there is a better way to do it, my question is two part. The first part is I am using Joomla 3.5.1 and in the template I have two fieldsets, one that allows the user to enter links to their social media accounts and the second I am attempting to make a sort if you will using a set of drop downs. This may or may not be the best so I am open to options here.
Now for the second part. I am able to bring in the both field sets into separate arrays, for simplicity array 1 and array 2. Array 1 has the links and array 2 has the order. So my question is this, how would be the best way to loop through and match everything up while removing the empties in array 1 and "none" in array 2.
One thought I did have is should array 2 not be multidimensional and let the key be the "soXYZ", but then how would the best way to match it up knowing that the lengths are different and the order as well.
Array 2 where (-1) are "None" in the drop downs
Array
(
[0] => soPhone
[1] => soContact
[2] => soFacebook
[3] => soMap
[4] => -1
[5] => -1
[6] => -1
[7] => -1
[8] => -1
[9] => -1
[10] => -1
[11] => -1
[12] => -1
[13] => -1
[14] => -1
)
Array 1
Array
(
[0] => Array
(
[0] => soPhone
[1] => 555.867.5309
)
[1] => Array
(
[0] => soContact
[1] => Contact
)
[2] => Array
(
[0] => soMap
[1] => Map
)
[3] => Array
(
[0] => soFacebook
[1] => Facebook
)
[4] => Array
(
[0] => soTwitter
[1] => Twitter
)
[5] => Array
(
[0] => soGoogle
[1] => Google Plus
)
[6] => Array
(
[0] => soLinkedIn
[1] => Linked In
)
[7] => Array
(
[0] => soPinterest
[1] => Pinerest
)
[8] => Array
(
[0] => soYouTube
[1] => YouTube
)
[9] => Array
(
[0] => soVimeo
[1] => Vimeo
)
[10] => Array
(
[0] => soYelp
[1] => Yelp
)
[11] => Array
(
[0] => soInstagram
[1] => Instagram
)
[12] => Array
(
[0] => soTripAdvisor
[1] => Trip Advisor
)
[13] => Array
(
[0] => soHouzz
[1] => Houzz
)
[14] => Array
(
[0] => soAngiesList
[1] => Angies List
)
)
Array Result
Array
(
[0] => Array
(
[0] => soPhone
[1] => 555.867.5309
)
[1] => Array
(
[0] => soContact
[1] => Contact
)
[2] => Array
(
[0] => soFacebook
[1] => Facebook
)
[3] => Array
(
[0] => soMap
[1] => Map
)
)
Any guidance is appreciated.
After the power of posting I was able to eventually get my desired result, which as follows:
I set up three arrays arrLinks (originally Array 1) , arrLinksOrder (originally Array 2) and arrLinksCombined (Array Result), then populated arrLinks with the user entered text links on the template, arrLinksOrder pulls from the drop downs.
My original plan was to have arrLinks be multidimensional but in the end I just set the key's for each in my code.
I then used this stackoverflow link to help combine the arrLinksOrder and arrLinks. Once everything is combined I was able to build the html and really clean up the file to something easier to manage.
Code below:
// Populate the contents of the text-box into an array
$arrLinks = array(
"soFacebook"=> $tbFacebook,
"soTwitter"=> $tbTwitter,
"soGoogle"=> $tbGoogle,
"soLinkedIn"=> $tbLinkedIn,
"soPinterest"=> $tbPinterest,
"soYouTube"=> $tbYouTube,
"soInstagram"=> $tbInstagram,
);
// Populate an array (arrLinksOrder) with the drop down selections
for ($i=1; $i < ($linksCount+1); $i++) {
$varName = 'listSocialOrder'.$i;
$arrLinksOrder[$i] = $$varName;
}
// Combine arrLinksOrder and arrLinks, if the arrLinksOrder exists in arrLinks
foreach($arrLinksOrder as $key) {
if(array_key_exists($key, $arrLinks)) {
$arrLinksCombined[$key] = $arrLinks[$key];
}
}
If anyone has any improvements please feel free to share, this is what worked for me at the time.
I have two array as following
Array
(
[0] => 641
[1] => 622
[2] => 100
[3] => 6431
)
Array
(
[0] => 64.1
[1] => 62.2
[2] => 10
[3] => 643.1
)
How can I make it as following
Array
(
[0] => 641
[1] => 64.1
[2] => 622
[3] => 62.2
[4] => 100
[5] => 10
[6] => 6431
[7] => 643.1
)
It's as simple as
$result=array_merge($array1,$array2);
Note: Your values wont be in the order you presented though. If that is important then you need to loop through your arrays to build a new array accordingly.
Ummm ok here is that version as well
if(count($array1)==count($array2))
{
for($i=0;$i<count($array1);$i++)
{
$result[]=$array1[$i];
$result[]=$array2[$i];
}
}
print_r($result);
Fiddle
Manual
you can use array_merge() function merges one or more arrays into one array.
example:
array_merge(array1,array2,array3...)
I need to parse some data from a text and so i copy/pasted it into an array and used it like this:
$array = array("_alpha","_beta","_gama","_delta","_epsilon");
foreach ($array as $key => $value) {
{use the value in my script bellow....}
}
The data was coming from a script that had private declarations on the top of the file looked like this, so it was easy to just copy/paste it.
$private = ["_alpha","_beta","_gama","_delta","_epsilon"];
Now i had to parse many files so i tried to gather that $array data as best as i could into one array, so i can parse it using the same function...Well the best i could come out with is this:
Array
(
[0] => "_alpha","_delta","_beta","_epsilon","_delta","_kapa",
[1] => "_beta","_alpha","_delta","_kapa","_lamda","_epsilon","_array","_alpha"
[2] => "_epsilon","_array","_alpha","_theta","_omega"
[3] => "_alpha"
[4] => "_kapa","_lamda","_epsilon"
[5] => "_beta","_epsilon","_delta","_kapa","_lamda"
[6] => "_omega","_omega","_delta"
....
....
}
Each line represents the header from a file. Note the quotes around the values...
How can i make the above look like this:
Array
(
[0] => _alpha
[1] => _beta
[2] => _gama
[3] => _delta
[4] => _epsilon
...
...
..
)
Each value on its own, and without the quote or commas etc....just a plain value.
I can also extract the data in this format..but i think thats harder to do:
Array
(
[0] => Array
(
[0] => "_alpha"
[1] => "_beta"
[2] => "_gama"
[3] => "_delta"
[4] => "_epsilon"
...
...
..
)
[1] => Array
(
[0] => "_alpha"
[1] => "_beta"
[2] => "_gama"
[3] => "_delta"
[4] => "_epsilon"
[5] => "_epsilon"
...
..
)
...... etc etc ......
[10] => Array
(
[0] => "_alpha"
[1] => "_delta"
[2] => "_omega"
)
}
_Thanks
$private = ["_alpha","_beta","_gama","_delta","_epsilon"];
print_r($private);
Array
(
[0] => _alpha
[1] => _beta
[2] => _gama
[3] => _delta
[4] => _epsilon
)
i dont know whats the problem
but when i use your codes everything works fine